Color transformation

Tweet


RGB

RGB represents the intensity of red, green, blue, respectively, and the value is 0-255 of integer or 0-1 real. For the following, I don't write down whether the value is integer or real, so please guess by yourself.

RGB<-->CMY

CMY represents Cyan, Magenta, Yellow, respectively. R and C, G and M, B and Y are complementary color.

C = 1-R
M = 1-G
Y = 1-B

R = 1-C
G = 1-M
B = 1-Y

RGB<-->CMYK

K is black.

Black=minimum(1-Red,1-Green,1-Blue)
Cyan=(1-Red-Black)/(1-Black)
Magenta=(1-Green-Black)/(1-Black)
Yellow=(1-Blue-Black)/(1-Black)

Red=1-minimum(1,Cyan*(1-Black)+Black)
Green=1-minimum(1,Magenta*(1-Black)+Black)
Blue=1-minimum(1,Yellow*(1-Black)+Black)

RGB<-->HSV

H: Hue angle 0-360 degree. Red is 0 degree, yellow is 60 degree, green is 120 degree, cyan is 180 degree, blue is 240 degree, magenta is 300 degree, and goes back to red at 360 degree.
S: Saturation 0-1 real. 0 pale (no saturation), 1 deep (has saturation).
V: Value 0-1 real. 0 weak, 1 strong. 0 black painted, 1 black paint not added. A bit similar to the Lightness.

The interger part of H/60 is i, and the decimal of it is f.
p1=V*(1-S)
p2=V*(1-S*f)
p3=V*(1-S*(1-f))
i=0: R=V, G=p3, B=p1
i=1: R=p2, G=V, B=p1
i=2: R=p1, G=V, B=p3
i=3: R=p1, G=p2, B=V
i=4: R=p3, G=p1, B=V
i=5: R=V, G=p1, B=p2

cmax=maximum(R, G, B)
cmin=minimum(R, G, B)
V=cmax
S=(cmax-cmin)/cmax (If cmax==0 then S=0)
R==cmax: H=60*{(G-B)/(cmax-cmin)}
G==cmax: H=60*{2+(B-R)/(cmax-cmin)}
B==cmax: H=60*{4+(R-G)/(cmax-cmin)}
If H<0 then add 360 into H.
If S==0 then H=0.

RGB<-->HLS

H: Hue angle 0-360 degree. Same as H in HSV.
L: Lightness 0-1 real. 0 dark, 1 bright. 0-0.5 added black paintings, 0.5-1 added white paintings. 0.5 added neither of them.
S: Saturation 0-1 real. Same as S in HSV.

L<=0.5: cmin=L*(1-S), cmax=2*L-cmin
L>0.5: cmax=L*(1-S)+S, cmin=2*L-cmax
R=f(H+120)
G=f(H)
B=f(H-120)
Where,
f(h)=
h<60: cmin+(cmax-cmin)*h/60
h>=60 && h<180: cmax
h>=180 && h<240: cmin+(cmax-cmin)*(240-h)/60
h>=240: cmin

cmax=maximum(R, G, B)
cmin=minimum(R, G, B)
L=(cmax+cmin)/2
cmax-cmin==0: S=0, H=0
L<=0.5: S=(cmax-cmin)/(cmax+cmin)
L>0.5: S=(cmax-cmin)/(2-(cmax+cmin))
R==cmax: H=60*{(G-B)/(cmax-cmin)}
G==cmax: H=60*{2+(B-R)*(cmax-cmin)}
B==cmax: H=60*{4+(R-G)*(cmax-cmin)}
If H<0 then add 360 into H.

RGB<-->CIE

CIE is the color model of the chromaticity diagram determined by Commission Internationale de l'Eclairage in 1931. The name of this color space determined by JIS is JIS Z 8701.

x: 0-1 chromaticity
y: chromaticity (0)-1
Y: intensity. 0 weak, 1 strong. 0 added black paint, 1 not added black paint.

R=2.739386694386690*x*Y/y-1.144708939708940*Y-0.424074844074844*(1-x-y)*Y/y
G=-1.118985713198160*x*Y/y+2.028500773974170*Y+0.033144618976324*(1-x-y)*Y/y
B=0.137976247723133*x*Y/y-0.333450588949605*Y+1.104800777170610*(1-x-y)*Y/y

X=0.478*R+0.299*G+0.175*B
Y=0.263*R+0.655*G+0.081*B
Z=0.020*R+0.160*G+0.908*B
W=X+Y+Z
x=X/W
y=Y/W
Y=y/W

YIQ

YIQ is the color model for color TV broadcast in NTSC(National Television System Committee). Y signal is for 0-4MHz band, I signal is for 1.5MHz band, Q signal is for 0.6MHz band. Y transforms the color image into gray image. (Compatible with black/white TV.)

Y: intensity
I: color tone from orange to cyan, including the flesh color.
Q: color tone other than I.

The following is copied from many web pages.

RGB<-->YIQ (No. 1)

R=Y+0.956*I+0.623*Q
G=Y-0.272*I-0.648*Q
B=Y-1.105*I+0.705*Q

Y=0.299*R+0.587*G+0.114*B
I=0.596*R-0.274*G-0.322*B
Q=0.211*R-0.522*G+0.311*B

RGB<-->YIQ (No. 2)

