mirror of
https://github.com/lehanspb/tuya-mqtt.git
synced 2025-12-18 16:17:30 +00:00
added comments and updated color calculation
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,4 +1,5 @@
|
|||||||
devices/
|
devices/
|
||||||
|
test/
|
||||||
|
|
||||||
|
|
||||||
# Logs
|
# Logs
|
||||||
|
|||||||
136
tuya-color.js
136
tuya-color.js
@@ -1,10 +1,12 @@
|
|||||||
const convert = require('color-convert');
|
const convert = require('color-convert');
|
||||||
const debug = require('debug')('TuyaColor');
|
const debug = require('debug')('TuyaColor');
|
||||||
|
|
||||||
function TuyaColorLight(tuya) {
|
/**
|
||||||
this.tuya = tuya;
|
* Class to calculate settings for Tuya colors
|
||||||
|
*/
|
||||||
|
function TuyaColorLight() {
|
||||||
|
|
||||||
this.colorMode = 'white';
|
this.colorMode = 'white'; // or 'colour'
|
||||||
this.brightness = 100; // percentage value use _convertValToPercentage functions below.
|
this.brightness = 100; // percentage value use _convertValToPercentage functions below.
|
||||||
|
|
||||||
this.color = {
|
this.color = {
|
||||||
@@ -12,11 +14,6 @@ function TuyaColorLight(tuya) {
|
|||||||
S: 100,
|
S: 100,
|
||||||
L: 50
|
L: 50
|
||||||
};
|
};
|
||||||
this.color2 = {
|
|
||||||
H: 0,
|
|
||||||
S: 100,
|
|
||||||
L: 50
|
|
||||||
};
|
|
||||||
|
|
||||||
this.hue = this.color.H;
|
this.hue = this.color.H;
|
||||||
this.saturation = this.color.S;
|
this.saturation = this.color.S;
|
||||||
@@ -29,18 +26,33 @@ function TuyaColorLight(tuya) {
|
|||||||
this.dps = {};
|
this.dps = {};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* calculate color value from given percentage
|
||||||
|
* @param {Integer} percentage 0-100 percentage value
|
||||||
|
* @returns {Integer} color value from 0-255
|
||||||
|
*/
|
||||||
TuyaColorLight.prototype._convertPercentageToVal = function (percentage) {
|
TuyaColorLight.prototype._convertPercentageToVal = function (percentage) {
|
||||||
var tmp = Math.round(255 * (percentage / 100));
|
var tmp = Math.round(255 * (percentage / 100));
|
||||||
debug('Converted ' + percentage + ' to: ' + tmp);
|
debug('Converted ' + percentage + ' to: ' + tmp);
|
||||||
return tmp;
|
return tmp;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* calculate percentage from color value
|
||||||
|
* @param {Integer} val 0-255 color value
|
||||||
|
* @returns {Integer} HK-Value
|
||||||
|
*/
|
||||||
TuyaColorLight.prototype._convertValToPercentage = function (val) {
|
TuyaColorLight.prototype._convertValToPercentage = function (val) {
|
||||||
var tmp = Math.round((val / 255) * 100);
|
var tmp = Math.round((val / 255) * 100);
|
||||||
debug('Converted ' + val + ' to: ' + tmp);
|
debug('Converted ' + val + ' to: ' + tmp);
|
||||||
return tmp;
|
return tmp;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* converts color value to color temperature
|
||||||
|
* @param {Integer} val
|
||||||
|
* @returns {Integer} percentage from 0-100
|
||||||
|
*/
|
||||||
TuyaColorLight.prototype._convertColorTemperature = function (val) {
|
TuyaColorLight.prototype._convertColorTemperature = function (val) {
|
||||||
var tmpRange = this.colorTempMax - this.colorTempMin;
|
var tmpRange = this.colorTempMax - this.colorTempMin;
|
||||||
var tmpCalc = Math.round((val / this.colorTempMax) * 100);
|
var tmpCalc = Math.round((val / this.colorTempMax) * 100);
|
||||||
@@ -56,9 +68,13 @@ TuyaColorLight.prototype._convertColorTemperature = function (val) {
|
|||||||
debug('HK tuyaColorTemp: ' + tuyaColorTemp);
|
debug('HK tuyaColorTemp: ' + tuyaColorTemp);
|
||||||
|
|
||||||
return tuyaColorTemp;
|
return tuyaColorTemp;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Convert color temperature to HK
|
||||||
|
* @param {Integer} val
|
||||||
|
* @returns {Integer} HK-Value
|
||||||
|
*/
|
||||||
TuyaColorLight.prototype._convertColorTemperatureToHK = function (val) {
|
TuyaColorLight.prototype._convertColorTemperatureToHK = function (val) {
|
||||||
|
|
||||||
var tuyaColorTempPercent = this._convertValToPercentage(this.colorTemperature);
|
var tuyaColorTempPercent = this._convertValToPercentage(this.colorTemperature);
|
||||||
@@ -77,19 +93,25 @@ TuyaColorLight.prototype._convertColorTemperatureToHK = function (val) {
|
|||||||
debug('HK Value: ' + hkValue);
|
debug('HK Value: ' + hkValue);
|
||||||
|
|
||||||
return hkValue;
|
return hkValue;
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* check if given String is HEX
|
||||||
|
* @param {String} h
|
||||||
|
* @returns {boolean}
|
||||||
|
*/
|
||||||
TuyaColorLight.prototype._ValIsHex = function (h) {
|
TuyaColorLight.prototype._ValIsHex = function (h) {
|
||||||
debug("Check if value is hex", h);
|
debug("Check if value is hex", h);
|
||||||
var a = parseInt(h, 16);
|
return /(^#[0-9A-F]{6}$)|(^#[0-9A-F]{3}$)/i.test(h)
|
||||||
var result = (a.toString(16) === h.toLowerCase());
|
|
||||||
return result;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get AlphaHex from percentage brightness
|
||||||
|
* @param {Integer} brightness
|
||||||
|
* @return {string} brightness as HEX value
|
||||||
|
*/
|
||||||
TuyaColorLight.prototype._getAlphaHex = function (brightness) {
|
TuyaColorLight.prototype._getAlphaHex = function (brightness) {
|
||||||
// for (var i = 1; i >= 0; i -= 0.01) {
|
|
||||||
var i = brightness / 100;
|
var i = brightness / 100;
|
||||||
debug('input brightness: ' + brightness + ' and i is ' + i);
|
|
||||||
var alpha = Math.round(i * 255);
|
var alpha = Math.round(i * 255);
|
||||||
var hex = (alpha + 0x10000).toString(16).substr(-2);
|
var hex = (alpha + 0x10000).toString(16).substr(-2);
|
||||||
var perc = Math.round(i * 100);
|
var perc = Math.round(i * 100);
|
||||||
@@ -98,30 +120,38 @@ TuyaColorLight.prototype._getAlphaHex = function (brightness) {
|
|||||||
return hex;
|
return hex;
|
||||||
};
|
};
|
||||||
|
|
||||||
TuyaColorLight.prototype.setSaturation = function (value, callback) {
|
/**
|
||||||
var colorMode = 'colour';
|
* Set saturation from value
|
||||||
var saturation = value;
|
* @param {Integer} value
|
||||||
var color = this.color;
|
*/
|
||||||
color.S = value;
|
TuyaColorLight.prototype.setSaturation = function (value) {
|
||||||
|
this.color.S = value;
|
||||||
|
this.saturation = value;
|
||||||
|
this.colorMode = 'colour';
|
||||||
|
|
||||||
this.color = color;
|
debug('SET SATURATION: ' + value);
|
||||||
this.colorMode = colorMode;
|
|
||||||
this.saturation = saturation;
|
|
||||||
|
|
||||||
debug(' SET SATURATION: ' + value);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
TuyaColorLight.prototype.setBrightness = function (value, callback) {
|
/**
|
||||||
|
* Set Brightness
|
||||||
|
* @param {Integer} value
|
||||||
|
*/
|
||||||
|
TuyaColorLight.prototype.setBrightness = function (value) {
|
||||||
this.brightness = value;
|
this.brightness = value;
|
||||||
var newValue = this._convertPercentageToVal(value);
|
var newValue = this._convertPercentageToVal(value);
|
||||||
debug("BRIGHTNESS from UI: " + value + ' Converted from 100 to 255 scale: ' + newValue);
|
debug("BRIGHTNESS from UI: " + value + ' Converted from 100 to 255 scale: ' + newValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
TuyaColorLight.prototype.setHue = function (value, callback) {
|
/**
|
||||||
|
* @param {} value
|
||||||
|
*/
|
||||||
|
TuyaColorLight.prototype.setHue = function (value) {
|
||||||
debug('SET HUE: ' + value);
|
debug('SET HUE: ' + value);
|
||||||
debug('Saturation Value: ' + this.color.S);
|
debug('Saturation Value: ' + this.color.S);
|
||||||
this.color.H = value;
|
this.color.H = value;
|
||||||
|
|
||||||
|
//check color and set colormode if necessary
|
||||||
|
debug("colormode", value, this.color.S);
|
||||||
if (value === 0 && this.color.S === 0) {
|
if (value === 0 && this.color.S === 0) {
|
||||||
this.colorMode = 'white';
|
this.colorMode = 'white';
|
||||||
debug('SET Color Mode: \'white\'');
|
debug('SET Color Mode: \'white\'');
|
||||||
@@ -131,20 +161,31 @@ TuyaColorLight.prototype.setHue = function (value, callback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
var returnVal = {};
|
return {
|
||||||
|
color: this.color,
|
||||||
returnVal.color = this.color;
|
colorMode: this.colorMode,
|
||||||
returnVal.colorMode = this.colorMode;
|
hue: this.color.H,
|
||||||
returnVal.hue = this.color.H;
|
saturation: this.saturation
|
||||||
returnVal.saturation = this.saturation;
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set HSL color
|
||||||
|
* @param {Integer} hue
|
||||||
|
* @param {Integer} saturation
|
||||||
|
* @param {Integer} brightness
|
||||||
|
*/
|
||||||
TuyaColorLight.prototype.setHSL = function (hue, saturation, brightness) {
|
TuyaColorLight.prototype.setHSL = function (hue, saturation, brightness) {
|
||||||
this.setBrightness(brightness);
|
|
||||||
this.setSaturation(saturation);
|
this.setSaturation(saturation);
|
||||||
|
this.setBrightness(brightness);
|
||||||
this.setHue(hue);
|
this.setHue(hue);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set color from given string
|
||||||
|
* @param {String} colorValue could be HEX or HSL color type
|
||||||
|
* @returns {Object} dps settings for given color
|
||||||
|
*/
|
||||||
TuyaColorLight.prototype.setColor = function (colorValue) {
|
TuyaColorLight.prototype.setColor = function (colorValue) {
|
||||||
debug("Recieved color", colorValue);
|
debug("Recieved color", colorValue);
|
||||||
|
|
||||||
@@ -154,38 +195,39 @@ TuyaColorLight.prototype.setColor = function (colorValue) {
|
|||||||
} else {
|
} else {
|
||||||
debug("Color is HSL");
|
debug("Color is HSL");
|
||||||
var color = colorValue.split(",");
|
var color = colorValue.split(",");
|
||||||
|
// convert strings to numbers
|
||||||
|
color.forEach(function (element, key) {
|
||||||
|
color[key] = parseInt(element, 10);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
debug("Converted color as HSL", color);
|
debug("Converted color as HSL", {
|
||||||
|
0: color[0] + " - " + typeof color[0],
|
||||||
|
1: color[1] + " - " + typeof color[1],
|
||||||
|
2: color[2] + " - " + typeof color[2]
|
||||||
|
})
|
||||||
|
|
||||||
this.setHSL(color[0], color[1], color[2]);
|
this.setHSL(color[0], color[1], color[2]);
|
||||||
return this.getDps();
|
return this.getDps();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get dps settings for current color
|
||||||
|
* @returns {Object} dps settings
|
||||||
|
*/
|
||||||
TuyaColorLight.prototype.getDps = function () {
|
TuyaColorLight.prototype.getDps = function () {
|
||||||
var color = this.color;
|
var color = this.color;
|
||||||
var color2 = this.color2;
|
|
||||||
|
|
||||||
var lightness = Math.round(this.brightness / 2);
|
var lightness = Math.round(this.brightness / 2);
|
||||||
var brightness = this.brightness;
|
var brightness = this.brightness;
|
||||||
var apiBrightness = this._convertPercentageToVal(brightness);
|
var apiBrightness = this._convertPercentageToVal(brightness);
|
||||||
var alphaBrightness = this._getAlphaHex(brightness);
|
var alphaBrightness = this._getAlphaHex(brightness);
|
||||||
|
|
||||||
var hexColorOriginal1 = convert.hsl.hex(color.H, color.S, color.L);
|
|
||||||
var rgbColorOriginal1 = convert.hsl.rgb(color.H, color.S, color.L);
|
|
||||||
|
|
||||||
var hexColorOriginal2 = convert.hsl.hex(0, 0, 50);
|
|
||||||
var rgbColorOriginal2 = convert.hsl.rgb(0, 0, 50);
|
|
||||||
|
|
||||||
var hexColor1 = convert.hsl.hex(color.H, color.S, lightness);
|
var hexColor1 = convert.hsl.hex(color.H, color.S, lightness);
|
||||||
var rgbColor1 = convert.hsl.rgb(color.H, color.S, lightness);
|
|
||||||
|
|
||||||
var hexColor2 = convert.hsl.hex(0, 0, lightness);
|
var hexColor2 = convert.hsl.hex(0, 0, lightness);
|
||||||
var rgbColor2 = convert.hsl.rgb(0, 0, lightness);
|
|
||||||
|
|
||||||
var colorTemperature = this.colorTemperature;
|
var colorTemperature = this.colorTemperature;
|
||||||
|
|
||||||
// var ww = Math.round((this.brightness * 255) / 100);
|
|
||||||
|
|
||||||
var lightColor = (hexColor1 + hexColor2 + alphaBrightness).toLowerCase();
|
var lightColor = (hexColor1 + hexColor2 + alphaBrightness).toLowerCase();
|
||||||
|
|
||||||
var temperature = (this.colorMode === 'colour') ? 255 : this._convertColorTemperature(colorTemperature);
|
var temperature = (this.colorMode === 'colour') ? 255 : this._convertColorTemperature(colorTemperature);
|
||||||
@@ -198,7 +240,7 @@ TuyaColorLight.prototype.getDps = function () {
|
|||||||
'5': lightColor
|
'5': lightColor
|
||||||
// '6' : hexColor + hexColor + 'ff'
|
// '6' : hexColor + hexColor + 'ff'
|
||||||
};
|
};
|
||||||
debug(dpsTmp);
|
debug("dps", dpsTmp);
|
||||||
return dpsTmp;
|
return dpsTmp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user