function luv=xyz2luv(C,bbb);

% XYZ2LUV computes the lightness, L*, and the chromatic coordinates u* and v* in CIELUV
% of a set of colours defined by their tristimulus CIE-1931 values.
%
% CIELUV is a simple appearance model providing perceptual descriptors (lightness, hue
% and chroma) for related colours (colours in a scene).
%
% In this representation, information about the illumination conditions or, alternatively,
% about the scene, is included in a reference stimulus. Using CIELUV in the standard
% conditions implies that the reference stilulus is a perfect difuser illuminated as the
% test.
% 
% SYNTAX
% ----------------------------------------------------------------------------
% LUV=xyz2luv(XYZ,XYZR)
%
% XYZ = Tristimulus values of the test stimuli.
%       For N colours, this is a Nx3 matrix.
%
% XYZR = Tristimulus values of the reference stimulus.
%        If the reference stimulus is the same for all the test stimuli, this
%        is a 1x3 matrix. If the reference is different for each tes stimulus
%        XYZR is a Nx3 matrix.
%
% LUV  = For N colours, Nx3 matrix, containing, in columns, the lightness L*,
%        and the chromaticity coordinates u* and v*.
%
% RELATED FUNCTIONS
% ----------------------------------------------------------------------------
% luv2xyz, luv2perc, perc2luv
%

s=size(C);
ss=size(bbb);
if ss(1)~=s(1)
   bbb=ones(s(1),1)*bbb(1,:);
end

for i=1:s(1)
   c=C(i,:);
   b=bbb(i,:);
   uo=4*b(1)/(b(1)+15*b(2)+3*b(3));
   vo=9*b(2)/(b(1)+15*b(2)+3*b(3));

   u=4*c(1)/(c(1)+15*c(2)+3*c(3));
   v=9*c(2)/(c(1)+15*c(2)+3*c(3));
   if (c(2)/b(2))>0.008856
      L=116*((c(2)/b(2))^(1/3))-16;
   else
      L=903.3*(c(2)/b(2));
   end
   uu=13*L*(u-uo);
   vv=13*L*(v-vo);
   luv(i,:)=[L uu vv];
end
