Hue (degrees), saturation [0, 100], lightness [0, 100]
422720
private static int  hslToRgb(float hue, float sat, float light)
   {
      hue = (hue >= 0f) ? hue % 360f : (hue % 360f) + 360f;  // positive modulo (ie. -10 => 350)
      hue /= 60f;    // [0, 360] -> [0, 6]
      sat /= 100;   // [0, 100] -> [0, 1]
      light /= 100; // [0, 100] -> [0, 1]
      sat = (sat < 0f) ? 0f : (sat > 1f) ? 1f : sat;
      light = (light < 0f) ? 0f : (light > 1f) ? 1f : light;
      float  t1, t2;
      if (light <= 0.5f) {
         t2 = light * (sat + 1f);
      } else {
         t2 = light + sat - (light * sat);
      }
      t1 = light * 2f - t2;
      float  r = hueToRgb(t1, t2, hue + 2f);
      float  g = hueToRgb(t1, t2, hue);
      float  b = hueToRgb(t1, t2, hue - 2f);
      return clamp255(r * 256f)<<16 | clamp255(g * 256f)<<8 | clamp255(b * 256f);
   }
378642
private static double calculateHue(double red, double green, double blue, double max, double chroma) {

    if (chroma == 0) {
      return 0;
    } else {
      double hue;
      if (red == max) {
        hue = (green - blue) / chroma;
      } else if (green == max) {
        hue = (blue - red) / chroma + 2;
      } else {
        hue = (red - green) / chroma + 4;
      }
      hue = hue * 60.0;
      if (hue < 0) {
        hue = hue + Hue.MAX_VALUE;
      }
      return hue;
    }
  }
241254
def _lightness(color, **kwargs):
    
    l = colorsys.rgb_to_hls(*[x / 255.0 for x in color.value[:3]])[1]
    return NumberValue((l * 100, '%'))
546085
function brightColor(layerId, alpha) {
  var luminosity = 'bright';
  var hue = null;

  if (/water|ocean|lake|sea|river/.test(layerId)) {
    hue = 'blue';
  }

  if (/state|country|place/.test(layerId)) {
    hue = 'pink';
  }

  if (/road|highway|transport/.test(layerId)) {
    hue = 'orange';
  }

  if (/contour|building/.test(layerId)) {
    hue = 'monochrome';
  }

  if (/building/.test(layerId)) {
    luminosity = 'dark';
  }

  if (/contour|landuse/.test(layerId)) {
    hue = 'yellow';
  }

  if (/wood|forest|park|landcover/.test(layerId)) {
    hue = 'green';
  }

  var rgb = randomColor({
    luminosity: luminosity,
    hue: hue,
    seed: layerId,
    format: 'rgbArray'
  });

  var rgba = rgb.concat([alpha || 1]);
  return 'rgba(' + rgba.join(', ') + ')';
}
1342217
def set_light_state_raw(self, hue, saturation, brightness, kelvin,
                            bulb=ALL_BULBS, timeout=None):
        
        with _blocking(self.lock, self.light_state, self.light_state_event,
                       timeout):
            self.send(REQ_SET_LIGHT_STATE, bulb, 'xHHHHI',
                      hue, saturation, brightness, kelvin, 0)
            self.send(REQ_GET_LIGHT_STATE, ALL_BULBS, '')
        return self.light_state
32215
private void HSVtoRGB(float hue, float sat, float bri) {

        int i;
        float f;
        float p;
        float q;
        float t;
        if (sat == 0) {
            m_red = bri;
            m_green = bri;
            m_blue = bri;
            return;
        }
        hue /= 60;
        i = (int)Math.floor(hue);
        f = hue - i;
        p = bri * (1 - sat);
        q = bri * (1 - (sat * f));
        t = bri * (1 - (sat * (1 - f)));
        switch (i) {
            case 0:
                m_red = bri;
                m_green = t;
                m_blue = p;
                break;
            case 1:
                m_red = q;
                m_green = bri;
                m_blue = p;
                break;
            case 2:
                m_red = p;
                m_green = bri;
                m_blue = t;
                break;
            case 3:
                m_red = p;
                m_green = q;
                m_blue = bri;
                break;
            case 4:
                m_red = t;
                m_green = p;
                m_blue = bri;
                break;
            default: // case 5:
                m_red = bri;
                m_green = p;
                m_blue = q;
                break;
        }
    }
