Class: WSLight::Color

Inherits:
Object
  • Object
show all
Defined in:
lib/ws_light/color.rb

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}
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(r = 0, g = 0, b = 0) ⇒ Color

Returns a new instance of Color.



16
17
18
19
20
21
22
23
# File 'lib/ws_light/color.rb', line 16

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

#bObject

Returns the value of attribute b.



3
4
5
# File 'lib/ws_light/color.rb', line 3

def b
  @b
end

#gObject

Returns the value of attribute g.



3
4
5
# File 'lib/ws_light/color.rb', line 3

def g
  @g
end

#rObject

Returns the value of attribute r.



3
4
5
# File 'lib/ws_light/color.rb', line 3

def r
  @r
end

Class Method Details

.by_name(name) ⇒ Object



48
49
50
51
52
53
54
55
56
57
# File 'lib/ws_light/color.rb', line 48

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
    fail "Cannot find color #{name}"
  end
end

.randomObject



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

def self.random
  Color.new(rand(192), rand(192), rand(192))
end

.random_from_setObject



59
60
61
62
63
# File 'lib/ws_light/color.rb', line 59

def self.random_from_set
  color_values = Color::COLORS.values
  selected_color = color_values[rand(color_values.length)]
  Color.new(selected_color[:r], selected_color[:g], selected_color[:b])
end

Instance Method Details

#mix(other, ratio) ⇒ Object



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

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



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

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_aObject



25
26
27
# File 'lib/ws_light/color.rb', line 25

def to_a
  [@b, @g, @r]
end