Skip to content

Commit

Permalink
Add differenceHyab(), fixes #126
Browse files Browse the repository at this point in the history
  • Loading branch information
danburzo committed Jun 21, 2021
1 parent e80304d commit fcd67ed
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
10 changes: 8 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -673,14 +673,20 @@ Computes the [CIE94][cie94] ΔE\*<sub>94</sub> color difference between the colo

Computes the [CIEDE2000][ciede2000] ΔE\*<sub>00</sub> color difference between the colors _a_ and _b_ as implemented by [G. Sharma](http://www2.ece.rochester.edu/~gsharma/ciede2000/).

Returns a [CIEDE2000](https://en.wikipedia.org/wiki/Color_difference#CIEDE2000) Delta E\* function.

<a name="differenceCmc" href="#differenceCmc">#</a> culori.**differenceCmc**() &middot; [Source](https://github.com/evercoder/culori/blob/master/src/difference.js)

Computes the [CMC l:c (1984)][cmc] ΔE\*<sub>CMC</sub> color difference between the colors _a_ and _b_.

ΔE\*<sub>CMC</sub> is not considered a metric since it's not symmetrical, that is the distance from _a_ to _b_ is not always equal to the distance from _b_ to _a_. Therefore it cannot be reliably used with [`culori.nearest()`](#nearest).

<a name="differenceHyab" href="#differenceHyab">#</a> culori.**differenceHyab**() &middot; [Source](https://github.com/evercoder/culori/blob/master/src/difference.js)

Computes the HyAB color difference between the colors _a_ and _b_, as proposed in:

> Abasi S, Amani Tehran M, Fairchild MD. _Distance metrics for very large color differences._ Color Res Appl. 2019; 1–16. https://doi.org/10.1002/col.22451 ([PDF](http://markfairchild.org/PDFs/PAP40.pdf))
The HyAB formula combines the Euclidean and [city block](https://en.wikipedia.org/wiki/Taxicab_geometry) distance and has been experimentally shown to work better for large color differences than CIEDE2000, while still holding up well for smaller color differences, making it a _"good candidate formula for image processing and computer vision applications"_.

### Other difference formulas

<a name="differenceDin99o" href="#differenceDin99o">#</a> culori.**differenceDin99o**() &middot; [Source](https://github.com/evercoder/culori/blob/master/src/difference.js)
Expand Down
26 changes: 26 additions & 0 deletions src/difference.js
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,31 @@ const differenceCmc = (l = 1, c = 1) => {
};
};

/*
HyAB color difference formula, introduced in:
Abasi S, Amani Tehran M, Fairchild MD.
"Distance metrics for very large color differences."
Color Res Appl. 2019; 1–16.
https://doi.org/10.1002/col.22451
PDF available at:
http://markfairchild.org/PDFs/PAP40.pdf
*/
const differenceHyab = () => {
let lab = converter('lab65');
return (std, smp) => {
let LabStd = lab(std);
let LabSmp = lab(smp);
let dL = LabStd.l - LabSmp.l;
let dA = LabStd.a - LabSmp.a;
let dB = LabStd.b - LabSmp.b;
return Math.abs(dL) + Math.sqrt(dA * dA + dB * dB);
};
};

const differenceDin99o = () => differenceEuclidean('dlab');

/*
Expand All @@ -278,6 +303,7 @@ export {
differenceCie94,
differenceCiede2000,
differenceCmc,
differenceHyab,
differenceDin99o,
differenceKotsarenkoRamos
};
1 change: 1 addition & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ export {
differenceCie94,
differenceCiede2000,
differenceCmc,
differenceHyab,
differenceDin99o,
differenceKotsarenkoRamos,
differenceHueSaturation,
Expand Down
6 changes: 6 additions & 0 deletions test/difference.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
differenceCie94,
differenceCiede2000,
differenceCmc,
differenceHyab,
differenceKotsarenkoRamos,
rgb,
lab65,
Expand Down Expand Up @@ -181,3 +182,8 @@ tape('difference in LCh space', t => {
t.equal(differenceEuclidean('lch')('red', 'green'), 130.3764124621912);
t.end();
});

tape('differenceHyab', t => {
t.equal(differenceHyab()('red', 'green'), 139.93576718451553);
t.end();
});

0 comments on commit fcd67ed

Please sign in to comment.