701039
def RGB_to_HSL(cobj, *args, **kwargs):
    
    var_R = cobj.rgb_r
    var_G = cobj.rgb_g
    var_B = cobj.rgb_b

    var_max = max(var_R, var_G, var_B)
    var_min = min(var_R, var_G, var_B)

    var_H = __RGB_to_Hue(var_R, var_G, var_B, var_min, var_max)
    var_L = 0.5 * (var_max + var_min)

    if var_max == var_min:
        var_S = 0
    elif var_L <= 0.5:
        var_S = (var_max - var_min) / (2.0 * var_L)
    else:
        var_S = (var_max - var_min) / (2.0 - (2.0 * var_L))

    return HSLColor(
        var_H, var_S, var_L)
115292
function(h, s, l, a) {
    h = (number(h) % 360) / 360;
    s = number(s); l = number(l); a = number(a);
    if ([h, s, l, a].some(isNaN)) return null;

    var m2 = l <= 0.5 ? l * (s + 1) : l + s - l * s,
    m1 = l * 2 - m2;

    return this.rgba(hue(h + 1 / 3) * 255,
      hue(h) * 255,
      hue(h - 1 / 3) * 255,
      a);

    function hue(h) {
      h = h < 0 ? h + 1 : (h > 1 ? h - 1 : h);
      if (h * 6 < 1) return m1 + (m2 - m1) * h * 6;
      else if (h * 2 < 1) return m2;
      else if (h * 3 < 2) return m1 + (m2 - m1) * (2 / 3 - h) * 6;
      else return m1;
    }
  }
1330851
def set_hsb(self, hue, saturation, brightness=None, *, index=0,
                transition_time=None):
        """"""
        self._value_validate(hue, RANGE_HUE, "Hue")
        self._value_validate(saturation, RANGE_SATURATION, "Saturation")

        values = {
            ATTR_LIGHT_COLOR_SATURATION: saturation,
            ATTR_LIGHT_COLOR_HUE: hue
        }

        if brightness is not None:
            values[ATTR_LIGHT_DIMMER] = brightness
            self._value_validate(brightness, RANGE_BRIGHTNESS, "Brightness")

        if transition_time is not None:
            values[ATTR_TRANSITION_TIME] = transition_time

        return self.set_values(values, index=index)
56650
def _hue(color, **kwargs):
    
    h = colorsys.rgb_to_hls(*[x / 255.0 for x in color.value[:3]])[0]
    return NumberValue(h * 360.0)
887992
def to_hsv
      red, green, blue = *[r, g, b].collect {|x| x / 255.0}
      max = [red, green, blue].max
      min = [red, green, blue].min

      if min == max
        hue = 0
      elsif max == red
        hue = 60 * ((green - blue) / (max - min))
      elsif max == green
        hue = 60 * ((blue - red) / (max - min)) + 120
      elsif max == blue
        hue = 60 * ((red - green) / (max - min)) + 240
      end

      saturation = (max == 0) ? 0 : (max - min) / max
      [hue % 360, saturation, max]
    end
