forked from michael5r/mmm-hue-lights
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmm-hue-lights.js
1182 lines (898 loc) · 35.9 KB
/
mmm-hue-lights.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* global Module */
/* Magic Mirror
* Module: mmm-hue-lights
*
* By Michael Schmidt
* https://github.com/michael5r
*
* MIT Licensed.
*/
Module.register('mmm-hue-lights', {
defaults: {
bridgeIp: '',
user: '',
displayType: 'grid',
displayMode: 'lights',
displayFilter: ['all'],
hideFilter: [],
hideOff: false,
alignment: 'left',
coloredList: true,
minimalList: false,
minimalGrid: false,
minimalGridUltra: false,
orderByName: false,
motionSleep: false,
motionSleepSeconds: 300, // this is in seconds (not ms)
updateInterval: 2 * 60 * 1000,
animationSpeed: 2 * 1000,
initialLoadDelay: 0,
version: '1.4.0'
},
getScripts: function() {
return [
'handlebars.runtime.min-v4.0.12.js',
'mmm-hue-lights-templates.js'
];
},
getStyles: function() {
return [
'mmm-hue-lights.css'
];
},
start: function() {
Log.info('Starting module: ' + this.name + ', version ' + this.config.version);
this.errMsg = '';
this.loaded = false;
this.scheduleUpdate(this.config.initialLoadDelay);
this.sleepTimer = null;
this.sleeping = false;
this.lights = {};
this.groups = {};
},
getDom: function() {
var displayType = this.config.displayType;
var displayMode = this.config.displayMode;
var alignment = this.config.alignment;
var minimalList = this.config.minimalList;
var minimalGrid = this.config.minimalGrid;
var minimalGridUltra = (displayMode === 'lights') ? this.config.minimalGridUltra : false;
var coloredList = this.config.coloredList;
var outer_wrapper = document.createElement('div');
// show error message
if (this.errMsg !== '') {
outer_wrapper.innerHTML = this.errMsg;
outer_wrapper.className = 'normal regular small';
return outer_wrapper;
}
// show loading message
if (!this.loaded) {
outer_wrapper.innerHTML = 'Loading ...';
outer_wrapper.className = 'bright light small';
return outer_wrapper;
}
if (displayType === 'list') {
// list mode view
outer_wrapper.className = this.classNames(
'hue-wrapper',
'list',
alignment,
minimalList ? 'minimal' : '',
displayMode === 'groups' ? 'groups' : '',
!coloredList ? 'bw' : ''
);
outer_wrapper.innerHTML = this.renderList();
} else {
// grid mode view
outer_wrapper.className = this.classNames(
'hue-wrapper',
'grid',
minimalGrid ? 'minimal' : '',
minimalGridUltra ? 'minimal-ultra' : '',
minimalGridUltra ? alignment : '',
displayMode === 'groups' ? 'groups' : ''
);
outer_wrapper.innerHTML = this.renderGrid();
}
return outer_wrapper;
},
renderList: function() {
var self = this;
var isLights = (this.config.displayMode === 'lights');
var hideOff = this.config.hideOff;
var minimalList = this.config.minimalList;
var alignment = this.config.alignment;
var orderByName = this.config.orderByName;
var lights = this.lights;
var groups = this.groups;
var hiddenOffItems = false;
// create handlebars data object
var hbData = {
isRightAligned: alignment === 'right',
isMinimal: minimalList,
c2Title: isLights ? 'Light' : 'Group',
c3Title: isLights ? 'Product' : 'Lights',
c4Title: 'Type',
rows: []
};
var data = isLights ? lights : groups;
var dataArr = Object.values(data); // convert to array
// sort by name if orderByName is true
if (orderByName) {
dataArr.sort((a, b) => (a.name > b.name) ? 1 : -1);
}
dataArr.forEach(function(item) {
var itemColorData = isLights ? item.state : item.action;
var isOn = false;
var allOn = false;
var anyOn = false;
if (isLights) {
isOn = itemColorData.on;
allOn = isOn;
anyOn = isOn;
} else {
allOn = item.state.all_on;
anyOn = item.state.any_on;
isOn = allOn || anyOn;
}
if ((hideOff && isOn) || (!hideOff)) {
hbData.rows.push(self.formatListRow(item));
} else {
hiddenOffItems = true;
}
});
if (hiddenOffItems && hbData.rows.length < 1) {
// user's lights or groups are all off
return '<div class="normal regular small">All your Hue lights are off.</div>';
} else {
// generate html from template
var hbTemplate = Handlebars.templates['hue_list_table.hbs'];
var hbHtml = hbTemplate(hbData);
return hbHtml;
}
},
formatListRow: function(item) {
// generates a single light or group object
// only used in the list view
var isLights = (this.config.displayMode === 'lights');
var coloredList = this.config.coloredList;
var lights = this.lights;
var groups = this.groups;
var itemColorData = isLights ? item.state : item.action;
var type = item.type;
var name = item.name;
var isOn = false;
var allOn = false;
var anyOn = false;
var lightsOn = 0;
var lightText = false;
if (isLights) {
// lights
isOn = itemColorData.on;
allOn = isOn;
anyOn = isOn;
} else {
// groups
allOn = item.state.all_on;
anyOn = item.state.any_on;
isOn = allOn || anyOn;
var groupLights = item.lights;
for (j = 0; j < groupLights.length; j++) {
var lightId = groupLights[j];
var light = this.lights[lightId];
if (light) {
if (light.state.on) {
lightsOn++;
}
}
}
if (allOn) {
lightText = 'All lights are on';
} else if (anyOn) {
if (lightsOn > 1) {
lightText = lightsOn + ' lights are on';
} else {
lightText = lightsOn + ' light is on';
}
} else {
lightText = 'All lights are off';
}
}
var mainLight = isLights ? item : { state: itemColorData };
var mainLightColor = this.getHueColorStyle(mainLight);
var mainLightStyle = mainLightColor.colorStyle;
if (!mainLightStyle) {
isOn = false;
}
if (!coloredList) {
mainLightStyle = false;
}
var rowClass = isOn? 'on' : 'off';
var rowStyles = isOn ? mainLightStyle : false;
var c2Text = isOn ? '<i class="fa fa-lightbulb-o"></i>' : '<i class="fa fa-power-off"></i>';
var c2Class = false;
var c3Text = isLights? item.productname : lightText;
var c3Class = false;
var c4Text = item.type;
var c4Class = false;
var row = {
rowClass,
rowStyles,
name,
c2Text,
c2Class,
c3Text,
c3Class,
c4Text,
c4Class
};
return row;
},
renderGrid: function() {
var self = this;
var isLights = (this.config.displayMode === 'lights');
var minimalGrid = this.config.minimalGrid;
var minimalGridUltra = isLights ? this.config.minimalGridUltra : false;
var hideOff = this.config.hideOff;
var orderByName = this.config.orderByName;
var lights = this.lights;
var groups = this.groups;
var numberOfLights = Object.keys(lights).length;
var numberOfGroups = Object.keys(groups).length;
var hiddenOffItems = false;
// create handlebars data object
var hbData = {
rows: []
};
// create rows
var data = isLights ? lights : groups;
var dataArr = Object.values(data); // convert to array
// sort by name if orderByName is true
if (orderByName) {
dataArr.sort((a, b) => (a.name > b.name) ? 1 : -1);
}
dataArr.forEach(function(item) {
var itemColorData = isLights ? item.state : item.action;
var type = item.type.toLowerCase();
var isOn = false;
var allOn = false;
var anyOn = false;
var itemBrightness = 0;
var itemBrightnessInPercent = 0;
var itemBrightnessStyle = '';
var lightText = false;
if (isLights) {
// lights
isOn = itemColorData.on;
allOn = isOn;
anyOn = isOn;
itemBrightness = itemColorData.bri; // value from 0 to 254
} else {
// groups
allOn = item.state.all_on;
anyOn = item.state.any_on;
isOn = allOn || anyOn;
}
// calculate colors for gradient or solid background
var colorStyle = false;
var lightOrDark = 'dark'; // default
var lightsOn = 0;
var coloredlightsOn = 0;
var contrast;
var hasColors = (itemColorData.colormode) && ((itemColorData.colormode === 'xy') || (itemColorData.colormode === 'hs'));
if (hasColors && isOn) {
// colored lights
// calculate contrast so we know whether to show a light or dark text
var mainLight = isLights ? item : { state: itemColorData };
var mainLightColor = self.getHueColorStyle(mainLight);
if (mainLightColor.colorRgb) {
contrast = self.contrast([255, 255, 255], [mainLightColor.colorRgb.r, mainLightColor.colorRgb.g, mainLightColor.colorRgb.b]);
if (contrast <= 1.5) {
// use dark text color
lightOrDark = 'dark';
} else {
// use light text color
lightOrDark = 'light';
}
}
if (isLights) {
// solid background for lights
lightsOn = 1;
colorStyle = 'background-color: ' + mainLightColor.colorHex + ';';
} else {
// gradient background for groups
var colorRgbArr = [];
var groupLights = item.lights;
for (j = 0; j < groupLights.length; j++) {
var lightId = groupLights[j];
var light = self.lights[lightId];
if (light) {
if (light.state.on) {
lightsOn++;
itemBrightness += light.state.bri;
var lightColor = self.getHueColorStyle(light);
if (lightColor.colorHex) {
colorRgbArr.push([
lightColor.colorRgb.r,
lightColor.colorRgb.g,
lightColor.colorRgb.b
]);
}
}
}
}
if (colorRgbArr.length > 0) {
// create gradient background color
var colorPercent = 100;
// sort colors (be aware that this removes duplicate colors)
var colorRgbSortedArr = (colorRgbArr.length > 1) ? self.sortColors(colorRgbArr) : colorRgbArr;
if (typeof colorRgbSortedArr === 'undefined') {
// all colors are the same
colorRgbSortedArr = colorRgbArr;
}
// set percentage
if (colorRgbSortedArr.length > 1) {
colorPercent = Math.round(100/parseInt((colorRgbSortedArr.length - 1)));
}
// override contrast settings based on sorted colors
contrast = self.contrast([255, 255, 255], [colorRgbSortedArr[0][0], colorRgbSortedArr[0][1], colorRgbSortedArr[0][2]]);
if (contrast <= 2.0) {
// use dark text color
lightOrDark = 'dark';
} else {
// use light text color
lightOrDark = 'light';
}
coloredlightsOn = colorRgbArr.length;
for (j = 0; j < colorRgbSortedArr.length; j++) {
if (colorRgbSortedArr.length > 1) {
// background gradient
if (j == 0) {
colorStyle = 'background: linear-gradient(to right, ' + self.rgbToHexAlt(colorRgbSortedArr[j]) + ' 0%';
} else {
colorStyle += ', ' + self.rgbToHexAlt(colorRgbSortedArr[j]) + ' ' + (colorPercent * j) + '%';
}
} else {
// solid background color
colorStyle = 'background-color: ' + self.rgbToHexAlt(colorRgbSortedArr[j]) + ';';
}
}
if (colorRgbSortedArr.length > 1) {
colorStyle += ');';
}
itemBrightness = itemBrightness / coloredlightsOn;
}
}
} else if (isOn) {
// white lights
if (isLights) {
// lights
lightsOn = 1;
} else {
// groups
var groupLights = item.lights;
for (j = 0; j < groupLights.length; j++) {
var lightId = groupLights[j];
var light = self.lights[lightId];
if (light) {
if (light.state.on) {
itemBrightness += light.state.bri;
lightsOn++;
}
}
}
}
}
// calculate brightness value in percent
if (isOn) {
itemBrightnessInPercent = Math.ceil(100/254 * itemBrightness);
// some weird tweaks based on what I saw in the Hue app
if (itemBrightnessInPercent < 91) {
itemBrightnessStyle = 'calc(' + (itemBrightnessInPercent - 1) + '% + 20px)';
} else if (itemBrightnessInPercent > 97) {
itemBrightnessStyle = '100%';
} else {
itemBrightnessStyle = itemBrightnessInPercent + '%';
}
}
// set light text (only relevant for groups)
if (!isLights) {
if (allOn) {
lightText = 'All lights are on';
} else if (anyOn) {
if (lightsOn > 1) {
lightText = lightsOn + ' lights are on';
} else {
lightText = lightsOn + ' light is on';
}
} else {
lightText = 'All lights are off';
}
}
// set class for row
var itemClass = self.classNames(
isOn ? 'on' : 'off',
colorStyle ? 'colored-' + lightOrDark : ''
);
// push data to HB object
var rowObj = {
isOn: isOn,
isMinimal: minimalGrid || minimalGridUltra ? true : false,
isMinimalUltra: minimalGridUltra,
name: item.name,
class: itemClass,
colorStyle,
lightText,
itemBrightnessStyle
};
if (hideOff && !isOn) {
hiddenOffItems = true;
}
if ((hideOff && isOn) || (!hideOff)) {
hbData.rows.push(rowObj);
}
});
if (hiddenOffItems && hbData.rows.length < 1) {
// user's lights or groups are all off
return '<div class="normal regular small">All your Hue lights are off.</div>';
} else {
// generate html from template
var hbTemplate = Handlebars.templates['hue_grid.hbs'];
var hbHtml = hbTemplate(hbData);
return hbHtml;
}
},
getData: function() {
if (!this.sleeping) {
if ((this.config.bridgeIp === '') || (this.config.user === '')) {
this.errMsg = 'Please add your Hue bridge IP and user to the MagicMirror config.js file.';
this.updateDom(this.config.animationSpeed);
} else {
this.sendSocketNotification('MMM_HUE_LIGHTS_GET', {
bridgeIp: this.config.bridgeIp,
user: this.config.user
});
}
}
},
notificationReceived(notification, payload, sender) {
var self = this;
if ((notification === 'USER_PRESENCE') && (this.config.motionSleep)) {
if (payload === true) {
if (this.sleeping) {
this.show(this.config.animationSpeed);
} else {
clearTimeout(this.sleepTimer);
this.sleepTimer = setTimeout(function() {
self.hide(self.config.animationSpeed);
}, self.config.motionSleepSeconds * 1000);
}
}
}
},
socketNotificationReceived: function(notification, payload) {
var self = this;
if (notification === 'MMM_HUE_LIGHTS_DATA') {
self.processHueData(payload);
} else if (notification === 'MMM_HUE_LIGHTS_DATA_ERROR') {
self.errMsg = payload;
self.updateDom(self.config.animationSpeed);
}
self.scheduleUpdate(self.config.updateInterval);
},
suspend: function() {
// this method is triggered when a module is hidden using this.hide()
this.sleeping = true;
},
resume: function() {
// this method is triggered when a module is shown using this.show()
var self = this;
if (this.sleeping) {
this.sleeping = false;
// get new data
this.getData();
// restart timer
if (this.config.motionSleep) {
clearTimeout(this.sleepTimer);
this.sleepTimer = setTimeout(function() {
self.hide(self.config.animationSpeed);
}, self.config.motionSleepSeconds * 1000);
}
}
},
processHueData: function(data) {
var self = this;
var displayMode = this.config.displayMode;
var displayFilter = this.config.displayFilter;
var hideFilter = this.config.hideFilter;
var renderUi = true;
var forceRender = false;
var oldLights = this.lights;
var oldGroups = this.groups;
var numberOfOldLights = Object.keys(oldLights).length;
var numberOfOldGroups = Object.keys(oldGroups).length;
var lights = data.lights;
var groups = data.groups;
// for the groups, let's immediately filter out anything that doesn't have a type of 'Room'
Object.keys(data.groups).forEach(function(key) {
var itemType = data.groups[key].type.toLowerCase();
if (itemType !== 'room') {
delete data.groups[key];
}
});
var itemsToFilter = (displayMode === 'lights') ? lights : groups;
var itemsFilteredOut = 0;
// check for positive filtering
if (this.isArray(displayFilter)) {
if (displayFilter.length > 0) {
// if the array is empty, we'll assume they want to see all lights/groups
if (displayFilter[0] !== 'all') {
Object.keys(itemsToFilter).forEach(function(key) {
var itemName = itemsToFilter[key].name.toLowerCase();
var deleteItem = true;
for (i = 0; i < displayFilter.length; i++) {
var filterString = displayFilter[i].toLowerCase().trim();
if (itemName.indexOf(filterString) > -1) {
// this should NOT be filtered out
deleteItem = false;
}
}
if (deleteItem) {
itemsFilteredOut++;
delete itemsToFilter[key];
}
});
}
}
} else if (this.isString(displayFilter)) {
// we will be nice and assume the user meant for this to be an array with the string inside it
if (displayFilter !== 'all') {
// filter based on the string
var filterString = displayFilter.toLowerCase().trim();
Object.keys(itemsToFilter).forEach(function(key) {
var itemName = itemsToFilter[key].name.toLowerCase();
if (itemName.indexOf(filterString) < 0) {
// this should be filtered out
itemsFilteredOut++;
delete itemsToFilter[key];
}
});
}
}
// check for negative filtering
if (this.isArray(hideFilter)) {
if (hideFilter.length > 0) {
Object.keys(itemsToFilter).forEach(function(key) {
var itemName = itemsToFilter[key].name.toLowerCase();
var deleteItem = false;
for (i = 0; i < hideFilter.length; i++) {
var filterString = hideFilter[i].toLowerCase().trim();
if (itemName.indexOf(filterString) > -1) {
// this should be filtered out
deleteItem = true;
}
}
if (deleteItem) {
itemsFilteredOut++;
delete itemsToFilter[key];
}
});
}
}
var numberOfLights = (lights) ? Object.keys(data.lights).length : 0;
var numberOfGroups = (groups) ? Object.keys(data.groups).length : 0;
// check if lights of each group is reachable
// no reachable lights will be "marked" as off
Object.values(data.groups).forEach(function (group, index) {
var numberOfLightsInGroup = group.lights.length;
var any_on = false;
for (var i = 0; i < numberOfLightsInGroup; i++) {
var hueLightID = group.lights[i];
if (data.lights[hueLightID].state.reachable == false) {
// if light is not reachable
data.lights[hueLightID].state.on = false;
group.state.all_on = false;
forceRender = true;
} else {
if (data.lights[hueLightID].state.on == true) {
any_on = true;
}
}
}
group.state.any_on = any_on;
});
// check old data to make sure we're not re-rendering the UI for no reason
if (this.loaded) {
if (numberOfOldLights !== numberOfLights) {
// number of lights changed (update dom)
} else if (numberOfOldGroups !== numberOfGroups) {
// number of groups changed (update dom)
} else if (forceRender) {
// force render (update dom)
} else {
// compare status of lights
if ((this.jsonEqual(oldLights,lights)) && (this.jsonEqual(oldGroups,groups))) {
renderUi = false;
}
}
}
this.loaded = true;
this.lights = lights;
this.groups = groups;
if (renderUi) {
if (itemsFilteredOut > 0) {
if ((displayMode === 'lights') && (numberOfLights === 0)) {
this.errMsg = 'No Hue lights were found<br>with the filter(s) you specified.';
} else if ((displayMode === 'groups') && (numberOfGroups === 0)) {
this.errMsg = 'No Hue light groups were found<br>with the filter(s) you specified.';
}
} else if ((displayMode === 'lights') && (numberOfLights === 0)) {
this.errMsg = 'You have no Hue lights.';
} else if ((displayMode === 'groups') && (numberOfGroups === 0)) {
this.errMsg = 'You have no Hue light groups.';
} else {
this.errMsg = '';
}
this.updateDom(this.config.animationSpeed);
}
},
getHueColorStyle: function(item) {
/*
Hue lights use HSB color space for colormode ct, not RGB
This is used for regular color light bulbs
item.state.hue = The hue value to set light to. The hue value is a wrapping value between 0 and 65535. Both 0 and 65535 are red, 25500 is green and 46920 is blue.
item.state.bri = Brightness of the light. This is a scale from the minimum brightness the light is capable of, 1, to the maximum capable brightness, 254.
item.state.sat = Saturation of the light. 254 is the most saturated (colored) and 0 is the least saturated (white).
Hue lights use CIE color space for colormode xy
This is used for color lightstrips and blooms
item.state.xy = The x and y coordinates of a color in CIE color space.The first entry is the x coordinate and the second entry is the y coordinate. Both x and y must be between 0 and 1.
item.state.colormode = Indicates the color mode in which the light is working, this is the last command type it received. Values are “hs” for Hue and Saturation, “xy” for XY and “ct” for Color Temperature. This parameter is only present when the light supports at least one of the values.
*/
var colorRgb = false;
var colorHex = false;
var colorStyle = false;
if (item.state.colormode && item.state.xy && item.state.bri) {
// colored light, with x, y, and bri values
colorRgb = this.XYtoRGB(item.state.xy[0], item.state.xy[1],item.state.bri);
colorHex = this.rgbToHex(colorRgb);
if (colorHex !== '#000000') {
colorStyle = 'color: ' + colorHex + '; border-color: ' + colorHex + ' !important;';
}
} else if (item.state.bri) {
// white light with bri value
// just a hacky conversion to show brightness
var bri = Math.floor(100/255 * item.state.bri);
if (bri > 0) {
if (bri > 98) {
bri = '1';
} else {
bri = '0.' + bri;
}
colorStyle = 'color: #fff; opacity: ' + bri +';';
}
}
return {
colorHex,
colorStyle,
colorRgb
};
},
isString: function(val) {
return typeof val === 'string' || val instanceof String;
},
isArray: function(val) {
return Array.isArray(val);
},
// https://github.com/JedWatson/classnames/
classNames: function() {
var classes = [];
for (var i = 0; i < arguments.length; i++) {
var arg = arguments[i];
if (!arg) continue;
var argType = typeof arg;
if (argType === 'string' || argType === 'number') {
classes.push(arg);
} else if (Array.isArray(arg) && arg.length) {
var inner = classNames.apply(null, arg);
if (inner) {
classes.push(inner);
}
} else if (argType === 'object') {
for (var key in arg) {
if (hasOwn.call(arg, key) && arg[key]) {
classes.push(key);
}
}
}
}
return classes.join(' ');
},
jsonEqual: function(a,b) {
return JSON.stringify(a) === JSON.stringify(b);
},
XYtoRGB: function(x, y, brightness) {
// convert X, Y, bri colors to RGB
if (brightness === undefined) {
brightness = 254;
}
var z = 1.0 - x - y;
var Y = brightness;
//var Y = (brightness / 254).toFixed(2);
var X = (Y / y) * x;
var Z = (Y / y) * z;
// convert to RGB using Wide RGB D65 conversion
var red = X * 1.656492 - Y * 0.354851 - Z * 0.255038;
var green = -X * 0.707196 + Y * 1.655397 + Z * 0.036152;
var blue = X * 0.051713 - Y * 0.121364 + Z * 1.011530;
// if red, green or blue is larger than 1.0 set it back to the maximum of 1.0
if (red > blue && red > green && red > 1.0) {
green = green / red;
blue = blue / red;
red = 1.0;
} else if (green > blue && green > red && green > 1.0) {
red = red / green;
blue = blue / green;
green = 1.0;
} else if (blue > red && blue > green && blue > 1.0) {
red = red / blue;
green = green / blue;
blue = 1.0;
}
// reverse gamma correction
red = red <= 0.0031308 ? 12.92 * red : (1.0 + 0.055) * Math.pow(red, (1.0 / 2.4)) - 0.055;
green = green <= 0.0031308 ? 12.92 * green : (1.0 + 0.055) * Math.pow(green, (1.0 / 2.4)) - 0.055;
blue = blue <= 0.0031308 ? 12.92 * blue : (1.0 + 0.055) * Math.pow(blue, (1.0 / 2.4)) - 0.055;
if (red > blue && red > green) {
// red is biggest
if (red > 1.0) {
green = green / red;
blue = blue / red;
red = 1.0;
}
} else if (green > blue && green > red) {
// green is biggest
if (green > 1.0) {
red = red / green;
blue = blue / green;
green = 1.0;
}
} else if (blue > red && blue > green) {
// blue is biggest
if (blue > 1.0) {