summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--deviceHelpers.nim21
-rw-r--r--tradfri.nim8
-rw-r--r--tradfriCli.nim9
-rw-r--r--types.nim8
4 files changed, 35 insertions, 11 deletions
diff --git a/deviceHelpers.nim b/deviceHelpers.nim
index c138d0d..321e185 100644
--- a/deviceHelpers.nim
+++ b/deviceHelpers.nim
@@ -1,3 +1,4 @@
+import colors
import types, helpers, tradfri
proc setPowerState* (device: TradfriDevice, state: bool): bool =
@@ -15,8 +16,6 @@ proc setPowerState* (device: TradfriDevice, state: bool): bool =
proc togglePowerState* (device: TradfriDevice): bool =
- var state: bool
-
if device.`type` == Lightbulb:
return device.operateDevice(TradfriDeviceAction(
kind: LightSetPowerState,
@@ -44,10 +43,26 @@ proc setColorHex* (device: TradfriDevice, color: string): bool =
))
-proc setColorXY* (device: TradfriDevice, colorX: int, colorY: int): bool =
+proc setColorXY* (device: TradfriDevice, colorX: float, colorY: float): bool =
return device.operateDevice(TradfriDeviceAction(
kind: LightSetColorXY,
lightColorX: colorX,
lightColorY: colorY
))
+
+proc setColorXYfromHex* (device: TradfriDevice, color: string): bool =
+ let color = extractRGB(parseColor(color))
+
+ let x = (0.4124 * toFloat(color.r)) + (0.3576 * toFloat(color.g)) + (0.1805 * toFloat(color.b))
+ let y = (0.2126 * toFloat(color.r)) + (0.7152 * toFloat(color.g)) + (0.0722 * toFloat(color.b))
+ let z = (0.0193 * toFloat(color.r)) + (0.1192 * toFloat(color.g)) + (0.9505 * toFloat(color.b))
+
+ let X = (x / (x + y + z))
+ let Y = (y / (x + y + z))
+
+ return device.operateDevice(TradfriDeviceAction(
+ kind: LightSetColorXY,
+ lightColorX: X,
+ lightColorY: Y
+ ))
diff --git a/tradfri.nim b/tradfri.nim
index 0de9208..0d4a9e2 100644
--- a/tradfri.nim
+++ b/tradfri.nim
@@ -52,11 +52,11 @@ proc parseTradfriDevice (data: JsonNode): TradfriDevice =
#get colorX and colorY values (can be used to set any color on RGB bulbs)
if data[DeviceLightbulb][0].hasKey(ParameterColorX):
- state.lightColorX = some(data[DeviceLightbulb][0][ParameterColorX].getInt)
- state.lightColorY = some(data[DeviceLightbulb][0][ParameterColorY].getInt)
+ state.lightColorX = some(data[DeviceLightbulb][0][ParameterColorX].getFloat)
+ state.lightColorY = some(data[DeviceLightbulb][0][ParameterColorY].getFloat)
else:
- state.lightColorX = none(int)
- state.lightColorY = none(int)
+ state.lightColorX = none(float)
+ state.lightColorY = none(float)
#get color-specturm value
diff --git a/tradfriCli.nim b/tradfriCli.nim
index fb39e01..1d52b33 100644
--- a/tradfriCli.nim
+++ b/tradfriCli.nim
@@ -31,6 +31,15 @@ of "toggle":
quit(0)
discard devices[deviceId].togglePowerState()
+
+of "setColor":
+ let deviceId = parseInt(paramStr(2))
+
+ if devices[deviceId].name == "":
+ echo "This device doesn't exist."
+ quit(0)
+
+ discard devices[deviceId].setColorXYfromHex(paramStr(3))
of "devices-json":
let devicesJson = %* devices
diff --git a/types.nim b/types.nim
index eefd104..92dd464 100644
--- a/types.nim
+++ b/types.nim
@@ -43,8 +43,8 @@ type
lightHue*: Option[int]
lightSaturation*: Option[int]
lightColorHex*: Option[string]
- lightColorX*: Option[int]
- lightColorY*: Option[int]
+ lightColorX*: Option[float]
+ lightColorY*: Option[float]
lightColorTemperature*: Option[int]
lightBrightness*: int
@@ -89,8 +89,8 @@ type
lightColorHex*: string
of LightSetColorXY:
- lightColorX*: int
- lightColorY*: int
+ lightColorX*: float
+ lightColorY*: float
of LightSetHueSaturation:
lightHue*: int