Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for L*a*b* values being passed as Strings #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dist/deltae.global.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

89 changes: 48 additions & 41 deletions src/dE00.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,28 +29,35 @@
function dE00(x1, x2, weights) {
var sqrt = Math.sqrt;
var pow = Math.pow;


x1.L = parseFloat(x1.L);
x1.A = parseFloat(x1.A);
x1.B = parseFloat(x1.B);
x2.L = parseFloat(x2.L);
x2.A = parseFloat(x2.A);
x2.B = parseFloat(x2.B);

this.x1 = x1;
this.x2 = x2;

this.weights = weights || {};
this.ksubL = this.weights.lightness || 1;
this.ksubC = this.weights.chroma || 1;
this.ksubH = this.weights.hue || 1;

// Delta L Prime
this.deltaLPrime = x2.L - x1.L;

// L Bar
this.LBar = (x1.L + x2.L) / 2;

// C1 & C2
this.C1 = sqrt(pow(x1.A, 2) + pow(x1.B, 2));
this.C2 = sqrt(pow(x2.A, 2) + pow(x2.B, 2));

// C Bar
this.CBar = (this.C1 + this.C2) / 2;

// A Prime 1
this.aPrime1 = x1.A +
(x1.A / 2) *
Expand All @@ -72,52 +79,52 @@ function dE00(x1, x2, weights) {
pow(this.aPrime1, 2) +
pow(x1.B, 2)
);

// C Prime 2
this.CPrime2 = sqrt(
pow(this.aPrime2, 2) +
pow(x2.B, 2)
);

// C Bar Prime
this.CBarPrime = (this.CPrime1 + this.CPrime2) / 2;

// Delta C Prime
this.deltaCPrime = this.CPrime2 - this.CPrime1;

// S sub L
this.SsubL = 1 + (
(0.015 * pow(this.LBar - 50, 2)) /
sqrt(20 + pow(this.LBar - 50, 2))
);

// S sub C
this.SsubC = 1 + 0.045 * this.CBarPrime;

/**
* Properties set in getDeltaE method, for access to convenience functions
*/
// h Prime 1
this.hPrime1 = 0;

// h Prime 2
this.hPrime2 = 0;

// Delta h Prime
this.deltahPrime = 0;

// Delta H Prime
this.deltaHPrime = 0;

// H Bar Prime
this.HBarPrime = 0;

// T
this.T = 0;

// S sub H
this.SsubH = 0;

// R sub T
this.RsubT = 0;
}
Expand All @@ -131,36 +138,36 @@ dE00.prototype.getDeltaE = function() {
var sqrt = Math.sqrt;
var sin = Math.sin;
var pow = Math.pow;

// h Prime 1
this.hPrime1 = this.gethPrime1();

// h Prime 2
this.hPrime2 = this.gethPrime2();

// Delta h Prime
this.deltahPrime = this.getDeltahPrime();

// Delta H Prime
this.deltaHPrime = 2 * sqrt(this.CPrime1 * this.CPrime2) * sin(this.degreesToRadians(this.deltahPrime) / 2);

// H Bar Prime
this.HBarPrime = this.getHBarPrime();

// T
this.T = this.getT();

// S sub H
this.SsubH = 1 + 0.015 * this.CBarPrime * this.T;

// R sub T
this.RsubT = this.getRsubT();

// Put it all together!
var lightness = this.deltaLPrime / (this.ksubL * this.SsubL);
var chroma = this.deltaCPrime / (this.ksubC * this.SsubC);
var hue = this.deltaHPrime / (this.ksubH * this.SsubH);

return sqrt(
pow(lightness, 2) +
pow(chroma, 2) +
Expand All @@ -179,7 +186,7 @@ dE00.prototype.getRsubT = function() {
var sqrt = Math.sqrt;
var pow = Math.pow;
var exp = Math.exp;

return -2 *
sqrt(
pow(this.CBarPrime, 7) /
Expand All @@ -204,7 +211,7 @@ dE00.prototype.getRsubT = function() {
*/
dE00.prototype.getT = function() {
var cos = Math.cos;

return 1 -
0.17 * cos(this.degreesToRadians(this.HBarPrime - 30)) +
0.24 * cos(this.degreesToRadians(2 * this.HBarPrime)) +
Expand All @@ -219,11 +226,11 @@ dE00.prototype.getT = function() {
*/
dE00.prototype.getHBarPrime= function() {
var abs = Math.abs;

if (abs(this.hPrime1 - this.hPrime2) > 180) {
return (this.hPrime1 + this.hPrime2 + 360) / 2
}

return (this.hPrime1 + this.hPrime2) / 2
};

Expand All @@ -234,17 +241,17 @@ dE00.prototype.getHBarPrime= function() {
*/
dE00.prototype.getDeltahPrime = function() {
var abs = Math.abs;

// When either C′1 or C′2 is zero, then Δh′ is irrelevant and may be set to
// zero.
if (0 === this.C1 || 0 === this.C2) {
return 0;
}

if (abs(this.hPrime1 - this.hPrime2) <= 180) {
return this.hPrime2 - this.hPrime1;
}

if (this.hPrime2 <= this.hPrime1) {
return this.hPrime2 - this.hPrime1 + 360;
} else {
Expand Down Expand Up @@ -278,13 +285,13 @@ dE00.prototype.gethPrime2 = function() {
*/
dE00.prototype._gethPrimeFn = function(x, y) {
var hueAngle;

if (x === 0 && y === 0) {
return 0;
}

hueAngle = this.radiansToDegrees(Math.atan2(x, y));

if (hueAngle >= 0) {
return hueAngle;
} else {
Expand All @@ -310,4 +317,4 @@ dE00.prototype.degreesToRadians = function(degrees) {
return degrees * (Math.PI / 180);
};

module.exports = dE00;
module.exports = dE00;