1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
| return {
on = {
timer = {
"on 06/12-31/12 every 5 minutes between civiltwilightend and 23:59"
}
},
data = {},
logging = {},
execute = function(domoticz, triggeredItem)
local kleuren = { {r=255, g=0, b=0}, {r=255, g=128, b=0}, {r=255, g=255, b=0}, {r=128, g=255, b=0}, {r=0, g=255, b=0}, {r=0, g=255, b=128}, {r=0, g=255, b=255}, {r=0, g=128, b=255}, {r=0, g=0, b=255}, {r=128, g=0, b=255}, {r=255, g=0, b=255}, {r=255, g=0, b=128} }
local devices = { 94, 95, 99 }
local frames = 60;
local function hslToRgb(h, s, l)
h = h / 360
if s == 0 then
l = math.floor((l * 255) + 0.5)
return l, l, l
end
local function to(p, q, t)
if t < 0 then
t = t + 1
end
if t > 1 then
t = t - 1
end
if t < .16667 then
return p + (q - p) * 6 * t
end
if t < .5 then
return q
end
if t < .66667 then
return p + (q - p) * (.66667 - t) * 6
end
return p
end
local q = l < .5 and l * (1 + s) or l + s - l * s
local p = 2 * l - q
return math.floor((to(p, q, h + .33334) * 255) + 0.5), math.floor((to(p, q, h) * 255) + 0.5), math.floor((to(p, q, h - .33334) * 255) + 0.5)
end
local function rgbToHsl(r, g, b)
r = r / 255
g = g / 255
b = b / 255
local max = math.max(r, g, b)
local min = math.min(r, g, b)
local h = (max + min) / 2;
local s, l = h, h
if (max == min) then
h = 0
s = 0 -- achromatic
else
local d = max - min;
if (l > 0.5) then
s = d / (2 - max - min)
else
s = d / (max + min)
end
if (max == r and g < b) then
h = (g - b) / d + 6
elseif (max == r and g >= b) then
h = (g - b) / d
elseif (max == g) then
h = (b - r) / d + 2
elseif (max == b) then
h = (r - g) / d + 4
end
h = h / 6;
end
return (h * 360), s, l;
end
for deviceIndex = 1, #devices do
local rgbDevice = domoticz.devices(devices[deviceIndex])
if (rgbDevice.active == true) then
-- Just a simple rand would somehow always give device #2 & #3 the same color; So enjoy multiple math randoms for each device!
math.randomseed( os.time() * deviceIndex )
local randKleurId = math.random(1,#kleuren)
for frame = 1, deviceIndex do
randKleurId = math.random(1,#kleuren)
end
local currentColor = rgbDevice.getColor()
local currentColorHlsH, currentColorHlsS, currentColorHlsL = rgbToHsl(currentColor.r, currentColor.g, currentColor.b)
local newColor = kleuren[randKleurId]
local newColorHlsH, newColorHlsS, newColorHlsL = rgbToHsl(newColor.r, newColor.g, newColor.b)
-- Decide to turn Hls clockwise or anti-clockwise (and speed)
local hInc = 0;
if ((currentColorHlsH == newColorHlsH or math.abs(currentColorHlsH - newColorHlsH) == 180) and math.random(1,2) == 2) then
hInc = 180 / frames
elseif (currentColorHlsH == newColorHlsH or math.abs(currentColorHlsH - newColorHlsH) == 180) then
hInc = -180 / frames
elseif ((currentColorHlsH + 180 > newColorHlsH and currentColorHlsH < newColorHlsH) or (currentColorHlsH + 180 > 360 and currentColorHlsH - 180 > newColorHlsH)) then
if (currentColorHlsH > newColorHlsH) then
hInc = ((newColorHlsH + 360) - currentColorHlsH) / frames
else
hInc = (newColorHlsH - currentColorHlsH) / frames
end
else
if (currentColorHlsH > newColorHlsH) then
hInc = (newColorHlsH - currentColorHlsH) / frames
else
hInc = (newColorHlsH - (currentColorHlsH + 360)) / frames
end
end
-- Special random full circle to animate from a color to the same color
if (currentColorHlsH == newColorHlsH) then
hInc = hInc * 2
end
if (hInc ~= 0) then
for frame = 1, (frames - 1) do
-- calculate frame hls values
local frameHlsH = currentColorHlsH + (hInc * frame)
if (frameHlsH < 0) then
frameHlsH = frameHlsH + 360
end
if (frameHlsH >= 360) then
frameHlsH = frameHlsH - 360
end
local frameHlsS = currentColorHlsS
if (currentColorHlsS ~= newColorHlsS) then
frameHlsS = (currentColorHlsS - (currentColorHlsS - newColorHlsS) / frames * frame);
end
local frameHlsL = currentColorHlsL
if (currentColorHlsL ~= newColorHlsL) then
frameHlsL = (currentColorHlsL - (currentColorHlsL - newColorHlsL) / frames * frame);
end
-- Calculate frame rgb values
local frameRgbR, frameRgbG, frameRgbB = hslToRgb(frameHlsH, frameHlsS, frameHlsL)
local secs = frame - 1;
if (secs == 0) then
rgbDevice.setColor(frameRgbR, frameRgbG, frameRgbB, rgbDevice.level)
else
rgbDevice.setColor(frameRgbR, frameRgbG, frameRgbB, rgbDevice.level).afterSec(secs)
end
end
-- At end of animation set final select color
rgbDevice.setColor(newColor.r, newColor.g, newColor.b, rgbDevice.level).afterSec((frames - 1))
end
end
end
end
} |