var colorconv = require('color-convert');
var converter = require('@q42philips/hue-color-converter');
var JS = "javascript.0."
var LightIDs = [ ["Light1","deconz.0.Light_1","LCT015"]];
/*
* Setup
*/
for (i=0; i<LightIDs.length; i++)
{
createState(LightIDs[i][0]+"-y_hue");
createState(LightIDs[i][0]+"-y_sat");
createState(LightIDs[i][0]+"-y_ct")
var LinkedObject = { self: JS+LightIDs[i][0], id: LightIDs[i][1], type:[i][2], v: ".bri", xy: '.xy', ct: '.ct' };
createState(LightIDs[i][0]+"-LinkedObject");
setState(LightIDs[i][0]+"-LinkedObject", { val: LinkedObject, ack:false });
on({ id:LightIDs[i][0] + "-y_hue", change: "ne"}, CalcXYfromHueSat);
on({ id:LightIDs[i][0] + "-y_sat", change: "ne"}, CalcXYfromHueSat);
on({ id:LightIDs[i][0] + "-y_ct", change: "ne"}, CalcXYfromct);
}
var timer = null;
function CalcXYfromHueSat(obj)
{
console.log('Object ' + JSON.stringify(obj.id) + ' was changed set');
var ChangedIdName = JSON.stringify(obj.id);
var i = ChangedIdName.lastIndexOf("-");
ObjectName = ChangedIdName.substring(1, i);
var Link = getState(ObjectName + "-LinkedObject").val
console.log('Link = '+JSON.stringify(Link))
console.log('Getting ' + Link.self + '-y_hue');
var Hue = getState(Link.self + '-y_hue').val; // 0 .. 360
console.log('Getting ' + Link.self + '-y_sat');
var Sat = getState(Link.self + '-y_sat').val; // 0 .. 100
console.log('Getting ' + Link.id + Link.v);
var Val = getState(Link.id + Link.v).val * 255 / 100; // 0 .. 100
console.log('Hue ' + JSON.stringify(Hue) + ' Sat ' + JSON.stringify(Sat) + ' Bri '+JSON.stringify(Val));
var rgb = colorconv.hsv.rgb(Hue, Sat, Val);
console.log('RGB returned ' + JSON.stringify(rgb))
var xy = converter.calculateXY(rgb[0], rgb[1], rgb[2], 'LCT015');
console.log('xy returned ' + JSON.stringify(xy))
if (!timer) { setState(Link.id + Link.xy, xy[0]+','+xy[1]); }
}
function CalcXYfromct(obj)
{
var ct = obj.state.val
var ctk = 6500 - ((ct-153) / (500-153) * 4500);
console.log('Object ' + JSON.stringify(obj.id) + ' was set to raw'+ct+' '+ctk+'K')
var ChangedIdName = JSON.stringify(obj.id);
var i = ChangedIdName.lastIndexOf("-");
ObjectName = ChangedIdName.substring(1, i);
var Link = getState(ObjectName + "-LinkedObject").val
console.log('Link = '+JSON.stringify(Link))
var rgb = colorTemperatureToRGB(ctk);
console.log('RGB returned ' + JSON.stringify(rgb))
var hsv = colorconv.rgb.hsv(rgb.r, rgb.g, rgb.b);
console.log('hsv returned ' + JSON.stringify(hsv))
if (!timer) {
timer = setTimeout(function() { timer = null; }, 50);
setState(Link.self+"-y_hue", hsv[0], false)
setState(Link.self+"-y_sat", hsv[1], false)
setState(Link.id + Link.ct, ct);
}
}
function colorTemperatureToRGB(kelvin){
var temp = kelvin / 100;
var red, green, blue;
if( temp <= 66 ){
red = 255;
green = temp;
green = 99.4708025861 * Math.log(green) - 161.1195681661;
if( temp <= 19){
blue = 0;
} else {
blue = temp-10;
blue = 138.5177312231 * Math.log(blue) - 305.0447927307;
}
} else {
red = temp - 60;
red = 329.698727446 * Math.pow(red, -0.1332047592);
green = temp - 60;
green = 288.1221695283 * Math.pow(green, -0.0755148492 );
blue = 255;
}
return {
r : clamp(red, 0, 255),
g : clamp(green, 0, 255),
b : clamp(blue, 0, 255)
}
}
function clamp( x, min, max ) {
if(x<min){ return min; }
if(x>max){ return max; }
return x;
}