Class: LIFX::Color
- Inherits:
-
Struct
- Object
- Struct
- LIFX::Color
- Extended by:
- Colors
- Defined in:
- lib/lifx/color.rb
Overview
LIFX::Color represents a color intervally by HSBK (Hue, Saturation, Brightness/Value, Kelvin). It has methods to construct a LIFX::Color instance from various color representations.
Constant Summary collapse
- UINT16_MAX =
65535
- KELVIN_MIN =
2500
- KELVIN_MAX =
10000
- EQUALITY_THRESHOLD =
0.1% variance
0.001
Constants included from Colors
Instance Attribute Summary collapse
-
#brightness ⇒ Object
Returns the value of attribute brightness.
-
#hue ⇒ Object
Returns the value of attribute hue.
-
#kelvin ⇒ Object
Returns the value of attribute kelvin.
-
#saturation ⇒ Object
Returns the value of attribute saturation.
Class Method Summary collapse
-
.from_struct(hsbk) ⇒ Color
private
Creates an instance from a Protocol::Light::Hsbk struct.
-
.hsb(hue, saturation, brightness) ⇒ Color
(also: hsv)
Helper method to create from HSB/HSV.
-
.hsbk(hue, saturation, brightness, kelvin) ⇒ Color
Helper method to create from HSBK/HSVK.
-
.hsl(hue, saturation, luminance) ⇒ Color
Helper method to create from HSL.
-
.rgb(r, g, b) ⇒ Color
Helper method to create from RGB.
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Checks if colours are equal to 0.1% variance.
-
#initialize(hue, saturation, brightness, kelvin) ⇒ Color
constructor
A new instance of Color.
-
#to_a ⇒ Array<Float, Float, Float, Integer>
Returns hue, saturation, brightness and kelvin in an array.
-
#to_hsbk ⇒ Protocol::Light::Hsbk
private
Returns a struct for use by the protocol.
-
#with_brightness(brightness) ⇒ Color
Returns a new Color with the brightness changed while keeping other attributes.
-
#with_hue(hue) ⇒ Color
Returns a new Color with the hue changed while keeping other attributes.
-
#with_kelvin(kelvin) ⇒ Color
Returns a new Color with the kelvin changed while keeping other attributes.
-
#with_saturation(saturation) ⇒ Color
Returns a new Color with the saturaiton changed while keeping other attributes.
Methods included from Colors
Constructor Details
#initialize(hue, saturation, brightness, kelvin) ⇒ Color
Returns a new instance of Color.
126 127 128 129 |
# File 'lib/lifx/color.rb', line 126 def initialize(hue, saturation, brightness, kelvin) hue = hue % 360 super(hue, saturation, brightness, kelvin) end |
Instance Attribute Details
#brightness ⇒ Object
Returns the value of attribute brightness
36 37 38 |
# File 'lib/lifx/color.rb', line 36 def brightness @brightness end |
#hue ⇒ Object
Returns the value of attribute hue
36 37 38 |
# File 'lib/lifx/color.rb', line 36 def hue @hue end |
#kelvin ⇒ Object
Returns the value of attribute kelvin
36 37 38 |
# File 'lib/lifx/color.rb', line 36 def kelvin @kelvin end |
#saturation ⇒ Object
Returns the value of attribute saturation
36 37 38 |
# File 'lib/lifx/color.rb', line 36 def saturation @saturation end |
Class Method Details
.from_struct(hsbk) ⇒ Color
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Creates an instance from a Protocol::Light::Hsbk struct
116 117 118 119 120 121 122 123 |
# File 'lib/lifx/color.rb', line 116 def from_struct(hsbk) new( (hsbk.hue.to_f / UINT16_MAX) * 360, (hsbk.saturation.to_f / UINT16_MAX), (hsbk.brightness.to_f / UINT16_MAX), hsbk.kelvin ) end |
.hsb(hue, saturation, brightness) ⇒ Color Also known as: hsv
Helper method to create from HSB/HSV
48 49 50 |
# File 'lib/lifx/color.rb', line 48 def hsb(hue, saturation, brightness) new(hue, saturation, brightness, DEFAULT_KELVIN) end |
.hsbk(hue, saturation, brightness, kelvin) ⇒ Color
Helper method to create from HSBK/HSVK
59 60 61 |
# File 'lib/lifx/color.rb', line 59 def hsbk(hue, saturation, brightness, kelvin) new(hue, saturation, brightness, kelvin) end |
.hsl(hue, saturation, luminance) ⇒ Color
Helper method to create from HSL
68 69 70 71 72 73 74 75 |
# File 'lib/lifx/color.rb', line 68 def hsl(hue, saturation, luminance) # From: http://ariya.blogspot.com.au/2008/07/converting-between-hsl-and-hsv.html l = luminance * 2 saturation *= (l <= 1) ? l : 2 - l brightness = (l + saturation) / 2 saturation = (2 * saturation) / (l + saturation) new(hue, saturation, brightness, DEFAULT_KELVIN) end |
.rgb(r, g, b) ⇒ Color
RGB is not the recommended way to create colors
Helper method to create from RGB.
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/lifx/color.rb', line 83 def rgb(r, g, b) r = r / 255.0 g = g / 255.0 b = b / 255.0 max = [r, g, b].max min = [r, g, b].min h = s = v = max d = max - min s = max.zero? ? 0 : d / max if max == min h = 0 else case max when r h = (g - b) / d + (g < b ? 6 : 0) when g h = (b - r) / d + 2 when b h = (r - g) / d + 4 end h = h * 60 end new(h, s, v, DEFAULT_KELVIN) end |
Instance Method Details
#==(other) ⇒ Boolean
Checks if colours are equal to 0.1% variance
181 182 183 184 185 186 187 188 |
# File 'lib/lifx/color.rb', line 181 def ==(other) return false unless other.is_a?(Color) conditions = [] conditions << ((hue - other.hue).abs < (EQUALITY_THRESHOLD * 360)) conditions << ((saturation - other.saturation).abs < EQUALITY_THRESHOLD) conditions << ((brightness - other.brightness).abs < EQUALITY_THRESHOLD) conditions.all? end |
#to_a ⇒ Array<Float, Float, Float, Integer>
Returns hue, saturation, brightness and kelvin in an array
173 174 175 |
# File 'lib/lifx/color.rb', line 173 def to_a [hue, saturation, brightness, kelvin] end |
#to_hsbk ⇒ Protocol::Light::Hsbk
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a struct for use by the protocol
162 163 164 165 166 167 168 169 |
# File 'lib/lifx/color.rb', line 162 def to_hsbk Protocol::Light::Hsbk.new( hue: (hue / 360.0 * UINT16_MAX).to_i, saturation: (saturation * UINT16_MAX).to_i, brightness: (brightness * UINT16_MAX).to_i, kelvin: [KELVIN_MIN, kelvin.to_i, KELVIN_MAX].sort[1] ) end |
#with_brightness(brightness) ⇒ Color
Returns a new Color with the brightness changed while keeping other attributes
148 149 150 |
# File 'lib/lifx/color.rb', line 148 def with_brightness(brightness) Color.new(hue, saturation, brightness, kelvin) end |
#with_hue(hue) ⇒ Color
Returns a new Color with the hue changed while keeping other attributes
134 135 136 |
# File 'lib/lifx/color.rb', line 134 def with_hue(hue) Color.new(hue, saturation, brightness, kelvin) end |