Disclaimer: This project aims to be a 1:1 port of Markus Ekholm's great color-diff library which is written in Javascript (hence the occasional untypical naming convention).
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.
color-diff is available on maven central. It works on JDK 8 and later. Use the following snipped in
your pom.xml
to add it as a dependency:
<dependency>
<groupId>com.dajudge.color-diff</groupId>
<artifactId>color-diff</artifactId>
<version>0.0.4</version>
</dependency>
If you're using gradle then you'll want something like this:
buildscript {
repositories {
mavenCentral()
}
}
dependencies {
compile 'com.dajudge.color-diff:color-diff:0.0.4'
}
color-diff has no transitive dependencies, so you won't face any compatiblity issues.
If you want to build it yourself just checkout the source and run the following command:
./gradlew clean build
Tests are implemented in JUnit 5 and run with the build.
Returns the closest color. The parameter bc is optional and is used as background color when the color and/or palette uses alpha channels.
RgbColor color = new RgbColor(255, 1, 30);
List<RgbColor> palette = Arrays.asList(
new RgbColor(255, 0, 0), // red
new RgbColor(0, 255, 0), // green
new RgbColor(0, 0, 255) // blue
);
RgbColor closestMatch = ColorDiff.closest(color, palette);
assertEquals(new RgbColor(255, 0, 0), closestMatch); // red
The result above is obvious, but ColorDiff.closest()
could deal with more complicated
cases.
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.
RgbColor color = new RgbColor(255, 255, 255); // white
List<RgbColor> palette = Arrays.asList(
new RgbColor(0, 0, 0), // black
new RgbColor(255, 255, 255) // white
);
RgbColor furthestMatch = ColorDiff.furthest(color, palette);
assertEquals(new RgbColor(0,0,0), furthestMatch); // black
The result above is obvious, but ColorDiff.furthest()
could deal with more
complicated cases.
Returns a mapping from the colors in palette1
to palette2
.
RgbColor
is type containing 3 properties: R
, G
, B
, such as:
RgbColor color = new RgbColor(255, 1, 0);
There is an optional (i.e. nullable) property A
, which specifies
the alpha channel between 0.0 and 1.0.
RgbColor colorWithAlpha = new RgbColor(255, 1, 0, .5);
Each RGBA-color is transformed into a RGB-color before being used to calculate the CIEDE2000 difference, using the specified background color (which defaults to white).
Original Javascript version by Markus Ekholm
Java port by Alex Stockinger
3-clause BSD. For details see COPYING
.