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:

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

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

214 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers