fork download
  1. import numpy
  2.  
  3. # RGB in 0..1; YCbCr in 8-bit space
  4. RGB_to_YCbCr = numpy.array([
  5. [ 65.481, 128.553, 24.966, 16],
  6. [ -37.797, -74.203, 112.000, 128],
  7. [ 112.000, -93.786, -18.214, 128],
  8. [ 0, 0, 0, 1],
  9. ])
  10.  
  11. # white, yellow, cyan, green, magenta, red, blue, black
  12. Rs = [255, 255, 0, 0, 255, 255, 0, 0]
  13. Gs = [255, 255, 255, 255, 0, 0, 0, 0]
  14. Bs = [255, 0, 255, 0, 255, 0, 255, 0]
  15.  
  16. for R, G, B in zip(Rs, Gs, Bs):
  17. Y, Cb, Cr, _ = RGB_to_YCbCr @ [R / 255, G / 255, B / 255, 1]
  18. print(f"{int(round(Y)):>3} {int(round(Cb - 128)):>4} {int(round(Cr - 128)):>4}")
Success #stdin #stdout 0.2s 27268KB
stdin
Standard input is empty
stdout
235    0    0
210 -112   18
170   38 -112
145  -74  -94
106   74   94
 81  -38  112
 41  112  -18
 16    0    0