R=Y+0.956*I+0.621*Q
G=Y-0.272*I-0.647*Q
B=Y-1.105*I+0.702*Q

Y=0.299*R+0.587*G+0.114*B
I=0.596*R-0.274*G-0.322*B
Q=0.212*R-0.523*G+0.311*B

RGB<-->YIQ (No. 3)

R=Y+0.9489*I+0.6561*Q
G=Y-0.2645*I-0.6847*Q
B=Y-1.11270*I+1.8050*Q

Y=0.2990*R+0.5870*G+0.1140*B
I=0.5959*R-0.2750*G-0.3210*B
Q=0.2065*R-0.4969*G+0.2904*B

YUV, YCbCr, YCC

These are all similar color model. Many definitions can be found on web, thus I don't know which one is correct. YUV is maybe different from YCbCr, but in many cases, YUV and YCbCr is treated as the same. YCC is YCbCr. The following is copied from many web pages.

Y: intensity
U(Cb): bluishness
V(Cr): reddishness
CENTER: 0 or 0.5 or 128(0x80)

RGB<-->YCC(YCbCr)

Y = 0.29900*R + 0.58700*G + 0.11400*B
Cb = -0.16874*R - 0.33126*G + 0.50000*B + CENTER
Cr = 0.50000*R - 0.41869*G - 0.08131*B + CENTER

R = Y + 1.402*(Cr-CENTER)
G = Y - 0.34414*(Cb-CENTER) - 0.71414*(Cr-CENTER)
B = Y + 1.772*(Cb-CENTER)

RGB<-->YUV

Y = 0.299*R + 0.587*G + 0.114*B
U = -0.147*R - 0.289*G + 0.437*B + CENTER
V = 0.615*R - 0.515*G - 0.100*B + CENTER

R = Y + 0.000*(U-CENTER) + 1.140*(V-CENTER)
G = Y - 0.394*(U-CENTER) - 0.581*(V-CENTER)
B = Y + 2.028*(U-CENTER) + 0.000*(V-CENTER)

RGB<-->YCbCr

Y = 0.2989*R + 0.5866*G + 0.1145*B
Cb = -0.1687*R - 0.3312*G + 0.5000*B + CENTER
Cr = 0.5000*R - 0.4183*G - 0.0816*B + CENTER

R = Y + 0.0000*(Cb-CENTER) + 1.4022*(Cr-CENTER)
G = Y - 0.3456*(Cb-CENTER) - 0.7145*(Cr-CENTER)
B = Y + 1.7710*(Cb-CENTER) + 0.0000*(Cr-CENTER)

RGB<-->YCbCr

The above definition causes some error. The following is the definition of CCIR601, CCIR601-1(D65).

Y = 0.299*R + 0.587*G + 0.114*B
Cb = -0.172*R - 0.339*G + 0.511*B + CENTER
Cr = 0.511*R - 0.428*G - 0.083*B + CENTER

R = Y + 0.000*(Cb-CENTER) + 1.371*(Cr-CENTER)
G = Y - 0.336*(Cb-CENTER) - 0.698*(Cr-CENTER)
B = Y + 1.732*(Cb-CENTER) + 0.000*(Cr-CENTER)

RGB<-->YUV

Y = 0.299*R + 0.587*G + 0.114*B
U = -0.147*R - 0.289*G + 0.436*B + CENTER
V = 0.615*R - 0.515*G - 0.100*B + CENTER

R = Y + 0.000*(U-CENTER) + 1.140*(V-CENTER)
G = Y - 0.396*(U-CENTER) - 0.581*(V-CENTER)
B = Y + 2.029*(U-CENTER) + 0.000*(V-CENTER)

RGB<-->YCbCr

Y = 0.299*R + 0.587*G + 0.114*B
Cb = -0.1687*R - 0.3313*G + 0.5*B + CENTER
Cr = 0.5*R - 0.4187*G - 0.0813*B + CENTER

R = Y + 0.000*(Cb-CENTER) + 1.402*(Cr-CENTER)
G = Y - 0.34414*(Cb-CENTER) - 0.71414*(Cr-CENTER)
B = Y + 1.772*(Cb-CENTER) + 0.000*(Cr-CENTER)

RGB<-->YCbCr

CCIR 601-1

Y = 0.2990*R + 0.5870*G + 0.1140*B
Cb = -0.1687*R - 0.3313*G + 0.5000*B + CENTER
Cr = 0.5000*R - 0.4187*G - 0.0813*B + CENTER

R = Y + 0.0000*(Cb-CENTER) + 1.4020*(Cr-CENTER)
G = Y - 0.3441*(Cb-CENTER) - 0.7141*(Cr-CENTER)
B = Y + 1.7720*(Cb-CENTER) + 0.0000*(Cr-CENTER)

RGB<-->YUV

Y = 0.2990*R + 0.5870*G + 0.1140*B
U = -0.1684*R - 0.3316*G + 0.5000*B + CENTER
V = 0.5000*R - 0.4187*G - 0.0813*B + CENTER

R = Y + 0.0000*(U-CENTER) + 1.4020*(V-CENTER)
G = Y - 0.3441*(U-CENTER) - 0.7139*(V-CENTER)
B = Y + 1.7718*(U-CENTER) + 0.0012*(V-CENTER)


Back