How to convert from RGB to HSV
The maximum value of R, G, and B is MAX, and the minimum value is MIN.
MAX=max{R,G,B}
MIN=min{R,G,B}
Hue
The calculation method changes depending on whether MAX is R, G, or B.
H=⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧MAX−MING−B×60MAX−MINB−R×60+120MAX−MINR−G×60+240, MAX=R, MAX=G, MAX=B
Saturation
S=MAXMAX−MIN×100
Value
S=255MAX×100
How to convert from RGB to HSL
The definitions of MAX and MIN are the same as HSV.
Hue
Same as HSV.
Lightness
L=2MAX+MIN×255100
Saturation
The calculation method changes depending on the Lightness.
S=⎩⎪⎪⎪⎪⎨⎪⎪⎪⎪⎧MAX+MINMAX−MIN×100510−(MAX+MIN)MAX−MIN×100, 0≦L≦50, 51≦L≦100
How to convert from HSV to RGB
H is 0 for 360.
H={H0, H=360, H=360
Find the remainder (= decimal part) by dividing H / 60 by 1.
e.g. When H is 90: 6090mod1=1.5mod1=0.5
H′=60Hmod1
Convert S and V from percentages to decimals.
S′=100S
V′=100V
The value of H determines the solution. The exception is achromatic color (S = 0).
A=V′×255
B=V′×(1−S′)×255
C=V′×(1−S′×H′)×255
D=V′×(1−S′×(1−H′))×255
(R,G,B)=⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧(A,A,A)(A,D,B)(C,A,B)(B,A,D)(B,C,A)(D,B,A)(A,B,C), S=0, 0≦H<60, 60≦H<120, 120≦H<180, 180≦H<240, 240≦H<300, 300≦H<360
How to convert from HSL to RGB
H is 0 for 360.
H={H0, H=360, H=360
Apply magic to L.
L′={L100−L, 0≦L<50, 50≦L≦100
The value of H determines the solution.
MAX=2.55×(L+L′×100S)
MIN=2.55×(L−L′×100S)
f(x)=60x×(MAX−MIN)+MIN
(R,G,B)=⎩⎪⎪⎪⎪⎪⎪⎪⎪⎪⎨⎪⎪⎪⎪⎪⎪⎪⎪⎪⎧(MAX, f(H), MIN)(f(120−H), MAX, MIN)(MIN, MAX, f(H−120))(MIN, f(240−H), MAX)(f(H−240), MIN, MAX)(MAX, MIN, f(360−H)), 0≦H<60, 60≦H<120, 120≦H<180, 180≦H<240, 240≦H<300, 300≦H<360