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