Color difference algorithm – part 2

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:

and…

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

And you’ll find the difference between colors.

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

  • dim

    great snippet … i will love to see a function that can generate lighter or darker color by a given color value through parameter

    this will be very handy when we want to auto generate color themes bye given color value by user

  • Pingback: How to Choose Colours Procedurally (Algorithms) ยป devmag.org.za()

  • Eduardo

    I trying to transfer this code to pascal. I don’t understan the following operation:
    $_red *= 100;
    $_green *= 100;
    $_blue *= 100;
    Could you explain to me?

  • kpodenphant

    This is frickin’ awesome.I ported it to Actionscript.
    I tried the RGB distance method which worked ok, but had some errors. Then i tried converting to HSB which worked well for the RGB errors but has some other error.
    This algorithm is sweet!

  • anonyMouse

    Eduardo it means that the variable is set to its current value times 100;

    i.e. red=2;
    red*=100; (red is now 200, all further code that uses the variable “red” will now use 200 rather than 2);

  • Thanks for great snippet Emanuele !
    Just one point – shouldn’t be used abs(…) for all delta computations in de_1994() ?
    Thanks anyway !

  • Igor

    Thanks for explaining this stuff, and for your snippets of code )

    I’d like to use those snippets in our project, but in your post you don’t mention it anywhere, so can I ask you if it’s okay to reuse them? And if so, can you please put a license note on them, so they will be legally accessible?

    • Emanuele Feronato

      Hello Igor,

      use it as you want, there’s no restriction, ok?

  • Dylan

    Thanks for this, it saved me A LOT of time.

    I had to change Line 8 of de_1994 to $dh = sqrt(abs(($da*$da)+($db*$db)-($dc*$dc)));

  • Dylan

    Change line 8 of de_1994 to:
    $dh = sqrt(($da*$da)+($db*$db)+($dc*$dc));

  • Dylan

    You are going to kill me!!! Ignore the nonsense I posted earlier…

    Change line 8 of de_1994 to:
    $dh = ($da * $da) + ($db * $db) – ($dc * $dc);
    if ($dh < 0) { $dh = 0; } else { $dh = sqrt($dh); }

    I guess the problem is an excessively large delta C resulting in a sqrt of a negative number. This was causing NaN on many comparisons.

  • Jimmy James

    Where’s the next part? I want to see how these functions are used!