Skip to content

Commit

Permalink
Move to editable state
Browse files Browse the repository at this point in the history
  • Loading branch information
soffes committed Jan 3, 2024
1 parent e35ffbd commit 313c3aa
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 29 deletions.
28 changes: 28 additions & 0 deletions lib/hue/editable_state.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
require 'color_conversion'

module Hue
module EditableState
HUE_RANGE = 0..65535
SATURATION_RANGE = 0..254
BRIGHTNESS_RANGE = 0..254

def on?
@state['on']
end
Expand All @@ -23,5 +29,27 @@ def set_xy(x, y)
set_state({:xy => [x, y]})
@x, @y = x, y
end

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

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
end
end
4 changes: 2 additions & 2 deletions lib/hue/group.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ class Group
# Both 0 and 65535 are red, 25500 is green and 46920 is blue.
attr_accessor :hue

# Saturation of the group. 255 is the most saturated (colored)
# Saturation of the group. 254 is the most saturated (colored)
# and 0 is the least saturated (white).
attr_accessor :saturation

# Brightness of the group. This is a scale from the minimum
# brightness the group is capable of, 0, to the maximum capable
# brightness, 255. Note a brightness of 0 is not off.
# brightness, 254. Note a brightness of 0 is not off.
attr_accessor :brightness

# The x coordinate of a color in CIE color space. Between 0 and 1.
Expand Down
27 changes: 0 additions & 27 deletions lib/hue/light.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
require 'color_conversion'

module Hue
class Light
include TranslateKeys
include EditableState

HUE_RANGE = 0..65535
SATURATION_RANGE = 0..255
BRIGHTNESS_RANGE = 0..255
COLOR_TEMPERATURE_RANGE = 153..500

# Unique identification number.
Expand Down Expand Up @@ -142,28 +137,6 @@ def refresh
unpack(json)
end

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

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) * 65535.0).to_i,
saturation: ((hsb[:s].to_f / 100.0) * 254.0).to_i,
brightness: ((hsb[:b].to_f / 100.0) * 254.0).to_i
}

set_state(state)

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

private

KEYS_MAP = {
Expand Down

0 comments on commit 313c3aa

Please sign in to comment.