Skip to content

Commit

Permalink
feat: color modifier format hex for non-sRGB spaces (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema committed Jun 22, 2023
1 parent 9ab7b1c commit c4bb776
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/wild-snakes-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': patch
---

Allow color modifications to format to hex when the color space is not sRGB. It will do a conversion step to sRGB space before formatting to hex.
4 changes: 2 additions & 2 deletions package-lock.json

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

11 changes: 8 additions & 3 deletions src/color-modifiers/modifyColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ export function modifyColor(
}

returnedColor = returnedColor.to(modifier.space);
if (modifier.format && ['lch', 'srgb', 'p3', 'hsl'].includes(modifier.format)) {
returnedColor = returnedColor.to(modifier.format);
}

if (modifier.format && ['lch', 'srgb', 'p3', 'hsl', 'hex'].includes(modifier.format)) {
// Since hex is not a color space, convert to srgb, toString will then be able to format to hex
if (modifier.format === 'hex') {
returnedColor = returnedColor.to('srgb');
} else {
returnedColor = returnedColor.to(modifier.format);
}
}
return returnedColor.toString({
inGamut: true,
precision: 3,
Expand Down
19 changes: 19 additions & 0 deletions test/spec/color-modifiers/transformColorModifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,23 @@ describe('transform color modifiers', () => {
// output to hex
expect(transformColorModifiers(token('hex'))).to.equal('#9a3535');
});

it('can convert from a non-srgb space to srgb space to then format it as a hex color (which is fundamentally rgb)', () => {
const token = {
value: '#C14242',
type: 'color',
$extensions: {
'studio.tokens': {
modify: {
type: 'darken',
value: '0.2',
space: 'lch',
format: 'hex',
},
},
},
};

expect(transformColorModifiers(token)).to.equal('#983735');
});
});

0 comments on commit c4bb776

Please sign in to comment.