From 313c3aa1e5d27c358e6364059f6980467c46d2ba Mon Sep 17 00:00:00 2001 From: Sam Soffes Date: Wed, 3 Jan 2024 09:22:23 -0800 Subject: [PATCH] Move to editable state --- lib/hue/editable_state.rb | 28 ++++++++++++++++++++++++++++ lib/hue/group.rb | 4 ++-- lib/hue/light.rb | 27 --------------------------- 3 files changed, 30 insertions(+), 29 deletions(-) diff --git a/lib/hue/editable_state.rb b/lib/hue/editable_state.rb index 8d720350..3da73a63 100644 --- a/lib/hue/editable_state.rb +++ b/lib/hue/editable_state.rb @@ -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 @@ -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 diff --git a/lib/hue/group.rb b/lib/hue/group.rb index 07b428b9..0ea4dc59 100644 --- a/lib/hue/group.rb +++ b/lib/hue/group.rb @@ -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. diff --git a/lib/hue/light.rb b/lib/hue/light.rb index 690379a6..4980aee4 100644 --- a/lib/hue/light.rb +++ b/lib/hue/light.rb @@ -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. @@ -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 = {