Class: LIFX::LAN::Color

Inherits:
Struct
  • Object
show all
Extended by:
Colors
Defined in:
lib/lifx/lan/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 =
9000
DEFAULT_SIMILAR_THRESHOLD =

0.1% variance

0.001

Constants included from Colors

LIFX::LAN::Colors::DEFAULT_KELVIN

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Colors

random_color, white

Constructor Details

#initialize(hue, saturation, brightness, kelvin) ⇒ Color

Returns a new instance of Color.



127
128
129
130
# File 'lib/lifx/lan/color.rb', line 127

def initialize(hue, saturation, brightness, kelvin)
  hue = hue % 360
  super(hue, saturation, brightness, kelvin)
end

Instance Attribute Details

#brightnessObject

Returns the value of attribute brightness

Returns:

  • (Object)

    the current value of brightness



37
38
39
# File 'lib/lifx/lan/color.rb', line 37

def brightness
  @brightness
end

#hueObject

Returns the value of attribute hue

Returns:

  • (Object)

    the current value of hue



37
38
39
# File 'lib/lifx/lan/color.rb', line 37

def hue
  @hue
end

#kelvinObject

Returns the value of attribute kelvin

Returns:

  • (Object)

    the current value of kelvin



37
38
39
# File 'lib/lifx/lan/color.rb', line 37

def kelvin
  @kelvin
end

#saturationObject

Returns the value of attribute saturation

Returns:

  • (Object)

    the current value of saturation



37
38
39
# File 'lib/lifx/lan/color.rb', line 37

def saturation
  @saturation
end

Class Method Details

.hsb(hue, saturation, brightness) ⇒ Color Also known as: hsv

Helper method to create from HSB/HSV

Parameters:

  • hue (Float)

    Valid range: 0..360

  • saturation (Float)

    Valid range: 0..1

  • brightness (Float)

    Valid range: 0..1

Returns:



49
50
51
# File 'lib/lifx/lan/color.rb', line 49

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

Parameters:

  • hue (Float)

    Valid range: 0..360

  • saturation (Float)

    Valid range: 0..1

  • brightness (Float)

    Valid range: 0..1

  • kelvin (Integer)

    Valid range: 2500..9000

Returns:



60
61
62
# File 'lib/lifx/lan/color.rb', line 60

def hsbk(hue, saturation, brightness, kelvin)
  new(hue, saturation, brightness, kelvin)
end

.hsl(hue, saturation, luminance) ⇒ Color

Helper method to create from HSL

Parameters:

  • hue (Float)

    Valid range: 0..360

  • saturation (Float)

    Valid range: 0..1

  • luminance (Float)

    Valid range: 0..1

Returns:



69
70
71
72
73
74
75
76
# File 'lib/lifx/lan/color.rb', line 69

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

Note:

RGB is not the recommended way to create colors

Helper method to create from RGB.

Parameters:

  • r (Integer)

    Red. Valid range: 0..255

  • g (Integer)

    Green. Valid range: 0..255

  • b (Integer)

    Blue. Valid range: 0..255

Returns:



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
111
# File 'lib/lifx/lan/color.rb', line 84

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

#similar_to?(other, threshold: DEFAULT_SIMILAR_THRESHOLD) ⇒ Boolean

Checks if colours are equal to 0.1% variance

Parameters:

  • other (Color)

    Color to compare to

  • threshold: (Float) (defaults to: DEFAULT_SIMILAR_THRESHOLD)

    0..1. Threshold to consider it similar

Returns:

  • (Boolean)


183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/lifx/lan/color.rb', line 183

def similar_to?(other, threshold: DEFAULT_SIMILAR_THRESHOLD)
  return false unless other.is_a?(Color)
  conditions = []

  conditions << (((hue - other.hue).abs < (threshold * 360)) || begin
    # FIXME: Surely there's a better way.
    hues = [hue, other.hue].sort
    hues[0] += 360
    (hues[0] - hues[1]).abs < (threshold * 360)
  end)
  conditions << ((saturation - other.saturation).abs < threshold)
  conditions << ((brightness - other.brightness).abs < threshold)
  conditions.all?
end

#to_aArray<Float, Float, Float, Integer>

Returns hue, saturation, brightness and kelvin in an array

Returns:

  • (Array<Float, Float, Float, Integer>)


174
175
176
# File 'lib/lifx/lan/color.rb', line 174

def to_a
  [hue, saturation, brightness, kelvin]
end

#with_brightness(brightness) ⇒ Color

Returns a new Color with the brightness changed while keeping other attributes

Parameters:

  • brightness (Float)

    Brightness as float. 0..1

Returns:



149
150
151
# File 'lib/lifx/lan/color.rb', line 149

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

Parameters:

  • hue (Float)

    Hue in degrees. 0..360

Returns:



135
136
137
# File 'lib/lifx/lan/color.rb', line 135

def with_hue(hue)
  Color.new(hue, saturation, brightness, kelvin)
end

#with_kelvin(kelvin) ⇒ Color

Returns a new Color with the kelvin changed while keeping other attributes

Parameters:

  • kelvin (Integer)

    Kelvin. 2500..9000

Returns:



156
157
158
# File 'lib/lifx/lan/color.rb', line 156

def with_kelvin(kelvin)
  Color.new(hue, saturation, brightness, kelvin)
end

#with_saturation(saturation) ⇒ Color

Returns a new Color with the saturaiton changed while keeping other attributes

Parameters:

  • saturaiton (Float)

    Saturation as float. 0..1

Returns:



142
143
144
# File 'lib/lifx/lan/color.rb', line 142

def with_saturation(saturation)
  Color.new(hue, saturation, brightness, kelvin)
end