398866
def readin():
    """"""

    selection = sys.stdin.readline().strip("\n")
    MyBulbs.bulbs.sort(key=lambda x: x.label or x.mac_addr)
    lov=[ x for x in selection.split(" ") if x != ""]
    if lov:
        if MyBulbs.boi:
            #try:
            if True:
                if int(lov[0]) == 0:
                    MyBulbs.boi=None
                elif int(lov[0]) == 1:
                    if len(lov) >1:
                        MyBulbs.boi.set_power(lov[1].lower() in ["1","on","true"]) 
                        MyBulbs.boi=None
                    else:
                        print("Error: For power you must indicate on or off\n")
                elif int(lov[0]) == 2:
                    if len(lov) >2:
                        try:
                            MyBulbs.boi.set_color([58275,0, 
                                    int(round((float(lov[1])*65365.0)/100.0)),
                                    int(round(float(lov[2])))])
                            
                            MyBulbs.boi=None
                        except:
                            print("Error: For white brightness (0-100) and temperature (2500-9000) must be numbers.\n")
                    else:
                        print("Error: For white you must indicate brightness (0-100) and temperature (2500-9000)\n")
                elif int(lov[0]) == 3:
                    if len(lov) >3:
                        try:
                            MyBulbs.boi.set_color([int(round((float(lov[1])*65535.0)/360.0)),
                                    int(round((float(lov[2])*65535.0)/100.0)),
                                    int(round((float(lov[3])*65535.0)/100.0)),3500])
                            MyBulbs.boi=None
                        except:
                            print("Error: For colour hue (0-360), saturation (0-100) and brightness (0-100)) must be numbers.\n")
                    else:
                        print("Error: For colour you must indicate hue (0-360), saturation (0-100) and brightness (0-100))\n")
                        
                elif int(lov[0]) == 4: 
                    print(MyBulbs.boi.device_characteristics_str("    "))
                    print(MyBulbs.boi.device_product_str("    "))
                    MyBulbs.boi=None
                elif int(lov[0]) == 5: 
                    print(MyBulbs.boi.device_firmware_str("   "))
                    MyBulbs.boi=None
                elif int(lov[0]) == 6: 
                    mypartial=partial(MyBulbs.boi.device_radio_str)
                    MyBulbs.boi.get_wifiinfo(callb=lambda x,y:print("\n"+mypartial(y)))
                    MyBulbs.boi=None
                elif int(lov[0]) == 7: 
                    mypartial=partial(MyBulbs.boi.device_time_str)
                    MyBulbs.boi.get_hostinfo(callb=lambda x,y:print("\n"+mypartial(y)))
                    MyBulbs.boi=None
                elif int(lov[0]) == 8: 
                    if len(lov) >3:
                        try:
                            print ( "Sending {}".format([int(round((float(lov[1])*65535.0)/360.0)),
                                    int(round((float(lov[2])*65535.0)/100.0)),
                                    int(round((float(lov[3])*65535.0)/100.0)),3500]))
                            MyBulbs.boi.set_waveform({"color":[int(round((float(lov[1])*65535.0)/360.0)),
                                                               int(round((float(lov[2])*65535.0)/100.0)),
                                                               int(round((float(lov[3])*65535.0)/100.0)),
                                                               3500],
                                                      "transient":1, "period":100, "cycles":30,
                                                      "skew_ratio":0,"waveform":0})
                            MyBulbs.boi=None
                        except:
                            print("Error: For pulse hue (0-360), saturation (0-100) and brightness (0-100)) must be numbers.\n")
                    else:
                        print("Error: For pulse you must indicate hue (0-360), saturation (0-100) and brightness (0-100))\n")
            #except:
                #print ("\nError: Selection must be a number.\n")  
        else:
            try:
                if int(lov[0]) > 0:
                    if int(lov[0]) <=len(MyBulbs.bulbs):
                        MyBulbs.boi=MyBulbs.bulbs[int(lov[0])-1]
                    else:
                        print("\nError: Not a valid selection.\n")
            
            except:
                print ("\nError: Selection must be a number.\n")
        
    if MyBulbs.boi:
        print("Select Function for {}:".format(MyBulbs.boi.label))
        print("\t[1]\tPower (0 or 1)")
        print("\t[2]\tWhite (Brigthness Temperature)")
        print("\t[3]\tColour (Hue Saturation Brightness)")
        print("\t[4]\tInfo")
        print("\t[5]\tFirmware")
        print("\t[6]\tWifi")
        print("\t[7]\tUptime")
        print("\t[8]\tPulse")
        print("")
        print("\t[0]\tBack to bulb selection")
    else:
        idx=1
        print("Select Bulb:")
        for x in MyBulbs.bulbs:
            print("\t[{}]\t{}".format(idx,x.label or x.mac_addr))
            idx+=1
    print("")
    print("Your choice: ", end='',flush=True)
