Color conversions
Work under construction. to do: http://forum.doom9.org/showthread.php?p=1084293#post1084293
Should cover RGB->YUV conversions and lumarange scaling/preservation and when to use which conversion (latter in a different page).
YUV ranges for valid RGB ranges: http://forum.doom9.org/showthread.php?t=154731
blabla ...
Contents |
Color coefficients
The most common coefficient and the conversions are given below:
coefficients | Rec.601 | Rec.709 | FCC |
---|---|---|---|
Kr : Red channel coefficient | 0.299 | 0.2126 | 0.3 |
Kg : Green channel coefficient | 0.587 | 0.7152 | 0.59 |
Kb : Blue channel coefficient | 0.114 | 0.0722 | 0.11 |
The RGB <-> YUV conversion is given by:
Y = Kr*R + Kg*G + Kb*B V = (R-Y)/(1-Kr) = R - G * Kg/(1-Kr) - B * Kb/(1-Kr) U = (B-Y)/(1-Kb) = - R * Kr/(1-Kb) - G * Kg/(1-Kb) + B R = Y + V*(1-Kr) G = Y - U*(1-Kb)*Kb/Kg - V*(1-Kr)*Kr/Kg B = Y + U*(1-Kb)
with (0.0 <= [Y,R,G,B] <= 1.0) ; (-1.0 <= [U,V] <= 1.0) and Kr + Kg + Kb = 1.
Converting to programming values
color format | channel 1 | channel 2 | channel 3 |
---|---|---|---|
yuv [0,255] | y = Y * 255 | v = V * 127.5 + 128 | u = U * 127.5 + 128 |
yuv [16,235] | y = Y * 219 + 16 | v = V * 112 + 128 | u = U * 112 + 128 |
rgb [0,255] | r = R * 255 | g = G * 255 | b = B * 255 |
rgb [16,235] | r = R * 219 + 16 | g = G * 219 + 16 | b = B * 219 + 16 |
The following two conversions are implemented in AviSynth:
yuv [0,255] <-> rgb [0,255] (0 <= [r,g,b] <= 255, 0 <= y <= 255, 0 <= [u,v] <= 255)
Substituting (Y,V,U,R,G,B) in the equations above and multiplying with 127.5 and respectively 255 gives
y = Kr*r + Kg*g + Kb*b
v - 128 = 0.5*(r-y)/(1-Kr) = 0.5 * r - 0.5 * g * Kg/(1-Kr) - 0.5 * b * Kb/(1-Kr)
u - 128 = 0.5*(b-y)/(1-Kb) = - 0.5 * r * Kr/(1-Kb) - 0.5 * g * Kg/(1-Kb) + 0.5 * b
r = y + 2*(v-128)*(1-Kr)
g = y - 2*(u-128)*(1-Kb)*Kb/Kg - 2*(v-128)*(1-Kr)*Kr/Kg
b = y + 2*(u-128)*(1-Kb)
yuv [16,235] <-> rgb [0,255] (0 <= [r,g,b] <= 255, 16 <= y <= 235, 16 <= [u,v] <= 240)
Substituting (Y,V,U,R,G,B) in the equations above gives
y - 16 = (Kr*219/255)*r + (Kg*219/255)*g + (Kb*219/255)*b v - 128 = 112/255*r - Kg*112/255*g/(1-Kr) - Kb*112/255*b/(1-Kr) u - 128 = - Kr*112/255*r/(1-Kb) - Kg*112/255*g/(1-Kb) + 112/255*b r = (255/219)*y + (255/112)*v*(1-Kr) - (255*16/219 + 255*128/112*(1-Kr)) g = (255/219)*y - (255/112)*u*(1-Kb)*Kb/Kg - (255/112)*v*(1-Kr)*Kr/Kg - (255*16/219 - 255/112*128*(1-Kb)*Kb/Kg - 255/112*128*(1-Kr)*Kr/Kg) b = (255/219)*y + (255/112)*u*(1-Kb) - (255*16/219 + 255*128/112*(1-Kb))
Examples
As an example, the colors black, white and yellow will be computed in various color formats:
In RGB [0,255] black is given by r=0, g=0, b=0. It follows that R=0, G=0, B=0. Calculating the Y, V and U values using the Rec.601 coefficients gives: Y=0, V=0 and U=0. In YUV [16,235] this corresponds to y=16, v=128, u=128.
In RGB [0,255] white is given by r=255, g=255, b=255. It follows that R=1, G=1, B=1. Calculating the Y, V and U values using the Rec.601 coefficients gives: Y=1, V=0 and U=0. In YUV [16,235] this corresponds to y=235, v=128, u=128.
In RGB [0,255] yellow is given by r=255, g=255, b=0. It follows that R=1, G=1, B=0. Calculating the Y, V and U values using the Rec.601 coefficients gives: Y = Kr*R + Kg*G + Kb*B = 0.299*1 + 0.587*1 + 0.114*0 = 0.886, V = (R-Y)/(1-Kr) = (1-0.886)/(1-0.299) = 0.1626 and U = (B-Y)/(1-Kb) = (0-0.886)/(1-0.114) = -1.000. In YUV [16,235] this corresponds to y=210, v=146, u=16. In YUV [0,255] this corresponds to y=226, v=149, u=0.
For other examples, see ColorBars theory.
References:
ColorFAQ Poynton
ITU BT.601 ...
ITU BT.709 ...
ITU BT.1700 ...