% deltaE00: Calculates the CIE Delta E00 Color Difference

% Lawrence Taplin 08Jan01

% Note: SL,SC&SH are based on the second set of Lab values.

function   De00=deltaE00(Lab1, Lab2)

%CIELAB Chroma
C1 = sqrt(Lab1(2).^2+Lab1(3).^2);
C2 = sqrt(Lab2(2).^2+Lab2(3).^2);

%Lab Prime
mC = (C1+C2)./2;
G=0.5*(1-sqrt((mC.^7)./((mC.^7)+(25.^7))));
LabP1 = [Lab1(1) ; Lab1(2).*(1+G) ; Lab1(3)];
LabP2 = [Lab2(1) ; Lab2(2).*(1+G) ; Lab2(3)];

%Chroma
CP1 = sqrt(LabP1(2).^2+LabP1(3).^2);
CP2 = sqrt(LabP2(2).^2+LabP2(3).^2);

%Hue Angle
hP1t = atan2Deg(LabP1(3),LabP1(2));
hP2t = atan2Deg(LabP2(3),LabP2(2));

%Add in 360 to the smaller hue angle if absolute value of difference is > 180
hP1 = hP1t + ((hP1t<hP2t)&(abs(hP1t-hP2t)>180)).*360;
hP2 = hP2t + ((hP1t>hP2t)&(abs(hP1t-hP2t)>180)).*360;

%Delta Values
DLP = LabP1(1,:) - LabP2(1,:);
DCP = CP1 - CP2;
DhP = hP1 - hP2;
DHP = 2*(CP1.*CP2).^(1/2).*sinDeg(DhP./2);

%Arithmetic mean of LCh' values
mLP = (LabP1(1,:)+LabP2(1,:))./2;
mCP = (CP1+CP2)./2;
mhP = (hP1+hP2)./2;

%Weighting Functions
SL = 1+(0.015.*(mLP-50).^2)./sqrt(20+(mLP-50).^2);
SC = 1+0.045.*mCP;
T = 1-0.17.*cosDeg(mhP-30)+0.24.*cosDeg(2.*mhP)+0.32.*cosDeg(3.*mhP+6)-0.2.*cosDeg(4.*mhP-63);
SH = 1+0.015.*mCP.*T;

%Rotation function
RC = 2.*sqrt((mCP.^7)./((mCP.^7)+25.^7));
DTheta = 30.*exp(-((mhP-275)./25).^2);
RT = -sinDeg(2.*DTheta).*RC;

%Parametric factors
kL = 1;
kC = 1;
kH = 1;

De00 = ((DLP./kL./SL).^2+(DCP./kC./SC).^2+(DHP./kH./SH).^2+(RT.*(DCP./kC./SC).*(DHP./kH./SH))).^(1/2);

% ------------- define a few convenient subfunctions -------------
function out = atan2Deg(inY,inX);
out = atan2(inY,inX).*180./pi;
out = out+(out<0).*360;

function out = sinDeg(in);
out = sin(in.*pi./180);

function out = cosDeg(in);
out = cos(in.*pi./180);