275697
function(fillColor) {
            var fillColorHex = this.colorUtils.hexFromColor(fillColor),
                luminanceThreshold = 128,
                darkColor = 'black',
                lightColor = 'white',
                fillLuminance = this.colorUtils.getLuminance(fillColorHex);

            return (fillLuminance < luminanceThreshold) ? lightColor : darkColor;
        }
103022
def hsl_to_rgb(h, s, l)
      h %= 201
      s %= 101
      l %= 101
      if l <= 49
        color_max = 255.0 * (l + l * (s / 100.0)) / 100.0
        color_min = 255.0 * (l - l * (s / 100.0)) / 100.0
      else
        color_max = 255.0 * (l + (100 - l) * (s / 100.0)) / 100.0
        color_min = 255.0 * (l - (100 - l) * (s / 100.0)) / 100.0
      end

      if h < HUE_PER_6
        base = h
      elsif h < HUE_PER_6 * 3
        base = (h - HUE_PER_6 * 2).abs
      elsif h < HUE_PER_6 * 5
        base = (h - HUE_PER_6 * 4).abs
      else
        base = (200 - h)
      end
      base = base / HUE_PER_6 * (color_max - color_min) + color_min

      divide = (h / HUE_PER_6).to_i
      red, green, blue = (case divide
                          when 0 then  [color_max, base, color_min]
                          when 1 then  [base, color_max, color_min]
                          when 2 then  [color_min, color_max, base]
                          when 3 then  [color_min, base, color_max]
                          when 4 then  [base, color_min, color_max]
                          else         [color_max, color_min, base]
                          end)

      [red.round, green.round, blue.round]
    end
