-
Notifications
You must be signed in to change notification settings - Fork 0
/
MaxHue.js
371 lines (293 loc) · 7.52 KB
/
MaxHue.js
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
//autowatch = 1;
outlets = 5;
inlets = 14;
var ajaxreq;
var ip = "192.168.1.130";
var key = "z5jjB4qneEEwvocqYO40pLgaN995fdHR4gQkDvyZ" //"Max4LiveMax4Hue"; // username
var bulb_id; // 2
var bri_filter = 100 // 3 (Filter in %)
var tran = 0 // 4
var hard_mode = 0 // 5
var scaler = 1 // 6
var group = 0 // 7
var alerto = 0; //8
var chue = 0; // 9
var slider_hue = 0; //10
var slider_sat = 0; //11
var slider_bri = 0; //12
var ent_mode_on = 0; //13
function msg_int(v){
//assign inlets with if statements
if (!ent_mode_on)
{
if (inlet==0) {
var hue = v*900*scaler;
if(hue > 65280) hue = 65280;
set_light('hue', hue); // tone
CtoRGB(hue);
}
if (inlet==1){
if(hard_mode == 1 && v == 0) return false;
bri = Math.round(((v*2) / 100) * bri_filter);
outlet(0,bri);
set_light('bri',bri); // velocity to bri (42-255)
}
if(inlet==2){
bulb_id = v;
}
if(inlet==3){
// Bri Filter
bri_filter = v;
}
if(inlet==4){
tran = Math.round(Math.exp(0.04246951*v));
//set_light('transitiontime', tran);
}
if(inlet==5){
hard_mode = v;
}
if (inlet==6)
{
scaler= v;
}
if (inlet===7)
{
group = v;
}
if (inlet===8)
{
alerto = v;
set_light('alert','select');
}
if (inlet===9)
{
var chue = v*scaler;
if(chue > 65280) chue = 65280;
set_light('hue', chue); // tone
}
if (inlet===10)
{
var slider_hue=Math.floor(v/64.0*65280);
if(slider_hue > 65280) slider_hue = 65280;
set_light('hue', slider_hue); // tone
CtoRGB(slider_hue);
}
if (inlet===11)
{
var slider_sat=v;
set_light('sat', slider_sat);
}
if (inlet===12)
{
var slider_bri=v;
set_light('bri', slider_bri);
}
//register_user();
}
if (inlet===14)
{
ent_mode_on=v;
}
}
function register_user(){
ajaxreq = new XMLHttpRequest();
ajaxreq.open("POST",'http://' + ip + '/api');
ajaxreq.onreadystatechange = readystatechange_parsejson;
ajaxreq.send('{"devicetype":"'+ key +'","username":"'+ key +'"}');
console.log('user registered');
}
function get_lights(){
ajaxreq = new XMLHttpRequest();
ajaxreq.open("GET",'http://' + ip + '/api/' + key +'/lights');
ajaxreq.onreadystatechange = readystatechange_parsejson;
ajaxreq.send();
}
function get_groups(){
ajaxreq = new XMLHttpRequest();
ajaxreq.open("GET",'http://' + ip + '/api/' + key +'/groups');
ajaxreq.onreadystatechange = readystatechange_parsejson;
ajaxreq.send();
}
function on(state)
{
ajaxreq = new XMLHttpRequest();
ajaxreq.open("PUT",'http://' + ip + '/api/' + key +'/lights/' + bulb_id + '/state');
ajaxreq.onreadystatechange = readystatechange_parsejson;
if (state == 1)
ajaxreq.send('{"on": true}');
if (state == 0)
ajaxreq.send('{"on": false}');
}
function set_light(cmd, value){
if (!ent_mode_on)
{
ajaxreq = new XMLHttpRequest();
ajaxreq.open("PUT",_SetUrl(cmd,value));
ajaxreq.onreadystatechange = readystatechange_parsejson;
// {"on":true, "sat":255, "bri":255,"hue":10000}
ajaxreq.send('{"'+cmd+'": '+value+',"transitiontime": '+tran+'}');
}
}
function set_light_hsv( h,s,v){
if (!ent_mode_on)
{
ajaxreq = new XMLHttpRequest();
ajaxreq.open("PUT",_SetUrl(h,h));
ajaxreq.onreadystatechange = readystatechange_parsejson;
// {"on":true, "sat":255, "bri":255,"hue":10000}
var jsend='{"hue": '+h+',"sat":'+s+',"bri":'+v+',"transitiontime": '+tran+'}';
ajaxreq.send(jsend);
}
}
function _SetUrl(cmd,value)
{
if (group)
{
return 'http://' + ip + '/api/' + key +'/groups/' + 1 + '/action';
}
else
return 'http://' + ip + '/api/' + key +'/lights/' + bulb_id + '/state';
}
function readystatechange_parsejson()
{
if (this.readyState ==4){
// post(this.responseText);
}
}
function CtoRGB(h) {
var rgbc=HSVtoRGB(h/65535,1,1);
var r = rgbc.r/255.0;
var g = rgbc.g/255.0;
var b = rgbc.b/255.0;
outlet(1,r);
outlet(2,g);
outlet(3,b);
outlet(4,1);
}
function list(a) {
if (!ent_mode_on)
{
if (inlet===9)
{
var div = 255;
var div_float= div*1.0;
// post("the list contains", arguments.length, "elements")
//post(JSON.stringify(arguments));
var hdata=rgb2hsv(Math.floor(arguments[0]*255),Math.floor(arguments[1]*255),Math.floor(arguments[2]*255));
var nhue=hdata.h*192;
var nsat=hdata.s*2.54;
var nbri=hdata.v*2.54;
if (nsat>254) nsat=254;
if (nsat<1) nsat=1;
if (nbri>254) nbri=254;
if (nbri<1) nbri=1;
if(nhue > 65280) nhue = 65280;
set_light_hsv(Math.floor(nhue),Math.floor(nsat),Math.floor(nbri)); // tone
}
}
}
function rgb2xyz(rgbR, rgbG, rgbB) {
const [ lrgbR, lrgbB, lrgbG ] = [ rgbR, rgbG, rgbB ].map(
function(v) { return v > 4.045 ? pow((v + 5.5) / 105.5, 2.4) * 100 : v / 12.92 }
);
const [ xyzX, xyzY, xyzZ ] = matrix([ lrgbR, lrgbB, lrgbG ], [
[0.4124564, 0.3575761, 0.1804375],
[0.2126729, 0.7151522, 0.0721750],
[0.0193339, 0.1191920, 0.9503041]
]);
return [ xyzX, xyzY, xyzZ ];
}
/**
* @func xyz2rgb
* @desc Return an XYZ color from an RGB color
* @param {Number} x - Chromaticity of X
* @param {Number} y - Chromaticity of Y
* @param {Number} z - Chromaticity of Z
* @return {ArrayRGB}
* @example
* xyz2rgb(41.25, 21.27, 1.93) // => [100, 0, 0]
* @link https://www.w3.org/TR/css-color-4/#rgb-to-lab
* @link https://www.w3.org/TR/css-color-4/#color-conversion-code
*/
function xyz2rgb(xyzX, xyzY, xyzZ) {
const [ lrgbR, lrgbB, lrgbG ] = matrix([ xyzX, xyzY, xyzZ ], [
[ 3.2404542, -1.5371385, -0.4985314],
[-0.9692660, 1.8760108, 0.0415560],
[ 0.0556434, -0.2040259, 1.0572252]
]);
const [ rgbR, rgbG, rgbB ] = [ lrgbR, lrgbB, lrgbG ].map(
function(v) {
return (v > 0.31308 ) ? (1.055 * Math.pow(v / 100, 1 / 2.4) * 100 - 5.5) : (12.92 * v)
}
);
return [ rgbR, rgbG, rgbB ];
}
function matrix(params, mats) {
return mats.map(
function (mat) { return mat.reduce(
// (acc, value, index) => acc + params[index] * value,
function (acc, value, index) { acc + params[index] * precision * (value * precision) / precision / precision },
0
);
}
);
}
function rgb2hsv (r, g, b) {
var rabs, gabs, babs, rr, gg, bb, h, s, v, diff, diffc, percentRoundFn;
rabs = r / 255;
gabs = g / 255;
babs = b / 255;
v = Math.max(rabs, gabs, babs),
diff = v - Math.min(rabs, gabs, babs);
diffc = function (c) { return (v - c) / 6 / diff + 1 / 2; }
percentRoundFn = function (num) { return Math.round(num * 100) / 100; }
if (diff == 0) {
h = s = 0;
} else {
s = diff / v;
rr = diffc(rabs);
gg = diffc(gabs);
bb = diffc(babs);
if (rabs === v) {
h = bb - gg;
} else if (gabs === v) {
h = (1 / 3) + rr - bb;
} else if (babs === v) {
h = (2 / 3) + gg - rr;
}
if (h < 0) {
h += 1;
}else if (h > 1) {
h -= 1;
}
}
return {
h: Math.round(h * 360),
s: percentRoundFn(s * 100),
v: percentRoundFn(v * 100)
};
}
function HSVtoRGB(h, s, v) {
var r, g, b, i, f, p, q, t;
if (arguments.length === 1) {
s = h.s, v = h.v, h = h.h;
}
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return {
r: Math.round(r * 255),
g: Math.round(g * 255),
b: Math.round(b * 255)
};
}