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



75
76
77
78
79
# File 'lib/ra/color.rb', line 75

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

Instance Attribute Details

#bNumeric

Returns:

  • (Numeric)


30
31
32
# File 'lib/ra/color.rb', line 30

def b
  @b
end

#gNumeric

Returns:

  • (Numeric)


26
27
28
# File 'lib/ra/color.rb', line 26

def g
  @g
end

#rNumeric

Returns:

  • (Numeric)


22
23
24
# File 'lib/ra/color.rb', line 22

def r
  @r
end

Class Method Details

.[](value) ⇒ Ra::Color

Parameters:

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

Returns:



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

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

.blackRa::Color

Returns:



68
69
70
# File 'lib/ra/color.rb', line 68

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

.hex(value) ⇒ Ra::Color

Parameters:

  • value (String)

    e.g. “#336699”

Returns:



46
47
48
49
50
51
52
53
54
# File 'lib/ra/color.rb', line 46

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:



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

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

.whiteRa::Color

Returns:



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

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

Instance Method Details

#*(other) ⇒ Ra::Color

Parameters:

Returns:



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/ra/color.rb', line 109

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:



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

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

Parameters:

Returns:



101
102
103
104
105
# File 'lib/ra/color.rb', line 101

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:



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/ra/color.rb', line 124

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)


138
139
140
# File 'lib/ra/color.rb', line 138

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)


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

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