# How to convert RGB into HSBK?

As I’m getting started with Lightsd, I wanted to find a way to convert an RGB color into HSBK. As I understand it, the HTTP api automatically converts RGB colors into HSBK? How can we do this ourselves?

Thank you.

1 Like

There’s a Wikipedia page which explains how to convert from RGB to HSB. (Except that Wikipedia calls it HSV instead of HSB, but it’s the same thing.) Just translate the equations into code in your programming language of choice.

I’ve written it in Haskell, but unless you know Haskell, you’re probably better off going from the Wikipedia page:

``````rgbToHsbk :: Int -> Int -> Int -> PartialColor
rgbToHsbk r' g' b' =
let
rgb = map ((/ 255) . fromIntegral) [r', g', b']
[r, g, b] = rgb
-- https://en.wikipedia.org/wiki/HSL_and_HSV#Hue_and_chroma
mx = maximum rgb
mn = minimum rgb
c = mx - mn
wrap x = if x < 0 then x + 6 else x
h' = if | c == 0 -> 0
| mx == r   -> wrap ((g - b) / c)
| mx == g   -> 2 +  ((b - r) / c)
| otherwise -> 4 +  ((r - g) / c)
h = 60 * h'
-- https://en.wikipedia.org/wiki/HSL_and_HSV#Lightness
v = mx
-- https://en.wikipedia.org/wiki/HSL_and_HSV#Saturation
s = if c == 0 then 0 else c / v
in HSBK (Just h) (Just s) (Just v) Nothing
``````
4 Likes

Thanks for the info. I suppose the Kelvin value is not of great importance unless it’s for whites?

Right. I believe that if you set an RGB color via the HTTP API, it leaves the Kelvin value unchanged.

Or another way to think about it is, for an RGB value of (255, 255, 255), what color of white would you like that to represent?

1 Like

Figured it out, seems fairly straightforward. Checked with the HTTP value and its converted correctly. thanks for your help.