# RGB to HSV Color Conversion Algorithm

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!

• Hey what is max and min there? Sorry if it is painfully obvious, I am just beginning in this. Commented Nov 28, 2015 at 20:44
• Here is a better link to the hue calculation: rapidtables.com/convert/color/rgb-to-hsv/hue-calc2.gif Commented Apr 15, 2018 at 20:18
• Here is the site with the formulas: rapidtables.com/convert/color/rgb-to-hsv.html Commented Apr 15, 2018 at 20:18
• For any computer scientest reading this, like myself, I believe Mod 6 is short for Modulus 6, which in sudo code, the following equation x Mod 6 = r is the same as the following pseudo function int foo(x: int) { return X % 6; }. (or in a dynamic language, function foo(x){ return x % 6}) Commented Jul 18, 2022 at 10:29

$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'.

• Thanks a million, that clears it all up very nicely. Commented Nov 8, 2013 at 2:48
• You're very welcome. Commented Nov 8, 2013 at 3:43

Well, I have been searching for the same algorithm for months! I actually did not get my algorithm from Wikipedia, I got it from GitHub$$^1$$. Anyways, here is my formula:
$$r,g,b = \frac{r'}{255},\frac{g'}{255},\frac{b'}{255}$$ $$M = \max(r,g,b)$$ $$m = \min(r,g,b)$$ $$c = M - m$$ $$s = (\frac{c}{M})100$$ $$R, G, B = \frac{M-r}{c},\frac{M-g}{c},\frac{M-b}{c}$$
$$h' =$$ $$0$$: if M = m
$$0+B-G$$: if M = r
$$2+R-B$$: if M = g
$$4+G-R$$: if M = b

$$h = (\frac{h'}{6}\mod{1})360$$ $$v = M100$$

$$^1$$: https://github.com/python/cpython/blob/3.9/Lib/colorsys.py

This is my code version (python):

def rgb_to_hsv(r, g, b):
r /= 255
g /= 255
b /= 255
maxc = max(r, g, b)
minc = min(r, g, b)
v = maxc
if minc == maxc:
return 0.0, 0.0, v
s = (maxc-minc) / maxc
rc = (maxc-r) / (maxc-minc)
gc = (maxc-g) / (maxc-minc)
bc = (maxc-b) / (maxc-minc)
if r == maxc:
h = 0.0+bc-gc
elif g == maxc:
h = 2.0+rc-bc
else:
h = 4.0+gc-rc
h = (h/6.0) % 1.0
return h * 360, s * 100, v * 100

• I don't see how % 1.0 would make any difference. Commented Jan 19, 2022 at 17:02
• Because the other number is likely not a whole number. Commented Jul 23, 2022 at 2:41
• @tcurdt Apparently it's used to map back the negative numbers. So for example Magenta is (Max, 0, Max) so gc = 1 and bc= 0 and h would be -1 after the if conditions. Due to h/6 it would be -1/6 and the %1 makes it +5/6. Apart from that it's likely not going to do much as all numbers are between 0 and 1 anyway. Commented Oct 26, 2022 at 12:52