Skip to content

Commit

Permalink
fix NaN in color deconstruction
Browse files Browse the repository at this point in the history
  • Loading branch information
mck committed Nov 12, 2024
1 parent 9f72ce5 commit 3bea85f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-geckos-admire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@tokens-studio/graph-engine": patch
---

Fix Nan values in color deconstruct if the channel doesn't output something.
8 changes: 5 additions & 3 deletions packages/graph-engine/src/nodes/color/deconstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,11 @@ export default class DestructColorNode extends Node {
}

this.outputs.space.set(color.space);
this.outputs.a.set(color.channels[0]);
this.outputs.b.set(color.channels[1]);
this.outputs.c.set(color.channels[2]);

// Set channel values, defaulting to 0 if NaN
this.outputs.a.set(isNaN(color.channels[0]) ? 0 : color.channels[0]);
this.outputs.b.set(isNaN(color.channels[1]) ? 0 : color.channels[1]);
this.outputs.c.set(isNaN(color.channels[2]) ? 0 : color.channels[2]);

// Only set alpha if it exists
if (color.alpha !== undefined) {
Expand Down
19 changes: 19 additions & 0 deletions packages/graph-engine/tests/suites/nodes/color/deconstruct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,23 @@ describe('color/deconstruct', () => {
expect(node.outputs.c.value).to.equal(0);
expect(node.outputs.alpha.value).to.equal(0);
});

test('handles NaN values in OKHSL color space', async () => {
const graph = new Graph();
const node = new DestructColorNode({ graph });

node.inputs.color.setValue({
space: 'okhsl',
channels: [0.5, 0, NaN],
alpha: 1
});

await node.execute();

expect(node.outputs.space.value).toBe('okhsl');
expect(node.outputs.a.value).toBe(0.5);
expect(node.outputs.b.value).toBe(0);
expect(node.outputs.c.value).toBe(0);
expect(node.outputs.alpha.value).toBe(1);
});
});

0 comments on commit 3bea85f

Please sign in to comment.