1
1
import StyleDictionary from 'style-dictionary' ;
2
2
import { usesReferences } from 'style-dictionary/utils' ;
3
- import chroma from 'chroma-js ' ;
3
+ import Color from 'colorjs.io ' ;
4
4
5
5
const colorTransform = ( token ) => {
6
6
const { value, modify = [ ] } = token ;
7
- let color = chroma ( value ) ;
7
+
8
+ // This assumes "hex" format, if you want to support { h, s, l } format you have to do
9
+ // `new Color('hsl', [value.h, value.s, value.l]);`
10
+ const color = new Color ( value ) ;
8
11
9
12
// defer until reference is resolved
10
13
if ( typeof modify === 'string' && usesReferences ( modify ) ) {
@@ -18,15 +21,22 @@ const colorTransform = (token) => {
18
21
if ( usesReferences ( type ) || usesReferences ( amount ) ) {
19
22
return undefined ;
20
23
}
21
- // modifier type must match a method name in chromajs
22
- // https://gka.github.io/chroma.js/
23
- // chroma methods can be chained, so each time we override the color variable
24
- // we can still call other chroma methods, similar to
25
- // chroma(value).brighten(1).darken(1).hex();
26
- color = color [ type ] ( amount ) ;
24
+
25
+ switch ( type ) {
26
+ case 'lighten' : {
27
+ const lightness = color . hsl . l ;
28
+ const difference = 100 - lightness ;
29
+ const newLightness = Math . min ( 100 , lightness + difference * amount ) ;
30
+ color . set ( 'hsl.l' , newLightness ) ;
31
+ break ;
32
+ }
33
+ case 'transparentize' :
34
+ color . alpha = Math . max ( 0 , Math . min ( 1 , Number ( amount ) ) ) ;
35
+ break ;
36
+ }
27
37
} ) ;
28
38
29
- return color . hex ( ) ;
39
+ return color . to ( 'srgb' ) . toString ( { format : 'hex' } ) ;
30
40
} ;
31
41
32
42
export default {
@@ -36,21 +46,24 @@ export default {
36
46
37
47
// I am directly defining transforms here
38
48
// This would work if you were to call StyleDictionary.registerTransform() as well
39
- transform : {
40
- colorTransform : {
41
- type : `value` ,
42
- // only transforms that have transitive: true will be applied to tokens
43
- // that alias/reference other tokens
44
- transitive : true ,
45
- filter : ( token ) => token . attributes . category === 'color' && token . modify ,
46
- transform : colorTransform ,
47
- } ,
49
+ hooks : {
50
+ transforms : {
51
+ colorTransform : {
52
+ type : `value` ,
53
+ // only transforms that have transitive: true will be applied to tokens
54
+ // that alias/reference other tokens
55
+ transitive : true ,
56
+ filter : ( token ) => token . attributes . category === 'color' && token . modify ,
57
+ transform : colorTransform ,
58
+ } ,
48
59
49
- // For backwards compatibility, all built-in transforms are not transitive
50
- // by default. This will make the 'color/css' transform transitive
51
- 'color/css' : Object . assign ( { } , StyleDictionary . transform [ `color/css` ] , {
52
- transitive : true ,
53
- } ) ,
60
+ // For backwards compatibility, all built-in transforms are not transitive
61
+ // by default. This will make the 'color/css' transform transitive
62
+ 'color/css' : {
63
+ ...StyleDictionary . hooks . transforms [ `color/css` ] ,
64
+ transitive : true ,
65
+ } ,
66
+ } ,
54
67
} ,
55
68
56
69
platforms : {
0 commit comments