Class: Ra::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/ra/color.rb

Overview

Color can be represented using r / g / b. Each component is assigned a number between 0.0 and 1.0. A color can also be converted for use when saving as [PPM](netpbm.sourceforge.net/doc/ppm.html).

color = Ra::Color.hex("#00FF00")
color.r == 0.0
color.g == 1.0
color.b == 0.0

color = Ra::Color.new(
  r: 0.5,
  g: 0.7,
  b: 0.9,
)
color.ppm == "128 179 230"

Constant Summary collapse

PRECISION =
255

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r: 0.0, g: 0.0, b: 0.0) ⇒ Color

Returns a new instance of Color.

Parameters:

  • r (Numeric) (defaults to: 0.0)

    between 0.0 and 1.0

  • g (Numeric) (defaults to: 0.0)

    between 0.0 and 1.0

  • b (Numeric) (defaults to: 0.0)

    between 0.0 and 1.0



65
66
67
68
69
# File 'lib/ra/color.rb', line 65

def initialize(r: 0.0, g: 0.0, b: 0.0)
  @r = r
  @g = g
  @b = b
end

Instance Attribute Details

#bObject

Returns the value of attribute b.



20
21
22
# File 'lib/ra/color.rb', line 20

def b
  @b
end

#gObject

Returns the value of attribute g.



20
21
22
# File 'lib/ra/color.rb', line 20

def g
  @g
end

#rObject

Returns the value of attribute r.



20
21
22
# File 'lib/ra/color.rb', line 20

def r
  @r
end

Class Method Details

.[](value) ⇒ Ra::Color

Parameters:

  • value (Array<Numeric,Numeric,Numeric>)

Returns:



26
27
28
29
30
31
32
# File 'lib/ra/color.rb', line 26

def self.[](value)
  new(
    r: value[0],
    g: value[1],
    b: value[2],
  )
end

.blackRa::Color

Returns:



58
59
60
# File 'lib/ra/color.rb', line 58

def self.black
  @black ||= uniform(0.0)
end

.hex(value) ⇒ Ra::Color

Parameters:

  • value (String)

    e.g. “#336699”

Returns:



36
37
38
39
40
41
42
43
44
# File 'lib/ra/color.rb', line 36

def self.hex(value)
  r, g, b = value.match(/^#(..)(..)(..)$/).captures.map(&:hex)

  new(
    r: Float(r) / PRECISION,
    g: Float(g) / PRECISION,
    b: Float(b) / PRECISION,
  )
end

.uniform(value) ⇒ Ra::Color

Parameters:

  • value (Numeric)

    between 0.0 and 1.0

Returns:



48
49
50
# File 'lib/ra/color.rb', line 48

def self.uniform(value)
  new(r: value, g: value, b: value)
end

.whiteRa::Color

Returns:



53
54
55
# File 'lib/ra/color.rb', line 53

def self.white
  @white ||= uniform(1.0)
end

Instance Method Details

#*(other) ⇒ Ra::Color

Parameters:

Returns:



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ra/color.rb', line 99

def *(other)
  is_color = other.is_a?(self.class)
  other_r = is_color ? other.r : other
  other_g = is_color ? other.g : other
  other_b = is_color ? other.b : other

  self.class.new(
    r: r * other_r,
    g: g * other_g,
    b: b * other_b,
  )
end

#+(other) ⇒ Ra::Color

Combine the r / g / b components (+). If other is nil return self.

Parameters:

Returns:



81
82
83
84
85
# File 'lib/ra/color.rb', line 81

def +(other)
  return self if other.nil?

  self.class.new(r: r + other.r, g: g + other.g, b: b + other.b)
end

#-(other) ⇒ Ra::Color

Combine the r / g / b components (-). If other is nil return self.

Parameters:

Returns:



91
92
93
94
95
# File 'lib/ra/color.rb', line 91

def -(other)
  return self if other.nil?

  self.class.new(r: r - other.r, g: g - other.g, b: b - other.b)
end

#/(other) ⇒ Ra::Color

Parameters:

Returns:



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/ra/color.rb', line 114

def /(other)
  is_color = other.is_a?(self.class)
  other_r = is_color ? other.r : other
  other_g = is_color ? other.g : other
  other_b = is_color ? other.b : other

  self.class.new(
    r: r / other_r,
    g: g / other_g,
    b: b / other_b,
  )
end

#==(other) ⇒ Boolean

Returns:

  • (Boolean)


128
129
130
# File 'lib/ra/color.rb', line 128

def ==(other)
  r_val == other.r_val && g_val == other.g_val && b_val == other.b_val
end

#ppm(precision: PRECISION) ⇒ Integer

Parameters:

  • precision (Integer) (defaults to: PRECISION)

Returns:

  • (Integer)


73
74
75
# File 'lib/ra/color.rb', line 73

def ppm(precision: PRECISION)
  "#{r_val(precision:)} #{g_val(precision:)} #{b_val(precision:)}"
end