Skip to content

Commit

Permalink
fix(color): div-by-zero in XYY<>XYZ conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed Jan 30, 2021
1 parent f2799ef commit 8a71c6e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
5 changes: 3 additions & 2 deletions packages/color/src/xyy/xyy-xyz.ts
@@ -1,3 +1,4 @@
import { safeDiv } from "@thi.ng/math";
import { setC4 } from "@thi.ng/vectors";
import type { ColorOp } from "../api";
import { ensureAlpha } from "../internal/ensure-alpha";
Expand All @@ -14,9 +15,9 @@ export const xyyXyz: ColorOp = (out, src) => {
const { 0: x, 1: y, 2: Y } = src;
return setC4(
out || src,
(Y * x) / y,
safeDiv(Y * x, y),
Y,
(Y * (1 - x - y)) / y,
safeDiv(Y * (1 - x - y), y),
ensureAlpha(src[3])
);
};
13 changes: 10 additions & 3 deletions packages/color/src/xyz/xyz-xyy.ts
@@ -1,3 +1,4 @@
import { safeDiv } from "@thi.ng/math";
import { setC4 } from "@thi.ng/vectors";
import type { ColorOp } from "../api";
import { ensureAlpha } from "../internal/ensure-alpha";
Expand All @@ -11,7 +12,13 @@ import { ensureAlpha } from "../internal/ensure-alpha";
* @param src
*/
export const xyzXyy: ColorOp = (out, src) => {
const { 0: x, 1: y } = src;
const invSum = 1 / (x + y + src[2]);
return setC4(out || src, x * invSum, y * invSum, y, ensureAlpha(src[3]));
const { 0: x, 1: Y } = src;
const sum = x + Y + src[2];
return setC4(
out || src,
safeDiv(x, sum),
safeDiv(Y, sum),
Y,
ensureAlpha(src[3])
);
};

0 comments on commit 8a71c6e

Please sign in to comment.