function XYZd=adaptation(D,As,Ad,XYZs)
% Predicts how tristimulus values will change when moving from
% one illuminant to another, given the white points of the two
% illuminants.

% D is an nxn array of finite differencing constants, obtained
%        from function adaptation_prep_XYZ or adaptation_prep_xy.
%        (n is the number of wavelength bins (rows) used in the
%        CMFs.)
% As is an nx3 array of source-illuminant-referenced CMFs,
%        obtained from function adaptation_prep_XYZ or
%        adaptation_prep_xy.
% Ad is an nx3 array of destination-illuminant-referenced CMFs,
%        obtained from function adaptation_prep_XYZ or
%        adaptation_prep_xy.
% XYZs is a three element vector of source-illuminant-referenced
%        tristimulus values in the 0 <= Y <= 1 convention range.
% XYZd is a three element vector of destination-illuminant-referenced
%        tristimulus values in the 0 <= Y <= 1 convention range,
%        estimating what the effect of the illuminant would be.

% Notes
% -----
% Version 2020_01_18 Scott Allen Burns
%
% This chromatic adaptation transform (CAT) is based on smoothest
% reflectance reconstruction (References [1] and [2]) and is
% developed and compared to existing CATs (Bradford, CAT02, CAT16)
% in Reference [3].
% 
% References
% ----------
% [1] Burns SA. Numerical methods for smoothest reflectance
%     reconstruction. Color Research & Application, Vol 45,
%     No 1, 2020, pp 8-21.
% [2] Generating Reflectance Curves from sRGB Triplets, 2015
%     http://scottburns.us/reflectance-curves-from-srgb/#LLSS
% [3] Burns SA. Chromatic adaptation transform by spectral
%     reconstruction. Color Research & Application, Vol 44,
%     No 5, 2019, pp 682-693.

rho=method_2(D,As,XYZs);
XYZd=Ad'*rho;
XYZd=XYZd*XYZs(2)/XYZd(2);
