# Check calculation of CIE Ddlta E 2000 color difference 

# Reference data taken from 
# "The CIEDE2000 Color-Difference Formula: Implementation Notes, 
# Supplementary Test Data, and Mathematical Observations", 
# G. Sharma, W. Wu, E. N. Dalal, 
# Color Research and Application, vol. 30. No. 1, pp. 21-30, February 2005
# http://www2.ece.rochester.edu/~gsharma/ciede2000/
#
# Note: samples 1 to 6 and 28 are commented because the colors
# are out of RGB gamut.
# Samples 10 and 15 are aimed at testing discontinuity of CIEDE2000
# formula and so are very sensitive to accuracy, we need higher tolerance
# because conversion is done via RGB floats and loses precision.
#
# Format: L1 a1 b1 L2 a2 b2 expected_diff [tolerance]
set lab_diff_samples {
# 50.0000 2.6772 -79.7751 50.0000 0.0000 -82.7485 2.0425
# 50.0000 3.1571 -77.2803 50.0000 0.0000 -82.7485 2.8615
# 50.0000 2.8361 -74.0200 50.0000 0.0000 -82.7485 3.4412
# 50.0000 -1.3802 -84.2814 50.0000 0.0000 -82.7485 1.0000
# 50.0000 -1.1848 -84.8006 50.0000 0.0000 -82.7485 1.0000
# 50.0000 -0.9009 -85.5211 50.0000 0.0000 -82.7485 1.0000
50.0000 0.0000 0.0000 50.0000 -1.0000 2.0000 2.3669
50.0000 -1.0000 2.0000 50.0000 0.0000 0.0000 2.3669
50.0000 2.4900 -0.0010 50.0000 -2.4900 0.0009 7.1792
50.0000 2.4900 -0.0010 50.0000 -2.4900 0.0010 7.1792 0.05
50.0000 2.4900 -0.0010 50.0000 -2.4900 0.0011 7.2195
50.0000 2.4900 -0.0010 50.0000 -2.4900 0.0012 7.2195
50.0000 -0.0010 2.4900 50.0000 0.0009 -2.4900 4.8045
50.0000 -0.0010 2.4900 50.0000 0.0010 -2.4900 4.8045
50.0000 -0.0010 2.4900 50.0000 0.0011 -2.4900 4.7461 0.06
50.0000 2.5000 0.0000 50.0000 0.0000 -2.5000 4.3065
50.0000 2.5000 0.0000 73.0000 25.0000 -18.0000 27.1492
50.0000 2.5000 0.0000 61.0000 -5.0000 29.0000 22.8977
50.0000 2.5000 0.0000 56.0000 -27.0000 -3.0000 31.9030
50.0000 2.5000 0.0000 58.0000 24.0000 15.0000 19.4535
50.0000 2.5000 0.0000 50.0000 3.1736 0.5854 1.0000
50.0000 2.5000 0.0000 50.0000 3.2972 0.0000 1.0000
50.0000 2.5000 0.0000 50.0000 1.8634 0.5757 1.0000
50.0000 2.5000 0.0000 50.0000 3.2592 0.3350 1.0000
60.2574 -34.0099 36.2677 60.4626 -34.1751 39.4387 1.2644
63.0109 -31.0961 -5.8663 62.8187 -29.7946 -4.0864 1.2630
61.2901 3.7196 -5.3901 61.4292 2.2480 -4.9620 1.8731
# 35.0831 -44.1164 3.7933 35.0232 -40.0716 1.5901 1.8645
22.7233 20.0904 -46.6940 23.0331 14.9730 -42.5619 2.0373
36.4612 47.8580 18.3852 36.2715 50.5065 21.2231 1.4146
90.8027 -2.0831 1.4410 91.1528 -1.6435 0.0447 1.4441
90.9257 -0.5406 -0.9208 88.6381 -0.8985 -0.7239 1.5381
6.7747 -0.2908 -2.4247 5.8714 -0.0985 -2.2286 0.6377
2.0776 0.0795 -1.1350 0.9033 -0.0636 -0.5514 0.9082
}

set index -1
foreach sample [split $lab_diff_samples \n] {
  incr index
  set lab1 [lrange $sample 0 2]
  if { [lindex $lab1 0] == "#" || $lab1 == "" } continue
  set lab2 [lrange $sample 3 5]
  set dref [lindex $sample 6]
  set diff [vcolordiff {*}[vcolorconvert from lab {*}$lab1] {*}[vcolorconvert from lab {*}$lab2]]

  # use tolerance 1e-3 except if other value is defined in sample data
  set tol [lindex $sample 7]
  if { $tol == "" } { set tol 1e-3 }

  checkreal "Sample $index: Lab ($lab1) and ($lab2), diff" $diff $dref $tol 1e-6
}
