Class: Hue::Colors::ColorTemperature

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

Constant Summary collapse

MEGA =
1e6
KELVIN_MIN =
2000
KELVIN_MAX =
6500
MIRED_MIN =
153
MIRED_MAX =
500

Constants inherited from Color

Hue::Colors::Color::ERROR_METHOD_NOT_IMPLEMENTED

Instance Method Summary collapse

Methods inherited from Color

ranged

Constructor Details

#initialize(temperature) ⇒ ColorTemperature

Returns a new instance of ColorTemperature.



15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/hue/colors/color_temperature.rb', line 15

def initialize(temperature)
  if scale = Hue.percent_to_unit_interval(temperature)
    self.mired = unit_to_mired_interval(scale)
  else
    # Assume an integer value
    temperature = temperature.to_i
    if temperature >= KELVIN_MIN
      self.kelvin = temperature
    else
      self.mired = temperature
    end
  end
end

Instance Method Details

#kelvinObject



37
38
39
# File 'lib/hue/colors/color_temperature.rb', line 37

def kelvin
  ranged(KELVIN_MIN, MEGA / @mired, KELVIN_MAX).round
end

#kelvin=(t) ⇒ Object



41
42
43
# File 'lib/hue/colors/color_temperature.rb', line 41

def kelvin=(t)
  self.mired = (MEGA / ranged(KELVIN_MIN, t.to_f, KELVIN_MAX))
end

#miredObject



29
30
31
# File 'lib/hue/colors/color_temperature.rb', line 29

def mired
  @mired.floor
end

#mired=(t) ⇒ Object



33
34
35
# File 'lib/hue/colors/color_temperature.rb', line 33

def mired=(t)
  @mired = ranged(MIRED_MIN, t.to_f, MIRED_MAX)
end

#to_hashObject



45
46
47
# File 'lib/hue/colors/color_temperature.rb', line 45

def to_hash
  {ct: mired}
end

#to_rgbObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/hue/colors/color_temperature.rb', line 53

def to_rgb
  # using method described at
  # http://www.tannerhelland.com/4435/convert-temperature-rgb-algorithm-code/
  temp = kelvin / 100

  red = temp <= 66 ? 255 : 329.698727446 * ((temp - 60) ** -0.1332047592)

  green = if temp <= 66
            99.4708025861 * Math.log(temp) - 161.1195681661
          else
            288.1221695283 * ((temp - 60) ** -0.0755148492)
          end

  blue = if temp >= 66
           255
         elsif temp <= 19
           0
         else
           138.5177312231 * Math.log(temp - 10) - 305.0447927307
         end

  RGB.new(red, green, blue)
end

#to_sObject



49
50
51
# File 'lib/hue/colors/color_temperature.rb', line 49

def to_s
  "Temperature=#{self.kelvin.to_i}°K (#{self.mired} mired)"
end