859208
def generateDefaultColorRamp(cls, colorRampEnum):
        
        hue = [(255, 0, 255), (231, 0, 255), (208, 0, 255), (185, 0, 255), (162, 0, 255), (139, 0, 255), (115, 0, 255), (92, 0, 255), (69, 0, 255), (46, 0, 255), (23, 0, 255),        # magenta to blue
                   (0, 0, 255), (0, 23, 255), (0, 46, 255), (0, 69, 255), (0, 92, 255), (0, 115, 255), (0, 139, 255), (0, 162, 255), (0, 185, 255), (0, 208, 255), (0, 231, 255),          # blue to cyan
                   (0, 255, 255), (0, 255, 231), (0, 255, 208), (0, 255, 185), (0, 255, 162), (0, 255, 139), (0, 255, 115), (0, 255, 92), (0, 255, 69), (0, 255, 46), (0, 255, 23),        # cyan to green
                   (0, 255, 0), (23, 255, 0), (46, 255, 0), (69, 255, 0), (92, 255, 0), (115, 255, 0), (139, 255, 0), (162, 255, 0), (185, 255, 0), (208, 255, 0), (231, 255, 0),          # green to yellow
                   (255, 255, 0), (255, 243, 0), (255, 231, 0), (255, 220, 0), (255, 208, 0), (255, 197, 0), (255, 185, 0), (255, 174, 0), (255, 162, 0), (255, 151, 0), (255, 139, 0),    # yellow to orange
                   (255, 128, 0), (255, 116, 0), (255, 104, 0), (255, 93, 0), (255, 81, 0), (255, 69, 0), (255, 58, 0), (255, 46, 0), (255, 34, 0), (255, 23, 0), (255, 11, 0),            # orange to red
                   (255, 0, 0)]                                                                                                                                                            # red

        terrain = [(0, 100, 0), (19, 107, 0), (38, 114, 0), (57, 121, 0), (76, 129, 0), (95, 136, 0), (114, 143, 0), (133, 150, 0), (152, 158, 0), (171, 165, 0), (190, 172, 0),                   # dark green to golden rod yellow
                   (210, 180, 0), (210, 167, 5), (210, 155, 10), (210, 142, 15), (210, 130, 20), (210, 117, 25),                                                                                   # golden rod yellow to orange brown
                   (210, 105, 30), (188, 94, 25), (166, 83, 21), (145, 72, 17), (123, 61, 13), (101, 50, 9),                                                                                       # orange brown to dark brown
                   (80, 40, 5), (95, 59, 27), (111, 79, 50), (127, 98, 73), (143, 118, 95), (159, 137, 118),(175, 157, 141), (191, 176, 164), (207, 196, 186), (223, 215, 209), (239, 235, 232),   # dark brown to white
                   (255, 255, 255)]                                                                                                                                                                # white

        aqua = [(150, 255, 255), (136, 240, 250), (122, 226, 245), (109, 212, 240), (95, 198, 235), (81, 184, 230), (68, 170, 225), (54, 156, 220), (40, 142, 215), (27, 128, 210), (13, 114, 205), # aqua to blue
                (0, 100, 200), (0, 94, 195), (0, 89, 191), (0, 83, 187), (0, 78, 182), (0, 72, 178), (0, 67, 174), (0, 61, 170), (0, 56, 165), (0, 50, 161), (0, 45, 157), (0, 40, 153),            # blue to navy blue
                (0, 36, 143), (0, 32, 134), (0, 29, 125), (0, 25, 115), (0, 21, 106), (0, 18, 97), (0, 14, 88), (0, 10, 78), (0, 7, 69), (0, 3, 60), (0, 0, 51)]                                    # navy blue to dark navy blue


        if (colorRampEnum == ColorRampEnum.COLOR_RAMP_HUE):
            return hue

        elif (colorRampEnum == ColorRampEnum.COLOR_RAMP_TERRAIN):
            return terrain

        elif (colorRampEnum == ColorRampEnum.COLOR_RAMP_AQUA):
            return aqua
258787
private static final double[] getRGB(double hue, double saturation, double brightness) {
        double[] rgb = {0.0, 0.0, 0.0};

        double tmpHue = hue * 360.0;
        int hi;
        double f;
        double p, q, t;

        hi = (int)Math.floor(tmpHue / 60.0) % 6;
        f  = tmpHue / 60.0 - hi;
        p  = (brightness * (1.0 - saturation));
        q  = (brightness * (1.0 - saturation * f));
        t  = (brightness * (1.0 - saturation * (1.0 - f)));

        switch (hi) {
        case 0:
            rgb[0] = brightness;
            rgb[1] = t;
            rgb[2] = p;
            break;
        case 1:
            rgb[0] = q;
            rgb[1] = brightness;
            rgb[2] = p;
            break;
        case 2:
            rgb[0] = p;
            rgb[1] = brightness;
            rgb[2] = t;
            break;
        case 3:
            rgb[0] = p;
            rgb[1] = q;
            rgb[2] = brightness;
            break;
        case 4:
            rgb[0] = t;
            rgb[1] = p;
            rgb[2] = brightness;
            break;
        case 5:
            rgb[0] = brightness;
            rgb[1] = p;
            rgb[2] = q;
            break;
        default:
            break;
        }

        return rgb;
    }
913181
public RgbaColor adjustHue(float degrees) {
        float[] HSL = convertToHsl();
        HSL[0] = hueCheck(HSL[0] + degrees); // ensure [0-360)
        return RgbaColor.fromHsl(HSL);
    }