Class: WSLight::Color
- Inherits:
-
Object
- Object
- WSLight::Color
- Defined in:
- lib/ws_light/color.rb
Overview
Handles the red/green/blue value of a color
Constant Summary collapse
- COLORS =
{ pink: { r: 255, g: 16, b: 32 }, red: { r: 255, g: 0, b: 0 }, blue: { r: 0, g: 0, b: 255 }, green: { r: 0, g: 255, b: 0 }, cyan: { r: 0, g: 127, b: 127 }, orange: { r: 255, g: 70, b: 0 }, yellow: { r: 255, g: 220, b: 0 }, purple: { r: 80, g: 0, b: 180 } }.freeze
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
-
#initialize(r = 0, g = 0, b = 0) ⇒ Color
constructor
A new instance of Color.
- #mix(other, ratio) ⇒ Object
- #mix!(other, ratio) ⇒ Object
- #to_a ⇒ Object
Constructor Details
#initialize(r = 0, g = 0, b = 0) ⇒ Color
Returns a new instance of Color.
17 18 19 20 21 22 23 24 |
# File 'lib/ws_light/color.rb', line 17 def initialize(r=0, g=0, b=0) @r = r > 255 ? 255 : r.to_i @g = g > 255 ? 255 : g.to_i @b = b > 255 ? 255 : b.to_i @r = @r < 0 ? 0 : @r @g = @g < 0 ? 0 : @g @b = @b < 0 ? 0 : @b end |
Instance Attribute Details
#b ⇒ Object
Returns the value of attribute b.
4 5 6 |
# File 'lib/ws_light/color.rb', line 4 def b @b end |
#g ⇒ Object
Returns the value of attribute g.
4 5 6 |
# File 'lib/ws_light/color.rb', line 4 def g @g end |
#r ⇒ Object
Returns the value of attribute r.
4 5 6 |
# File 'lib/ws_light/color.rb', line 4 def r @r end |
Class Method Details
.by_name(name) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/ws_light/color.rb', line 49 def self.by_name(name) if Color::COLORS[name] selected_color = Color::COLORS[name] Color.new(selected_color[:r], selected_color[:g], selected_color[:b]) elsif name == :black Color.new(0, 0, 0) else raise "Cannot find color #{name}" end end |
Instance Method Details
#mix(other, ratio) ⇒ Object
30 31 32 33 34 35 36 |
# File 'lib/ws_light/color.rb', line 30 def mix(other, ratio) Color.new( (@r * (1 - ratio) + other.r * ratio).to_i, (@g * (1 - ratio) + other.g * ratio).to_i, (@b * (1 - ratio) + other.b * ratio).to_i ) end |
#mix!(other, ratio) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ws_light/color.rb', line 38 def mix!(other, ratio) @r = (@r * (1 - ratio) + other.r * ratio).to_i @g = (@g * (1 - ratio) + other.g * ratio).to_i @b = (@b * (1 - ratio) + other.b * ratio).to_i self end |
#to_a ⇒ Object
26 27 28 |
# File 'lib/ws_light/color.rb', line 26 def to_a [@b, @g, @r] end |