creeser
creeser

Reputation: 421

convert rgb to hsbk as required by lifx

There are many examples of converting hex colors to other formats. But, none to or from the Lifx hsbk format. My trials all give a darker color than what it should be. An hsbk of a red from a lifx bulb is: 0 =1059, 1 =43956,2 =24600,3 =3500: giving hex of '#2b110e'. When sent to a button background in html, it is far to dark.

def getHex(self):  # used to set the color picker background on startup
    try:
        hsbk = self.cur_color  # hsbk: (hue, saturation, brightness, kelvin)
        hue, saturation, brightness, kelvin = hsbk
        
        # Convert Hue (0-65535) to a value between 0 and 1 for RGB conversion
        hue = hue / 65535.0
        # Convert Saturation and Brightness (0-65535) to values between 0 and 1
        saturation = saturation / 65535.0
        brightness = brightness / 65535.0

        # Ensure brightness is factored in the RGB conversion
        r, g, b = self.hsbk_to_rgb(hue, saturation, brightness)
        
        # Convert RGB values (0-255) to hex f66151
        hex_color = f'#{r:02x}{g:02x}{b:02x}'
        return jsonify({'color': hex_color})
    except Exception as e:
        return jsonify({'error': str(e)}), 500

Upvotes: 0

Views: 50

Answers (0)

Related Questions