/ColorCompare

ColorCompare is a library to convert colors from hex, RGB, HSL, CIE L*a*b* (LAB) and DIN-99 into one another and calculate color distances (human visual difference).

Primary LanguagePHPMIT LicenseMIT

ColorCompare

License: MIT Build Status

ColorCompare is a library to convert colors from hex, RGB, HSL, CIE L*a*b* (LAB) and DIN-99 into one another and calculate color distances (human visual difference) using the DIN-99 methedology.

You can get the library from packagist:

composer require fjw/color-compare

How to use

You can get the DIN-99 visual difference (distance) of two colors easily:

use ColorCompare\Color;

$color1 = new Color("#aaff05");
$color2 = new Color("#CCC");

$difference = $color1->getDifference($color2);

You can convert each format into one another:

use ColorCompare\Color;

$color = new Color("#aaff05");

$hex = $color->getHex(); // just to show off, it already was Hex ;)
$rgb = $color->getRgb(); // [ "r" => 170, "g" => 255, "b" => 5 ]
$hsl = $color->getHsl(); // [ "h" => 80.4, "s" => 1.0, "l" => 0.51 ]
$lab = $color->getLab(); // [ "L" => 91.72, "a" => -54.41, "b" => 87.65 ]
$din99 = $color->getDin99(); // [ "L99" => 94.51, "a99" => -12.31, "b99" => 30.39 ]

You can create the color object by Hex, RGB, HSL and LAB:

use ColorCompare\Color;

$color = new Color([
    "h" => 300,
    "s" => 0.5,
    "l" => 1 
]);

$hex = $color->getHex();

Visual Color Distance with DIN-99

DIN-99 differences, returned by Color::getDifference(), can better calculate the human visual difference than LAB with delta-E. There are also superior distance calculations like CIE94 or CIEDE2000 but these are complicated and need intensive calculations. With DIN-99 the calculation is done beforehand and needs less ressources. When your color is already converted into DIN-99 you can just calculate the euklidean distance and get the same quality.

sqrt(($c2["L99"] - $c1["L99"])**2 +
    ($c2["a99"] - $c1["a99"])**2 +
    ($c2["b99"] - $c1["b99"])**2);

This is a huge advantage! If you would like, per example, make a client side filter of colored products (or whatever) you can convert your data into DIN-99 on the server side and only need to do the easier euklidean calculation in your JavaScript.

Sources (german):

http://www.germancolorgroup.de/html/Vortr_02_pdf/GCG_%202002_%20Buering.pdf

https://de.wikipedia.org/wiki/DIN99-Farbraum

Example Code

Just run ./devserver.sh if you have PHP-CLI and open http://localhost:8000 to see the difference calculation in action.