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"
Instance Attribute Summary collapse
-
#b ⇒ Object
Returns the value of attribute b.
-
#g ⇒ Object
Returns the value of attribute g.
-
#r ⇒ Object
Returns the value of attribute r.
Class Method Summary collapse
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: DEFAULT_PRECISION) ⇒ Integer
Constructor Details
#initialize(r: 0.0, g: 0.0, b: 0.0) ⇒ Color
Returns a new instance of Color.
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
#b ⇒ Object
Returns the value of attribute b.
20 21 22 |
# File 'lib/ra/color.rb', line 20 def b @b end |
#g ⇒ Object
Returns the value of attribute g.
20 21 22 |
# File 'lib/ra/color.rb', line 20 def g @g end |
#r ⇒ Object
Returns the value of attribute r.
20 21 22 |
# File 'lib/ra/color.rb', line 20 def r @r end |
Class Method Details
.black ⇒ Ra::Color
49 50 51 |
# File 'lib/ra/color.rb', line 49 def self.black @black ||= uniform(0.0) end |
.hex(value) ⇒ Ra::Color
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
39 40 41 |
# File 'lib/ra/color.rb', line 39 def self.uniform(value) new(r: value, g: value, b: value) end |
.white ⇒ Ra::Color
44 45 46 |
# File 'lib/ra/color.rb', line 44 def self.white @white ||= uniform(1.0) end |
Instance Method Details
#*(other) ⇒ Ra::Color
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.
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.
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
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
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
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 |