Class: Hue::Colors::RGB

Inherits:
Color
  • Object
show all
Defined in:
lib/hue/colors/rgb.rb

Constant Summary collapse

MIN =
0
MAX =
255

Constants inherited from Color

Color::ERROR_METHOD_NOT_IMPLEMENTED

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*rgb) ⇒ RGB

Returns a new instance of RGB.



18
19
20
21
22
23
# File 'lib/hue/colors/rgb.rb', line 18

def initialize(*rgb)
  red, green, blue = rgb
  self.red = red
  self.green = green
  self.blue = blue
end

Instance Attribute Details

#blueObject

Returns the value of attribute blue.



16
17
18
# File 'lib/hue/colors/rgb.rb', line 16

def blue
  @blue
end

#greenObject

Returns the value of attribute green.



16
17
18
# File 'lib/hue/colors/rgb.rb', line 16

def green
  @green
end

#redObject

Returns the value of attribute red.



16
17
18
# File 'lib/hue/colors/rgb.rb', line 16

def red
  @red
end

Class Method Details

.ranged(value) ⇒ Object



10
11
12
# File 'lib/hue/colors/rgb.rb', line 10

def self.ranged(value)
  super(MIN, value, MAX)
end

Instance Method Details

#==(rhs) ⇒ Object



71
72
73
74
# File 'lib/hue/colors/rgb.rb', line 71

def ==(rhs)
  rhs.is_a?(RGB) &&
    [:red, :green, :blue].all? { |m| self.send(m) == rhs.send(m) }
end

#to_hashObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/hue/colors/rgb.rb', line 37

def to_hash
  max = MAX.to_f
  red, green, blue = self.red / max, self.green / max, self.red / max

  max = [red, green, blue].max
  min = [red, green, blue].min
  h, s, l = 0, 0, ((max + min) / 2 * 255)

  d = max - min
  s = max == 0 ? 0 : (d / max * 255)

  h = case max
      when min
        0 # monochromatic
      when red
        (green - blue) / d + (green < blue ? 6 : 0)
      when green
        (blue - red) / d + 2
      when blue
        (red - green) / d + 4
      end * 60  # / 6 * 360

  h = (h * HueSaturation::HUE_SCALE).to_i
  {hue: h, sat: s.to_i, bri: Bulb::BRIGHTNESS_MAX}
end

#to_rgbObject



67
68
69
# File 'lib/hue/colors/rgb.rb', line 67

def to_rgb
  self
end

#to_sObject



63
64
65
# File 'lib/hue/colors/rgb.rb', line 63

def to_s
  "RGB≈#{rgb}"
end