Skip to content

Commit

Permalink
Fix broken serialization and add serialize test stub
Browse files Browse the repository at this point in the history
  • Loading branch information
LeaVerou committed May 24, 2024
1 parent a8a2b64 commit 095d029
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
12 changes: 5 additions & 7 deletions src/Format.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ let instance = Symbol("instance");

const outputByType = {
"<percentage>": {
range: [0, 100],
toRange: [0, 100],
suffix: "%"

Check warning on line 8 in src/Format.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing trailing comma
},
"<angle>": {
range: [0, 360],
toRange: [0, 360],
suffix: "deg"

Check warning on line 12 in src/Format.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing trailing comma
},
}

Check warning on line 14 in src/Format.js

View workflow job for this annotation

GitHub Actions / Lint & Test Types

Missing semicolon
Expand Down Expand Up @@ -64,7 +64,8 @@ export default class Format {
let outputType = this.coords[i][0];

let fromRange = coordMeta.range || coordMeta.refRange;
let {toRange, suffix} = outputByType[outputType] || {toRange: outputType.range, suffix: ""};

let {toRange, suffix} = outputByType[outputType.type] || {toRange: outputType.range, suffix: ""};

return {fromRange, toRange, suffix};
});
Expand All @@ -75,10 +76,7 @@ export default class Format {
return coords.map((c, i) => {
let {fromRange, toRange, suffix} = this.coordFormats[i];

if (fromRange && toRange) {
c = mapRange(fromRange, toRange, c);
}

c = mapRange(fromRange, toRange, c);
c = serializeNumber(c, {precision, unit: suffix});

return c;
Expand Down
17 changes: 3 additions & 14 deletions src/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function serialize (color, {
inGamut ||= format.toGamut;

if (inGamut && !checkInGamut(color)) {
// FIXME what happens if the color contains NaNs?
// FIXME what happens if the color contains none values?
coords = toGamut(clone(color), inGamut === true ? undefined : inGamut).coords;
}

Expand All @@ -54,22 +54,11 @@ export default function serialize (color, {
// Functional syntax
let name = format.name || "color";

if (format.serializeCoords) {
coords = format.serializeCoords(coords, precision);
}
else {
if (precision !== null) {
coords = coords.map(c => {
return util.serializeNumber(c, {precision});
});
}
}

let args = [...coords];
let args = format.serializeCoords(coords, precision);

if (name === "color") {
// If output is a color() function, add colorspace id as first argument
let cssId = format.id || format.ids?.[0] || color.space.id;
let cssId = format.id || format.ids?.[0] || color.space.cssId || color.space.id;
args.unshift(cssId);
}

Expand Down
18 changes: 18 additions & 0 deletions test/serialize.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import "../src/spaces/index.js";
import serialize from "../src/serialize.js";

const tests = {
name: "Color serialization Tests",
description: "These tests parse different color formats and compare the result as JSON",
run (spaceId, coords, alpha) {
return serialize({spaceId, coords, alpha});
},
tests: [
{
args: ["srgb", [1, 0.5, 0]],
expect: "rgb(100% 50% 0%)",
}
],
};

export default tests;

0 comments on commit 095d029

Please sign in to comment.