I'm a programmer looking to build an RGB to HSV color converter. I found an algorithm, but I have very little mathematical background and I'm not quite sure what's going on with it. A step-by-step breakdown of exactly what is happening would be tremendously helpful so that I could code it. RGB and HSV are each sets of three values. R, G, and B are each 0-255, while H is 0-360° and S and V are each 0%-100%.

Here's the algorithm:

The R,G,B values are divided by 255 to change the range from 0..255 to 0..1:

R' = R/255

G' = G/255

B' = B/255

Cmax = max(R', G', B')

Cmin = min(R', G', B')

Δ = Cmax - Cmin

Hue calculation:

Saturation calculation:

Value calculation: V = Cmax



Here are my questions:

Is CMax the average of the three numbers R, G, and B?

Is CMin always equal to zero? If so, wouldn't Delta just be CMax?

What does mod6 mean?

In the calculations for H, there are three lines each with two parts separated by commas. What the heck is going on here?

With S, I have the same gap in understanding as H, and I also don't know what <> means.


As you can see, I basically have no clue what's going on here. Like I said, your help in solving this problem would be extremely useful to me and would be immensely appreciated.

Thank you so much for your time, and hopefully your help as well!


$CMax$ is the largest of $R,G,$ and $B$. $CMin$ is the smallest.

$(\mod 6)$ is the remainder after dividing by $6$. (% operator in C-ish languages)

The commas look to be conditional statements. (e.g. $H = 60 ^\circ ({{G' -B' \over \Delta} \mod 6)} $ if $CMax = R'$)

$<>$ means 'not equal to'.

  • $\begingroup$ Thanks a million, that clears it all up very nicely. $\endgroup$ – jpppp Nov 8 '13 at 2:48
  • $\begingroup$ You're very welcome. $\endgroup$ – shade4159 Nov 8 '13 at 3:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.