Giter Site home page Giter Site logo

araeubig / colorcompare Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fjw/colorcompare

0.0 0.0 0.0 30 KB

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

License: MIT License

Shell 0.29% PHP 99.71%

colorcompare's Introduction

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.

colorcompare's People

Contributors

fjw avatar invincibear avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.