File: RGBToXYZMatrix.m

package info (click to toggle)
psychtoolbox-3 3.0.19.14.dfsg1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 86,796 kB
  • sloc: ansic: 176,245; cpp: 20,103; objc: 5,393; sh: 2,753; python: 1,397; php: 384; makefile: 193; java: 113
file content (48 lines) | stat: -rw-r--r-- 1,795 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
function M = RGBToXYZMatrix(xr, yr, xg, yg, xb, yb, xw, yw)
% M = RGBToXYZMatrix(xr, yr, xg, yg, xb, yb, xw, yw)
%
% Build a 3x3 color space conversion (CSC) matrix for converting R,G,B
% linear color value vectors into the XYZ color space, by a matrix-vector
% multiplication, ie. xyz = M * rgb;
%
% The source color space for the rgb (R,G,B) vector is defined by its
% color gamut, which in turn is specified by the 2D CIE-1931 chromaticity
% coordinates of the white-point (xw, yw) and the color primaries red,
% green and blue (xr, yr), (xg, yg) and (xb, yb).
%
% See also the function XYZToRGBMatrix() for building the inverse matrix
% for mapping from XYZ space to a target RGB color space.
%
% This code is based on the mathematical derivations and sample code from
% Gernot Hoffmann, from: http://docs-hoffmann.de/ciexyz29082000.pdf
%
% The math is consistent afaics with the one derived by Bruce Lindbloom:
% http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
%
% Also with http://www.ryanjuckett.com/programming/rgb-color-space-conversion
%
% This Matlab code delivers the same results as Screen()'s internal C
% implementation for color space conversion during movie playback, and was
% also spot-checked against publically available reference matrices for the
% sRGB color space, e.g., Wikipedia, Matlab / Octave, esp. Bruce Lindbloom:
% http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html
%
% So there's some confidence this is not totally wrong ;) - But i'm not a
% color scientist, so buyers beware!
%

% History:
%
% 30-Sep-2020   mk  Written.

    zr = 1 - xr - yr;
    zg = 1 - xg - yg;
    zb = 1 - xb - yb;
    zw = 1 - xw - yw;

    W = [xw; yw; zw];
    P = [xr xg xb; yr yg yb; zr zg zb];
    u = P \ W;
    D = diag(u / yw);
    M = P * D;
end