Skip to content

Commit

Permalink
fix: pass colorspace to color modifier mix utility (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorenbroekema authored Mar 27, 2024
1 parent 03cf7ad commit 7617f9d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/eight-owls-complain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tokens-studio/sd-transforms': patch
---

Pass colorspace to mix modifier, to use the correct color space to mix in.
10 changes: 8 additions & 2 deletions src/color-modifiers/mix.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import Color from 'colorjs.io';
import { ColorSpaceTypes } from '@tokens-studio/types';

export function mix(color: Color, amount: number, mixColor: Color): Color {
export function mix(
color: Color,
colorSpace: ColorSpaceTypes,
amount: number,
mixColor: Color,
): Color {
const mixValue = Math.max(0, Math.min(1, Number(amount)));

return new Color(color.mix(mixColor, mixValue).toString());
return new Color(color.mix(mixColor, mixValue, { space: colorSpace }).toString());
}
7 changes: 6 additions & 1 deletion src/color-modifiers/modifyColor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,12 @@ export function modifyColor(
returnedColor = darken(color, modifier.space, Number(modifier.value));
break;
case 'mix':
returnedColor = mix(color, Number(modifier.value), new Color(modifier.color));
returnedColor = mix(
color,
modifier.space,
Number(modifier.value),
new Color(modifier.color),
);
break;
case 'alpha': {
returnedColor = transparentize(color, Number(modifier.value));
Expand Down
8 changes: 3 additions & 5 deletions test/spec/color-modifiers/transformColorModifiers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,12 @@ describe('transform color modifiers', () => {
});

// mix black with white, should give a grey
expect(transformColorModifiers(token(ColorSpaceTypes.HSL))).to.equal('hsl(91.8 0% 46.6%)');
expect(transformColorModifiers(token(ColorSpaceTypes.HSL))).to.equal('hsl(0 0% 50%)');
expect(transformColorModifiers(token(ColorSpaceTypes.LCH))).to.equal('lch(50 0 0)');
expect(transformColorModifiers(token(ColorSpaceTypes.P3))).to.equal(
'color(display-p3 0.47 0.47 0.47)',
);
expect(transformColorModifiers(token(ColorSpaceTypes.SRGB))).to.equal(
'rgb(46.6% 46.6% 46.6%)',
'color(display-p3 0.5 0.5 0.5)',
);
expect(transformColorModifiers(token(ColorSpaceTypes.SRGB))).to.equal('rgb(50% 50% 50%)');
// without space, return original
expect(transformColorModifiers(token(''))).to.equal('#000000');
});
Expand Down

0 comments on commit 7617f9d

Please sign in to comment.