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"

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



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

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

.blackRa::Color

Returns:



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

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

.hex(value) ⇒ Ra::Color

Parameters:

  • value (String)

    e.g. “#336699”

Returns:



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

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

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

.uniform(value) ⇒ Ra::Color

Parameters:

  • value (Numeric)

    between 0.0 and 1.0

Returns:



39
40
41
# File 'lib/ra/color.rb', line 39

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

.whiteRa::Color

Returns:



44
45
46
# File 'lib/ra/color.rb', line 44

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

Instance Method Details

#*(other) ⇒ Ra::Color

Parameters:

Returns:



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ra/color.rb', line 90

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:



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

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:



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

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:



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/ra/color.rb', line 105

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)


119
120
121
# File 'lib/ra/color.rb', line 119

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

#ppm(precision: DEFAULT_PRECISION) ⇒ Integer

Parameters:

  • precision (Integer) (defaults to: DEFAULT_PRECISION)

Returns:

  • (Integer)


64
65
66
# File 'lib/ra/color.rb', line 64

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