Skip to content

Commit

Permalink
Consistently handle none/NaN across library (#476)
Browse files Browse the repository at this point in the history
* Consistently handle none/NaN across library

* Fix APCA
  • Loading branch information
facelessuser authored Mar 14, 2024
1 parent dfe3d14 commit e3a0a0d
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 16 deletions.
9 changes: 7 additions & 2 deletions src/contrast/APCA.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import getColor from "../getColor.js";
import to from "../to.js";
import {isNone} from "../util.js";

// exponents
const normBG = 0.56;
Expand Down Expand Up @@ -54,11 +55,15 @@ export default function contrastAPCA (background, foreground) {

// Calculates "screen luminance" with non-standard simple gamma EOTF
// weights should be from CSS Color 4, not the ones here which are via Myndex and copied from Lindbloom
[R, G, B] = foreground.coords;
[R, G, B] = foreground.coords.map(c => {
return isNone(c) ? 0 : c;
});
let lumTxt = linearize(R) * 0.2126729 + linearize(G) * 0.7151522 + linearize(B) * 0.0721750;

background = to(background, "srgb");
[R, G, B] = background.coords;
[R, G, B] = background.coords.map(c => {
return isNone(c) ? 0 : c;
});
let lumBg = linearize(R) * 0.2126729 + linearize(G) * 0.7151522 + linearize(B) * 0.0721750;

// toe clamping of very dark values to account for flare
Expand Down
3 changes: 2 additions & 1 deletion src/deltaE/deltaECMC.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import lab from "../spaces/lab.js";
import lch from "../spaces/lch.js";
import getColor from "../getColor.js";
import {isNone} from "../util.js";

// More accurate color-difference formulae
// than the simple 1976 Euclidean distance in Lab
Expand Down Expand Up @@ -87,7 +88,7 @@ export default function (color, sample, {l = 2, c = 1} = {}) {

// Cross term T for blue non-linearity
let T;
if (Number.isNaN(H1)) {
if (isNone(H1)) {
H1 = 0;
}

Expand Down
7 changes: 4 additions & 3 deletions src/deltaE/deltaEJz.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import jzczhz from "../spaces/jzczhz.js";
import getColor from "../getColor.js";
import {isNone} from "../util.js";

// More accurate color-difference formulae
// than the simple 1976 Euclidean distance in Lab
Expand All @@ -23,16 +24,16 @@ export default function (color, sample) {
let ΔC = Cz1 - Cz2;

// length of chord for ΔH
if ((Number.isNaN(Hz1)) && (Number.isNaN(Hz2))) {
if ((isNone(Hz1)) && (isNone(Hz2))) {
// both undefined hues
Hz1 = 0;
Hz2 = 0;
}
else if (Number.isNaN(Hz1)) {
else if (isNone(Hz1)) {
// one undefined, set to the defined hue
Hz1 = Hz2;
}
else if (Number.isNaN(Hz2)) {
else if (isNone(Hz2)) {
Hz2 = Hz1;
}

Expand Down
3 changes: 2 additions & 1 deletion src/distance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ColorSpace from "./space.js";
import {isNone} from "./util.js";

/**
* Euclidean distance of colors in an arbitrary color space
Expand All @@ -12,7 +13,7 @@ export default function distance (color1, color2, space = "lab") {

return Math.sqrt(coords1.reduce((acc, c1, i) => {
let c2 = coords2[i];
if (isNaN(c1) || isNaN(c2)) {
if (isNone(c1) || isNone(c2)) {
return acc;
}

Expand Down
6 changes: 3 additions & 3 deletions src/interpolation.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Functions related to color interpolation
*/
import ColorSpace from "./space.js";
import {type, interpolate} from "./util.js";
import {type, interpolate, isNone} from "./util.js";
import getColor from "./getColor.js";
import clone from "./clone.js";
import to from "./to.js";
Expand Down Expand Up @@ -167,10 +167,10 @@ export function range (color1, color2, options = {}) {
// Undefined hues must be evaluated before hue fix-up to properly
// calculate hue arcs between undefined and defined hues.
// See https://github.com/w3c/csswg-drafts/issues/9436#issuecomment-1746957545
if (isNaN(θ1) && !isNaN(θ2)) {
if (isNone(θ1) && !isNone(θ2)) {
θ1 = θ2;
}
else if (isNaN(θ2) && !isNaN(θ1)) {
else if (isNone(θ2) && !isNone(θ1)) {
θ2 = θ1;
}
[θ1, θ2] = angles.adjust(arc, [θ1, θ2]);
Expand Down
6 changes: 3 additions & 3 deletions src/space.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {type, parseCoordGrammar, serializeNumber, mapRange} from "./util.js";
import {type, parseCoordGrammar, serializeNumber, mapRange, isNone} from "./util.js";
import {getWhite} from "./adapt.js";
import hooks from "./hooks.js";
import getColor from "./getColor.js";
Expand Down Expand Up @@ -103,7 +103,7 @@ export default class ColorSpace {
let meta = coordMeta[i];

if (meta.type !== "angle" && meta.range) {
if (Number.isNaN(c)) {
if (isNone(c)) {
// NaN is always in gamut
return true;
}
Expand Down Expand Up @@ -186,7 +186,7 @@ export default class ColorSpace {
}

// Convert NaN to 0, which seems to be valid in every coordinate of every color space
coords = coords.map(c => Number.isNaN(c) ? 0 : c);
coords = coords.map(c => isNone(c) ? 0 : c);

// Find connection space = lowest common ancestor in the base tree
let myPath = this.path;
Expand Down
3 changes: 2 additions & 1 deletion src/spaces/lch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ColorSpace from "../space.js";
import Lab from "./lab.js";
import {constrain as constrainAngle} from "../angles.js";
import {isNone} from "../util.js";

export default new ColorSpace({
id: "lch",
Expand Down Expand Up @@ -49,7 +50,7 @@ export default new ColorSpace({
Chroma = 0;
}
// Deal with NaN Hue
if (isNaN(Hue)) {
if (isNone(Hue)) {
Hue = 0;
}
return [
Expand Down
3 changes: 2 additions & 1 deletion src/spaces/lchuv.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ColorSpace from "../space.js";
import Luv from "./luv.js";
import {constrain as constrainAngle} from "../angles.js";
import {isNone} from "../util.js";

export default new ColorSpace({
id: "lchuv",
Expand Down Expand Up @@ -49,7 +50,7 @@ export default new ColorSpace({
Chroma = 0;
}
// Deal with NaN Hue
if (isNaN(Hue)) {
if (isNone(Hue)) {
Hue = 0;
}
return [
Expand Down
3 changes: 2 additions & 1 deletion src/spaces/oklch.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import ColorSpace from "../space.js";
import OKLab from "./oklab.js";
import {constrain as constrainAngle} from "../angles.js";
import {isNone} from "../util.js";

export default new ColorSpace({
id: "oklch",
Expand Down Expand Up @@ -48,7 +49,7 @@ export default new ColorSpace({
let a, b;

// check for NaN hue
if (isNaN(h)) {
if (isNone(h)) {
a = 0;
b = 0;
}
Expand Down

0 comments on commit e3a0a0d

Please sign in to comment.