Coder Social home page Coder Social logo

markusn / color-diff Goto Github PK

View Code? Open in Web Editor NEW
356.0 10.0 33.0 170 KB

Implemets the CIEDE2000 color difference algorithm, conversion between RGB and lab color and mapping all colors in palette X to the closest color in palette Y based on the CIEDE2000 difference.

License: Other

JavaScript 100.00%
ciede2000 javascript lab-color conversion rgb colordiff palette

color-diff's Introduction

color-diff

Build Status Coverage Status

Implements the CIEDE2000 color difference algorithm, conversion between RGB and LAB color and mapping all colors in palette X to the closest color in palette Y based on the CIEDE2000 difference.

Installation

npm install color-diff --save

Tests

Are located in the test/ folder and are run by:

npm test

Usage

// CommonJS
const { 
  closest, 
  furthest,
  diff, 
  mapPalette,
  paletteMapKey,
  rgbaToLab,
  mapPaletteLab,
  labPaletteMapKey,
} = require("color-diff");

// ESM
import {
  closest,
  furthest,
  diff,
  mapPalette,
  paletteMapKey,
  rgbaToLab,
  mapPaletteLab,
  labPaletteMapKey,
} from "color-diff";

closest(color, palette, bc)

Returns the closest color. The parameter bc is optional and is used as background color when the color and/or palette uses alpha channels.

const color = { R: 255, G: 1, B: 30 };
// red, green, blue
const palette = [ {R: 255, G: 0, B: 0 },
                {R: 0, G: 255, B: 0 },
                {R: 0, G: 0, B: 255} ];

closest(color, palette); // {R: 255, G: 0, B: 0 }, red

The result above is obvious, but diff.closest could deal with more complicated cases.

furthest(color, palette, bc)

Returns the most different color. The parameter bc is optional and is used as background color when the color and/or palette uses alpha channels.

const color = { R: 255, G: 255, B: 255 };
// black, white
const palette = [ {R: 0, G: 0, B: 0 }, {R: 255, G: 255, B: 255 } ];

furthest(color, palette); // {R: 0, G: 0, B: 0 }, black

The result above is obvious, but diff.furthest could deal with more complicated cases.

mapPalette(palette1, palette2)

Returns a mapping from the colors in palette1 to palette2.

paletteMapKey(color)

Return the palette map key for the color, to be used with the result from mapPalette.

diff(color1, color2, bc)

Returns the difference between the lab colors color1 and color2. The parameter bc is optional and is used as background color when one of the colors uses alpha channels.

rgba color

Object

RGBAColor is an object containing 4 properties: 'R', 'G', 'B', 'A', where 'A' is optional OR 'r', 'g', 'b', 'a', where 'a' is optional . Such as:

{ R: 255, G: 1, B: 0 }

There is an optional property 'A', which specifies the alpha channel between 0.0 and 1.0. If not present the color will be treated as fully opaque, i.e. A = 1.0.

Each RGBA-color is transformed into a RGB-color before being used to calculate the CIEDE2000 difference, using the specified background color (defaults to white).

lab color

Object

LabColor is an object containing 3 properties 'L', 'a', 'b' such as:

{ L: 100, a: 0.005, b: -0.010 }

palette

Array.<RGBAColor>

Color palette array which contains many RGBAColor objects.

Author

Markus Ekholm

License

3-clause BSD. For details see COPYING.

color-diff's People

Contributors

benjie avatar caseman avatar dependabot[bot] avatar evelynhathaway avatar markusn avatar munter avatar sunesimonsen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

color-diff's Issues

Limit function

I bumped into this lib by search for checking color relevance. I got a python example which can order them by relevance using a limit as well. So instead of returning 1 color, it returns an ordered list with a preset limit..

Is such a thing possible?

This example transform the RGB values to a vector list and then uses .length. I dont fully understand/know what .lenght is though in vector lists. I was think 3d points, but since these are its different i guess?!

def relevance(rgb1, rgb2):
        if isinstance(rgb1, RGB) and isinstance(rgb2, RGB):
            relevancelengtn = (__class__.rgb_to_vector(rgb1) - __class__.rgb_to_vector(rgb2)).length
            return 1 - relevancelengtn / __class__.__relevance0
        else:
            return 0

Allow lowercase RGB objects as input

Many libraries (tinycolor for instance) and codebases use {r, g, b} objects to represent RGB colors instead of the currently supported {R, G, B}.

Would be open to a PR adding support for the lowercase keys?

Expose diff function

It would be interesting to expose the diff function so we can get the difference between two colors.

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.