Instantly share code, notes, and snippets.

# yohhoy/yuvrgb.md

Last active June 11, 2024 03:44
Show Gist options
• Save yohhoy/dafa5a47dade85d8b40625261af3776a to your computer and use it in GitHub Desktop.
RGB <=> YCbCr(YPbPr) color space conversion
``````Y  = a * R + b * G + c * B
Cb = (B - Y) / d
Cr = (R - Y) / e
``````
BT.601 BT.709 BT.2020
a 0.299 0.2126 0.2627
b 0.587 0.7152 0.6780
c 0.114 0.0722 0.0593
d 1.772 1.8556 1.8814
e 1.402 1.5748 1.4746
``````R = Y + e * Cr
G = Y - (a * e / b) * Cr - (c * d / b) * Cb
B = Y + d * Cb
``````

### Vanilagy commented Oct 27, 2022

Color spaces are the most niche, rabbit holey thing ever, I swear

### danigc98 commented Apr 14, 2023

They really are. I feel I will never gain my time back from learning this stuff.

### zhanwang-sky commented Jun 7, 2023

e for BT.2020 should be 1.4746, it can be calculated by the following formula:
e = (1 - a) / 0.5,
d = (1 - c) / 0.5

### yohhoy commented Jun 8, 2023

e for BT.2020 should be 1.4746,

Fixed. thanks!

### molesmoke commented Jan 17, 2024

Looks like Cb/Cr should be centred around 0.5?

``````Y  = a * R + b * G + c * B
Cb = (B - Y) / d  + 0.5
Cr = (R - Y) / e + 0.5
``````

### yohhoy commented Jan 17, 2024

Chrominance(Chroma) Cb/Cr are defined in the range [-0.5, 0.5].