Skip to content

Request for direct 12 digit hex out put for Tuya smart LED  #13

@dane-k-1981

Description

@dane-k-1981

I would like to ask if its possible to add a 12 digit HEX color output/input as all my Tuya Smart 5CH Led using it.
At the moment I using decoding on input and output to change to right data.
Am a beginner so my code might be longer then it should be, but this is only way I know to make it work.

Output:
var hue = msg.payload.h;
var saturation = msg.payload.s10;
var value = msg.payload.v
10;

// Remove decimal places "issues with long decimals"
var integerNumber = Math.round(hue);
var integerNumber = Math.round(saturation);
var integerNumber = Math.round(value);

// Convert decimal to 4-digit hex
var hueHex = hue.toString(16).padStart(4, '0');
var saturationHex = saturation.toString(16).padStart(4, '0');
var valueHex = value.toString(16).padStart(4, '0');

// Format the HSV-A to HEX color string
var hsv = hueHex + saturationHex + valueHex;

msg.payload = hsv;
return msg;

To get 12 digit HEX to send to Tuya

Then from 12 digit hex from Tuya to HSV

payload.dps["24"] = 000003e803e8

var hexCode = msg.payload;

function hexToHsv(hex) {
// Remove the "#" symbol from the hex code
hex = hex.replace("#", "");

// Convert 12 digit hex to HSV
const h = parseInt(hex.slice(0, 4), 16);
const s = parseInt(hex.slice(4, 8), 16);
const v = parseInt(hex.slice(8, 12), 16);

// Convert RGB to HSV
//const [h, s, v] = convert.rgb.hsv(red, green, blue);

// Return the HSV values
//return { h, s, v };
return "hsv(" + h + ", " + s + ", " + v + ")";

}

const hsv = hexToHsv(hexCode);

msg.payload = hsv;
return msg;

// Input data string
var dataString = msg.payload; // Replace with your data string

// Extract values from the string
var values = dataString.match(/\d+/g);

// Create an object with properties h, s, and v
var dataObject = {
h: parseInt(values[0]),
s: parseInt(values[1])/10,
v: parseInt(values[2])/10
};

// Output data object
msg.payload = dataObject;
return msg;

So I get data to send back to color-picker.

Hope its possible to add. Thank you in advance for your time.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions