Module: Hue::EditableState

Included in:
Group, Light
Defined in:
lib/hue/editable_state.rb

Constant Summary collapse

HUE_RANGE =
0..65535
SATURATION_RANGE =
0..254
BRIGHTNESS_RANGE =
0..254

Instance Method Summary collapse

Instance Method Details

#hexObject



42
43
44
# File 'lib/hue/editable_state.rb', line 42

def hex
  ColorConversion::Color.new(h: hue, s: saturation, b: brightness).hex
end

#hex=(hex) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/hue/editable_state.rb', line 46

def hex=(hex)
  hex = "##{hex}" unless hex.start_with?("#")
  hsb = ColorConversion::Color.new(hex).hsb

  # Map values from standard HSB to what Hue wants and update state
  state = {
    hue: ((hsb[:h].to_f / 360.0) * HUE_RANGE.last.to_f).to_i,
    saturation: ((hsb[:s].to_f / 100.0) * SATURATION_RANGE.last.to_f).to_i,
    brightness: ((hsb[:b].to_f / 100.0) * BRIGHTNESS_RANGE.last.to_f).to_i
  }

  set_state(state)

  @hue = state[:hue]
  @saturation = state[:saturation]
  @brightness = state[:brightness]
end

#off!Object



17
18
19
# File 'lib/hue/editable_state.rb', line 17

def off!
  self.on = false
end

#on!Object



13
14
15
# File 'lib/hue/editable_state.rb', line 13

def on!
  self.on = true
end

#on?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/hue/editable_state.rb', line 9

def on?
  @on || false
end

#set_xy(x, y) ⇒ Object



37
38
39
40
# File 'lib/hue/editable_state.rb', line 37

def set_xy(x, y)
  set_state({xy: [x, y]})
  @x, @y = x, y
end

#toggle!Object

Turn the light on if it’s off and vice versa



22
23
24
25
26
27
28
# File 'lib/hue/editable_state.rb', line 22

def toggle!
  if @on
    off!
  else
    on!
  end
end