Page 136 - Computational Colour Science Using MATLAB
P. 136
IMPLEMENTATIONS AND EXAMPLES 123
Box 20: rgb2xyz.m
function [XYZ] = rgb2xyz(dacs, gogs, A)
% function [XYZ] = rgb2xyz(dacs, gogs, A)
% converts RGB DACS from a monitor to CIE XYZ
% dacs is a 3 by 1 matrix containing the RGB DACS (0-255)
% gogs is a 2 by 1 matrix containing the gamma and gain
% A is a 3 by 3 matrix to transform RGB to XYZ
dacs = dacs(:)’; % force to be a row matrix
if (length(dacs) *=3)
disp(’DACS must be 3 by 1 or 1 by 3’); return;
end
dacs = dacs/255;
RGB(1) = compgog(gogs(1,:), dacs(1));
RGB(2) = compgog(gogs(2,:), dacs(2));
RGB(3) = compgog(gogs(3,:), dacs(3));
RGB = RGB(:);
XYZ = A*RGB;
The inverse function xyz2rgb is also provided.
Box 21: xyz2rgb.m
function [dacs] = xyz2rgb(XYZ)
% function [dacs] = xyz2rgb(XYZ, gogvals, A)
% converts XYZ to RGB DACS for a monitor
% XYZ is a 3 by 1 matrix containing the XYZ values
% gogvals is a 3 by 2 matrix containing the gamma and gain
% for each of the three channels
% A is a 3 by 3 matrix to transform RGB to XYZ