NEWS
Script Konvertierung HSV <==> Hex
-
Hi all,
Auf anfrage und. z.b. praktisch fuer Yahka, hier ein script zum konvertieren von HEX nach HSV und umgekehrt :
// ############################## // ######### DutchmanNL ######### // ###### HSV Color to HEX ###### // ############ V1.0 ############ // ############################## // Add the state containing HEX values here : const zigbeeDevice = [ 'zigbee.0.group_2.color', // Bank 'zigbee.0.group_3.color' // Vensterbank ]; // ##################################### // ## Don't change anything from here ## // ##################################### // Prepare variables const mySubscription = {}, debounceTimer = {}; // Create Folder structure extendObjectAsync(`0_userdata.0.HEXtoHSL` , { "type": "folder", "common": { "name": 'Convert HEX to HSL color', "role": "", "icon": "", }, "native": {}, }); // Read all array objects, create new state in javascript instance and subscribe on changes for (const device in zigbeeDevice) { // Define folder structure in userdata directory const statePrepare = zigbeeDevice[device].split('.'); const deviceName = `0_userdata.0.HEXtoHSL.${statePrepare[0]}_${statePrepare[1]}_${statePrepare[2]}` // Create Device Structure extendObjectAsync(deviceName , { "type": "device", "common": { "name": statePrepare[2], "role": "", "icon": "", }, "native": {}, }); // States to cover Hue and Sat values createState(`${deviceName}.hue` , { 'name': `Hue of ${statePrepare[2]}`, 'role': 'level.color.hue', 'type': 'number' }); // @ts-ignore createState(`${deviceName}.sat`, { 'name': `Sat of ${statePrepare[2]}`, 'role': 'level.color.sat', 'type': 'number' }); // Subscribe on state changes for HUE and Saturation // @ts-ignore mySubscription[`${deviceName}.hue`] = on( [`${deviceName}.hue`, `${deviceName}.sat` ], (data) => { // DebounceTimer // Reset timer (if running) and start new one for next watchdog interval if (debounceTimer[zigbeeDevice[device]]) { clearTimeout(debounceTimer[zigbeeDevice[device]]); debounceTimer[zigbeeDevice[device]] = null; } debounceTimer[zigbeeDevice[device]] = setTimeout(() => { if (!data.state.ack){ const h = getState(`${deviceName}.hue`).val / 360; const s = getState(`${deviceName}.sat`).val / 100; const v = 1; const colorRGB = hsvTOrgb(h,s,v) const colorHEX = rgbTOhex(colorRGB) console.log(`HSV value : ${h}, ${s}, ${v}`); setState(`${zigbeeDevice[device]}`, colorHEX); // setState(`${deviceName}.hue`, h * 360, true) // setState(`${deviceName}.sat`, h * 100, true) } }, (500)); }); // Subscribe on state changes for HEX surce mySubscription[`${deviceName}.hue`] = on( [ `${zigbeeDevice[device]}`, ], (data) => { console.log(`Device change detected : ${JSON.stringify(data.id)} value : ${data.state.val} | ack : ${data.state.ack}`); // DebounceTimer if (debounceTimer[zigbeeDevice[device]]) { clearTimeout(debounceTimer[zigbeeDevice[device]]); debounceTimer[zigbeeDevice[device]] = null; } debounceTimer[zigbeeDevice[device]] = setTimeout(() => { console.log(`Device change detected : ${JSON.stringify(data.id)} value : ${data.state.val} | ack : ${data.state.ack}`); const colorHEX = data.state.val; const colorRGB = hexTOrgb(colorHEX) const colorHSV = rgbTOhsv(colorRGB) const h = roundDigit(colorHSV[0]); console.log(colorHSV); const s = roundDigit(colorHSV[1]); setState(`${deviceName}.sat`, s, true); setState(`${deviceName}.hue`, h, true); }, (500)); }); } ////////////////Funktionen//////////////// /** * Coonvert HSV to RGB * @param {number} h - HUE value * @param {number} s - Saturation value * @param {number} v - Brightness value */ function hsvTOrgb(h, s, v) { var r, g, b, i, f, p, q, t; i = Math.floor(h * 6); f = h * 6 - i; p = v * (1 - s); q = v * (1 - f * s); t = v * (1 - (1 - f) * s); switch (i % 6) { case 0: r = v, g = t, b = p; break; case 1: r = q, g = v, b = p; break; case 2: r = p, g = v, b = t; break; case 3: r = p, g = q, b = v; break; case 4: r = t, g = p, b = v; break; case 5: r = v, g = p, b = q; break; } console.log(`${r} ${g} ${b}`) r=Math.round(255 * r); g=Math.round(255 * g); b=Math.round(255 * b); return [r,g,b]; }; function rgbTOhsv(rgb) { let rdif; let gdif; let bdif; let h; let s; const r = rgb[0] / 255; const g = rgb[1] / 255; const b = rgb[2] / 255; const v = Math.max(r, g, b); const diff = v - Math.min(r, g, b); const diffc = function (c) { return (v - c) / 6 / diff + 1 / 2; }; if (diff === 0) { h = 0; s = 0; } else { s = diff / v; rdif = diffc(r); gdif = diffc(g); bdif = diffc(b); if (r === v) { h = bdif - gdif; } else if (g === v) { h = (1 / 3) + rdif - bdif; } else if (b === v) { h = (2 / 3) + gdif - rdif; } if (h < 0) { h += 1; } else if (h > 1) { h -= 1; } } return [ h * 360, s * 100, v * 100 ]; }; function rgbTOhex (args) { const integer = ((Math.round(args[0]) & 0xFF) << 16) + ((Math.round(args[1]) & 0xFF) << 8) + (Math.round(args[2]) & 0xFF); const string = integer.toString(16).toUpperCase(); return '000000'.substring(string.length) + string; }; //In HEX konvertieren function toHex(number) { if (number < 0) number = 0xFFFFFFFF + number + 1; var n = number.toString(16).toUpperCase(); if (n.length == 1) { n = '0' + n; } return n; } // Function to convert HEX to RGB function hexTOrgb(args) { const match = args.toString(16).match(/[a-f0-9]{6}|[a-f0-9]{3}/i); if (!match) { return [0, 0, 0]; } let colorString = match[0]; if (match[0].length === 3) { colorString = colorString.split('').map(char => { return char + char; }).join(''); } const integer = parseInt(colorString, 16); const r = (integer >> 16) & 0xFF; const g = (integer >> 8) & 0xFF; const b = integer & 0xFF; return [r, g, b]; }; // Function to convert RGB to HSL function rgbTOhsl (rgb) { const r = rgb[0] / 255; const g = rgb[1] / 255; const b = rgb[2] / 255; const min = Math.min(r, g, b); const max = Math.max(r, g, b); const delta = max - min; let h; let s; if (max === min) { h = 0; } else if (r === max) { h = (g - b) / delta; } else if (g === max) { h = 2 + (b - r) / delta; } else if (b === max) { h = 4 + (r - g) / delta; } h = Math.min(h * 60, 360); if (h < 0) { h += 360; } const l = (min + max) / 2; if (max === min) { s = 0; } else if (l <= 0.5) { s = delta / (max + min); } else { s = delta / (2 - max - min); } return [h, s * 100, l * 100]; }; // Funtion to round digits properly function roundDigit(num){ // return Math.round((num + Number.EPSILON) * 100) / 100 return Math.round((num + Number.EPSILON)); }
-
@dutchman
Markiere bitte das Thema gemäß den Forum Rules als [Vorlage]. -
Statt dieser Schleife for-in
for (const device in zigbeeDevice) { // pick device from array using index myDevice = zigbeeDevice[device]; }
kann man auch for-of benutzen, sofern man den Index nicht braucht.
for (const device of zigbeeDevice) { // no need to pick from array, because 'of' returns the element and not the index myDevice = device; }
Hab ich aus JavaScript-Tutorials gelernt
-
hi,
sorry stehe momentan a bissl auf dem Schlauch: wie kann ich das Script nutzen/konfigurieren?
Werden Datenpunkte angelegt?
Grüße
Thomas