Class: Ra::Color
- Inherits:
-
Object
- Object
- Ra::Color
- 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
- .[](value) ⇒ Ra::Color
- .black ⇒ Ra::Color
- .hex(value) ⇒ Ra::Color
- .uniform(value) ⇒ Ra::Color
- .white ⇒ Ra::Color
Instance Method Summary collapse
- #*(other) ⇒ Ra::Color
-
#+(other) ⇒ Ra::Color
Combine the r / g / b components (+).
-
#-(other) ⇒ Ra::Color
Combine the r / g / b components (-).
- #/(other) ⇒ Ra::Color
- #==(other) ⇒ Boolean
-
#initialize(r: 0.0, g: 0.0, b: 0.0) ⇒ Color
constructor
A new instance of Color.
- #ppm(precision: PRECISION) ⇒ Integer
Constructor Details
#initialize(r: 0.0, g: 0.0, b: 0.0) ⇒ Color
Returns a new instance of Color.
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
#b ⇒ Numeric
30 31 32 |
# File 'lib/ra/color.rb', line 30 def b @b end |
#g ⇒ Numeric
26 27 28 |
# File 'lib/ra/color.rb', line 26 def g @g end |
#r ⇒ Numeric
22 23 24 |
# File 'lib/ra/color.rb', line 22 def r @r end |
Class Method Details
.[](value) ⇒ Ra::Color
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 |
.black ⇒ Ra::Color
68 69 70 |
# File 'lib/ra/color.rb', line 68 def self.black @black ||= uniform(0.0) end |
.hex(value) ⇒ Ra::Color
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
58 59 60 |
# File 'lib/ra/color.rb', line 58 def self.uniform(value) new(r: value, g: value, b: value) end |
.white ⇒ Ra::Color
63 64 65 |
# File 'lib/ra/color.rb', line 63 def self.white @white ||= uniform(1.0) end |
Instance Method Details
#*(other) ⇒ Ra::Color
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`.
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`.
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
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
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
83 84 85 |
# File 'lib/ra/color.rb', line 83 def ppm(precision: PRECISION) "#{r_val(precision:)} #{g_val(precision:)} #{b_val(precision:)}" end |