Build 10 games and learn game development along the way in this fast paced game development course

Get the source code of 12 commercial games, loaded 50+ million times, and learn the secrets of game design

Learn how to make a successful Flash game from a real world example. Fully commented source code

Create realistic Flash physics games from scratch with advanced features, from Angry Birds to Crush the Castle

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…)

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (9 votes, average: 4.78 out of 5)
Loading ... Loading ...
Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now
Be my fan on Facebook and follow me on Twitter! Exclusive content for my Facebook fans and Twitter followers

This post has 12 comments

  1. dim

    on September 9, 2009 at 12:40 am

    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

  2. How to Choose Colours Procedurally (Algorithms) ยป devmag.org.za

    on July 30, 2012 at 10:49 am

    […] http://www.emanueleferonato.com/2009/09/08/color-difference-algorithm-part-2/ […]

  3. Eduardo

    on October 27, 2012 at 10:06 pm

    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?

  4. kpodenphant

    on November 12, 2012 at 1:12 am

    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!

  5. anonyMouse

    on December 28, 2012 at 3:33 pm

    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);

  6. Radek

    on February 16, 2013 at 9:04 pm

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

  7. Igor

    on March 9, 2013 at 12:11 pm

    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?

  8. Emanuele Feronato

    on March 11, 2013 at 5:37 pm

    Hello Igor,

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

  9. Dylan

    on May 5, 2013 at 9:10 pm

    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)));

  10. Dylan

    on May 6, 2013 at 1:29 am

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

  11. Dylan

    on May 6, 2013 at 4:21 pm

    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.

  12. Jimmy James

    on October 12, 2013 at 5:59 pm

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

Thank you for the download!!

I hope you will find it useful and make something interesting out of it

To keep up to date with the blog, why don't you like my Facebook page and follow me on Twitter?

Want to learn more? Don't miss this:

Learn how to make a successful commercial Flash game from a real world example: get the fully commented source code of Globez, a Flash game played millions of times which generated a four figure income. Limited copies available.

Get it now

×