1
\$\begingroup\$

I'm trying to use this solution to calculate the secondary coordinates from primaries: https://photo.stackexchange.com/a/75238/115749

But it won't work for any other primary than those in the solution (red and green). The results are wrong.

Here is my adaption to work with colour 0.4.4:

import colour
import numpy as np
from colour.models import RGB_COLOURSPACE_BT709

# Conversion from RGB to chromaticity coordinates.
# Defining RGB values for reference.
RGB_r = np.array([1, 0, 0])
RGB_g = np.array([0, 1, 0])

# We assume they are encoded in *sRGB* colourspace.
XYZ_r = colour.RGB_to_XYZ(RGB_r,RGB_COLOURSPACE_BT709)
XYZ_g = colour.RGB_to_XYZ(RGB_g,RGB_COLOURSPACE_BT709)

# Conversion to chromaticity coordinates.
xy_r = colour.XYZ_to_xy(XYZ_r)
print(xy_r)
# [ 0.64  0.33]

xy_g = colour.XYZ_to_xy(XYZ_g) 
print(xy_g)
# [ 0.3  0.6]

# Conversion to CIE xyY in order to maintain Luminance ratios.
# Using sRGB Luminance ratios, second row of the NPM.
xyY_r = [xy_r[0], xy_r[1], colour.models.RGB_COLOURSPACE_BT709.matrix_RGB_to_XYZ[1, 0]]
xyY_g = [xy_g[0], xy_g[1], colour.models.RGB_COLOURSPACE_BT709.matrix_RGB_to_XYZ[1, 1]]
print(xyY_r)
print(xyY_g)

xy_s = colour.XYZ_to_xy(
    colour.RGB_to_XYZ(
        colour.XYZ_to_RGB(colour.xyY_to_XYZ(xyY_r),RGB_COLOURSPACE_BT709) +
        colour.XYZ_to_RGB(colour.xyY_to_XYZ(xyY_g),RGB_COLOURSPACE_BT709),RGB_COLOURSPACE_BT709))
print(xy_s)

So if I change here to any other primary the coordinates are wrong:

RGB_r = np.array([1, 0, 0])
RGB_g = np.array([0, 1, 0])
  • Yellow ([1, 0, 0] + [0, 1, 0]) --> [ 0.419306 0.505257] correct
  • Cyan ([0, 1, 0] + [0, 0, 1]) --> [ 0.15433113 0.07559207] not correct
  • Magenta ([1, 0, 0] + [0, 0, 1]) --> [ 0.17513059 0.07384747] not correct
\$\endgroup\$
3
  • \$\begingroup\$ I’m voting to close this question because it is not about the photography: photo.stackexchange.com/help/on-topic \$\endgroup\$ Mar 27 at 21:13
  • \$\begingroup\$ @RomeoNinov the link to the solution I used is on the same topic ... \$\endgroup\$
    – FLX
    Mar 28 at 9:59
  • \$\begingroup\$ I cannot vote to NOT close this question because apparently more than 15 years are needed to implement this feature. \$\endgroup\$ Mar 29 at 19:17

2 Answers 2

2
\$\begingroup\$

I don't why I had to use those calculations over the matrix. It was much more simple.

Here is the code to get everything by hand for BT.709 colourspace:

import colour
import numpy as np
from colour.models import RGB_COLOURSPACE_BT709


RGB_r = np.array([1, 0, 0])
RGB_g = np.array([0, 1, 0])
RGB_b = np.array([0, 0, 1])
RGB_y = np.array([1, 1, 0])
RGB_c = np.array([0, 1, 1])
RGB_m = np.array([1, 0, 1])


# RGB_COLOURSPACE_BT709
XYZ_r = colour.RGB_to_XYZ(RGB_r,RGB_COLOURSPACE_BT709)
xy = colour.XYZ_to_xy(XYZ_r)
print("RGB_COLOURSPACE_BT709_RED: ", xy)

XYZ_g = colour.RGB_to_XYZ(RGB_g,RGB_COLOURSPACE_BT709)
xy = colour.XYZ_to_xy(XYZ_g)
print("RGB_COLOURSPACE_BT709_GREEN: ", xy)

XYZ_b = colour.RGB_to_XYZ(RGB_b,RGB_COLOURSPACE_BT709)
xy = colour.XYZ_to_xy(XYZ_b)
print("RGB_COLOURSPACE_BT709_BLUE: ", xy)

XYZ_y = colour.RGB_to_XYZ(RGB_y,RGB_COLOURSPACE_BT709)
xy = colour.XYZ_to_xy(XYZ_y)
print("RGB_COLOURSPACE_BT709_YELLOW: ", xy)

XYZ_c = colour.RGB_to_XYZ(RGB_c,RGB_COLOURSPACE_BT709)
xy = colour.XYZ_to_xy(XYZ_c)
print("RGB_COLOURSPACE_BT709_CYAN: ", xy)

XYZ_m = colour.RGB_to_XYZ(RGB_m,RGB_COLOURSPACE_BT709)
xy = colour.XYZ_to_xy(XYZ_m)
print("RGB_COLOURSPACE_BT709_MAGENTA: ", xy)


xy = np.array([0.3127, 0.3290])
#https://en.wikipedia.org/wiki/Correlated_color_temperature#Calculation
u = (4*0.3127) / (-2*0.3127 + 12*0.3290 + 3)
v = (6*0.3290) / (-2*0.3127 + 12*0.3290 + 3)
uv = np.array([u, v])
print("Whitepoint: ", xy)
print("CCT: ", colour.uv_to_CCT(uv, 'Ohno 2013')[0])

>RGB_COLOURSPACE_BT709_RED:  [ 0.64  0.33]
>RGB_COLOURSPACE_BT709_GREEN:  [ 0.3  0.6]
>RGB_COLOURSPACE_BT709_BLUE:  [ 0.15  0.06]
>RGB_COLOURSPACE_BT709_YELLOW:  [ 0.419306  0.505257]
>RGB_COLOURSPACE_BT709_CYAN:  [ 0.22464749  0.32873097]
>RGB_COLOURSPACE_BT709_MAGENTA:  [ 0.32089334  0.15416572]
>Whitepoint:  [ 0.3127  0.329 ]
>CCT:  6504.314849
\$\endgroup\$
0
\$\begingroup\$

It seems very likely to me that the problem is with xyY_r = and xyY_g = calculations. It's very obvious that it only works for finding yellow.
For other colours the subscripts of RGB_to_XYZ_matrix must be different.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.