Skip to content
  • Recent
  • Tags
  • 0 Unread 0
  • Categories
  • Unreplied
  • Popular
  • GitHub
  • Docu
  • Hilfe
Skins
  • Light
  • Brite
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Logo
  1. ioBroker Community Home
  2. Deutsch
  3. Skripten / Logik
  4. Script Konvertierung HSV <==> Hex

NEWS

  • UPDATE 31.10.: Amazon Alexa - ioBroker Skill läuft aus ?
    apollon77A
    apollon77
    48
    3
    8.1k

  • Monatsrückblick – September 2025
    BluefoxB
    Bluefox
    13
    1
    1.8k

  • Neues Video "KI im Smart Home" - ioBroker plus n8n
    BluefoxB
    Bluefox
    15
    1
    2.0k

Script Konvertierung HSV <==> Hex

Scheduled Pinned Locked Moved Skripten / Logik
javascript
4 Posts 4 Posters 875 Views 5 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • DutchmanD Offline
    DutchmanD Offline
    Dutchman
    Developer Most Active Administrators
    wrote on last edited by Dutchman
    #1

    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));
    }
    
    paul53P S 2 Replies Last reply
    3
    • DutchmanD Dutchman

      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));
      }
      
      paul53P Offline
      paul53P Offline
      paul53
      wrote on last edited by
      #2

      @dutchman
      Markiere bitte das Thema gemäß den Forum Rules als [Vorlage].

      Bitte verzichtet auf Chat-Nachrichten, denn die Handhabung ist grauenhaft !
      Produktiv: RPi 2 mit S.USV, HM-MOD-RPI und SLC-USB-Stick mit root fs

      1 Reply Last reply
      0
      • DutchmanD Dutchman

        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));
        }
        
        S Offline
        S Offline
        stan23
        wrote on last edited by
        #3

        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 😉

        Viele Grüße
        Marco

        1 Reply Last reply
        0
        • T Offline
          T Offline
          tklein
          wrote on last edited by
          #4

          hi,

          sorry stehe momentan a bissl auf dem Schlauch: wie kann ich das Script nutzen/konfigurieren?

          Werden Datenpunkte angelegt?

          Grüße
          Thomas

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          Support us

          ioBroker
          Community Adapters
          Donate

          607

          Online

          32.4k

          Users

          81.4k

          Topics

          1.3m

          Posts
          Community
          Impressum | Datenschutz-Bestimmungen | Nutzungsbedingungen
          ioBroker Community 2014-2025
          logo
          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Recent
          • Tags
          • Unread 0
          • Categories
          • Unreplied
          • Popular
          • GitHub
          • Docu
          • Hilfe