Function rgb_derivation::matrix::calculate [−][src]
pub fn calculate<K: Scalar>(
white: &[K; 3],
primaries: &[Chromacity<K>; 3]
) -> Result<Matrix<K>, Error<K>> where
&'x K: RefNum<K>,
Calculates change of basis matrix for moving from linear RGB to XYZ colour spaces.
The matrix is calculated from XYZ coordinates of a reference white point and
chromacities of the three primary colours (red, green and blue). (Note that
super::Chromacity::to_xyz
function allows conversion from
chromacity to XYZ coordinates thus the function may be used when only x and
y coordinates of the white point are known).
The result is a three-by-three matrix M such that multiplying it by a column-vector representing a colour in linear RGB space results in a column-vector representing the same colour in XYZ coordinates.
To get the change of basis matrix for moving in the other direction
(i.e. from XYZ colour space to linear RGB space) simply inverse the result.
This scan be done with inversed_copy
function.
Finally, the columns of the result are XYZ coordinates of the primaries. To
get the primaries oriented in rows transposed_copy
function can be used.
Example
use rgb_derivation::*; let white = Chromacity::new(1.0_f32 / 3.0, 1.0 / 3.0).unwrap(); let primaries = [ Chromacity::new(0.735_f32, 0.265_f32).unwrap(), Chromacity::new(0.274_f32, 0.717_f32).unwrap(), Chromacity::new(0.167_f32, 0.009_f32).unwrap(), ]; let matrix = matrix::calculate(&white.to_xyz(), &primaries).unwrap(); let inverse = matrix::inversed_copy(&matrix).unwrap(); let primaries = matrix::transposed_copy(&matrix); assert_eq!([ [0.4887181, 0.31068033, 0.20060167], [0.17620447, 0.8129847, 0.010810869], [0.0, 0.010204833, 0.98979515], ], matrix); assert_eq!([ [ 2.3706737, -0.9000402, -0.47063363], [-0.5138849, 1.4253035, 0.08858136], [ 0.005298177, -0.014694944, 1.0093968], ], inverse); assert_eq!([ [0.4887181, 0.17620447, 0.0], [0.31068033, 0.8129847, 0.010204833], [0.20060167, 0.010810869, 0.98979515], ], primaries);