This is a short introduction on the origin of RGB->YUV matrix coefficients
In a 3x3 RGB to YUV matrix, it is enough to know the RGB to Y conversion coefficients. The coefficients for U and V can be derived from them. The U component corresponds to the difference between B and Y, while the V component corresponds to the difference between R and Y. umax and vmax are generaly set to 0.5 to normalize the range of U and V to [-0.5, 0.5].
Python function to convert Y coeffs (wr, wg, wb) to RGB to YUV matrix
Now that we know how to compute U, V coefficients from Y coefficients, where do we get Y coefficients from?
Often, x and y of red green and blue and a white point (eg D65) are specified in specs such as BT 709. Given the 6 primary chromaticity numbers and the 2 white point numbers, you can plug them in here and get your RGB->XYZ matrix. The mathematical details of the conversion can be found here. Now the middle row of this matrix converts from RGB to Y, and this Y is the Y of YUV color space. So you can get the coefficients of Y from here given primary chromaticities
Python code to compute RGB->XYZ matrix given primary chromaticities and white point
BT 709 color spec is defined by these 3 x, y numbers for R, G and B and the white point (D65) as shown in this table here.
Now the D65 white point can be written as: D65_xyz = [0.31271, 0.32902, 0.35827]
Converting xyz to XYZ (keeping Y=1) we get the D65 white point in XYZ terms: D65_XYZ = [D65_xyz[0]/D65_xyz[1], 1, (1 - D65_xyz[0] - D65_xyz[1]) / D65_xyz[1]]
Now we can call out function like this: get_rgb_to_xyz_mtx([0.64,0.33], [0.29,0.6], [0.15,0.06], D65_XYZ)
RGB->XYZ matrix using BT 709 chromaticities
Now taking the middle row, we get the coefficients for Y: 0.212 * R + 0.715 * G + 0.072 * B.
Passing them to our RGB->YUV matrix computing function (with umax, vmax = 0.5) as: compute_rgb_to_yuv(0.21263682, 0.71518298, 0.0721802, 0.5, 0.5)
RGB->YUV matrix for BT 709 from Y coefficients of RGB->XYZ matrix
Screen shot of BT 709 spec pdf