Get the full commented source code of

HTML5 Suika Watermelon Game

Talking about Php and Tutorials.

It’s time to see how to calculate difference between two colors.

I am using the CIE1994 formula because it’s quite accurate and not that complex to calculate.

Here it is, assuming I have two colors in Lab format

where

(thanx to Bruce Lindbloom)

First, I am turning the scripts explained at Color difference algorithm into functions, this way:

function rgb_to_xyz($rgb){
     $red = $rgb[0];
     $green = $rgb[1];
     $blue = $rgb[2]; 
     $_red = $red/255;
     $_green = $green/255;
     $_blue = $blue/255;
     if($_red>0.04045){
          $_red = ($_red+0.055)/1.055;
          $_red = pow($_red,2.4);
     }
     else{
          $_red = $_red/12.92;
     }
     if($_green>0.04045){
          $_green = ($_green+0.055)/1.055;
          $_green = pow($_green,2.4);     
     }
     else{
          $_green = $_green/12.92;
     }
     if($_blue>0.04045){
          $_blue = ($_blue+0.055)/1.055;
          $_blue = pow($_blue,2.4);     
     }
     else{
          $_blue = $_blue/12.92;
     }
     $_red *= 100;
     $_green *= 100;
     $_blue *= 100;
     $x = $_red * 0.4124 + $_green * 0.3576 + $_blue * 0.1805;
     $y = $_red * 0.2126 + $_green * 0.7152 + $_blue * 0.0722;
     $z = $_red * 0.0193 + $_green * 0.1192 + $_blue * 0.9505;
     return(array($x,$y,$z));
}

and…

function xyz_to_lab($xyz){
     $x = $xyz[0];
     $y = $xyz[1];
     $z = $xyz[2];
     $_x = $x/95.047;
     $_y = $y/100;
     $_z = $z/108.883;
     if($_x>0.008856){
          $_x = pow($_x,1/3);
     }
     else{
          $_x = 7.787*$_x + 16/116;
     }
     if($_y>0.008856){
          $_y = pow($_y,1/3);
     }
     else{
          $_y = (7.787*$_y) + (16/116);
     }
     if($_z>0.008856){
          $_z = pow($_z,1/3);
     }
     else{
          $_z = 7.787*$_z + 16/116;
     }
     $l= 116*$_y -16;
     $a= 500*($_x-$_y);
     $b= 200*($_y-$_z);
     return(array($l,$a,$b));
}

Now, it’s time to determine color difference, using the above formula:

function de_1994($lab1,$lab2){
     $c1 = sqrt($lab1[1]*$lab1[1]+$lab1[2]*$lab1[2]);
     $c2 = sqrt($lab2[1]*$lab2[1]+$lab2[2]*$lab2[2]);
     $dc = $c1-$c2;
     $dl = $lab1[0]-$lab2[0];
     $da = $lab1[1]-$lab2[1];
     $db = $lab1[2]-$lab2[2];
     $dh = sqrt(($da*$da)+($db*$db)-($dc*$dc));
     $first = $dl;
     $second = $dc/(1+0.045*$c1);
     $third = $dh/(1+0.015*$c1);
     return(sqrt($first*$first+$second*$second+$third*$third));
}

And you’ll find the difference between colors.

Next time, a real application using all this boring theory… (you will be surprised…)

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.