class WSLight::Color

Handles the red/green/blue value of a color

Constants

COLORS

Attributes

b[RW]
g[RW]
r[RW]

Public Class Methods

by_name(name) click to toggle source
# 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
new(r=0, g=0, b=0) click to toggle source
# 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
random() click to toggle source
# File lib/ws_light/color.rb, line 45
def self.random
  Color.new(rand(192), rand(192), rand(192))
end
random_from_set() click to toggle source
# File lib/ws_light/color.rb, line 60
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

Public Instance Methods

mix(other, ratio) click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/ws_light/color.rb, line 26
def to_a
  [@b, @g, @r]
end