1
1
import { nearestColorFrom } from "../../nearest-color/nearestColor" ;
2
2
import { retrieveTopFill } from "../../common/retrieveFill" ;
3
- import { gradientAngle } from "../../common/color" ;
3
+ import { gradientAngle , rgbTo6hex } from "../../common/color" ;
4
4
import { nearestOpacity , nearestValue } from "../conversionTables" ;
5
+ import { localTailwindSettings } from "../tailwindMain" ;
5
6
6
7
// retrieve the SOLID color for tailwind
7
8
export const tailwindColorFromFills = (
@@ -13,7 +14,7 @@ export const tailwindColorFromFills = (
13
14
14
15
const fill = retrieveTopFill ( fills ) ;
15
16
if ( fill && fill . type === "SOLID" ) {
16
- return tailwindSolidColor ( fill . color , fill . opacity , kind ) ;
17
+ return tailwindSolidColor ( fill , kind ) ;
17
18
} else if (
18
19
fill &&
19
20
( fill . type === "GRADIENT_LINEAR" ||
@@ -22,31 +23,45 @@ export const tailwindColorFromFills = (
22
23
fill . type === "GRADIENT_DIAMOND" )
23
24
) {
24
25
if ( fill . gradientStops . length > 0 ) {
25
- return tailwindSolidColor (
26
- fill . gradientStops [ 0 ] . color ,
27
- fill . opacity ,
28
- kind
29
- ) ;
26
+ return tailwindSolidColor ( fill . gradientStops [ 0 ] , kind ) ;
30
27
}
31
28
}
32
29
return "" ;
33
30
} ;
34
31
35
- export const tailwindSolidColor = (
36
- color : RGB ,
37
- opacity : number | undefined ,
38
- kind : string
39
- ) : string => {
40
- // example: text-opacity-50
41
- // ignore the 100. If opacity was changed, let it be visible.
42
- const opacityProp =
43
- opacity !== 1.0 ? `opacity-${ nearestOpacity ( opacity ?? 1.0 ) } ` : null ;
32
+ /**
33
+ * Get the tailwind token name for a given color
34
+ *
35
+ * - variables: uses the tokenised variable name
36
+ * - colors: uses the nearest Tailwind color name
37
+ */
38
+ export const tailwindSolidColor = ( fill : SolidPaint | ColorStop , kind ?: string ) : string => {
39
+ // example: stone-500 or custom-color-700
40
+ let color : string ;
41
+ if ( localTailwindSettings . customTailwindColors && fill . boundVariables ?. color ) {
42
+ color = getTailwindFromVariable ( fill . boundVariables . color ) ;
43
+ } else {
44
+ const colorValue = "#" + rgbTo6hex ( fill . color ) ;
45
+ const { name, value } = getTailwindFromFigmaRGB ( fill . color ) ;
46
+ if ( localTailwindSettings . roundTailwindColors || colorValue === value ) {
47
+ color = name ;
48
+ } else {
49
+ color = `[#${ rgbTo6hex ( fill . color ) } ]` ;
50
+ }
51
+ }
44
52
45
- // example: text-red-500
46
- const colorProp = `${ kind } -${ getTailwindFromFigmaRGB ( color ) } ${ opacityProp ? `/${ opacityProp } ` : "" } ` ;
53
+ // if no kind, it's a variable stop, so just return the name
54
+ if ( ! kind ) {
55
+ return color ;
56
+ }
47
57
48
- // if fill isn't visible, it shouldn't be painted.
49
- return colorProp ;
58
+ // grab opacity, or set it to full
59
+ const opacity = "opacity" in fill && fill . opacity !== 1.0
60
+ ? `/${ nearestOpacity ( fill . opacity ?? 1.0 ) } `
61
+ : "" ;
62
+
63
+ // example: text-red-500, text-custom-color-700/25
64
+ return `${ kind } -${ color } ${ opacity } ` ;
50
65
} ;
51
66
52
67
/**
@@ -71,24 +86,22 @@ export const tailwindGradient = (fill: GradientPaint): string => {
71
86
const direction = gradientDirection ( gradientAngle ( fill ) ) ;
72
87
73
88
if ( fill . gradientStops . length === 1 ) {
74
- const fromColor = getTailwindFromFigmaRGB ( fill . gradientStops [ 0 ] . color ) ;
89
+ const fromColor = tailwindSolidColor ( fill . gradientStops [ 0 ] ) ;
75
90
76
91
return `${ direction } from-${ fromColor } ` ;
77
92
} else if ( fill . gradientStops . length === 2 ) {
78
- const fromColor = getTailwindFromFigmaRGB ( fill . gradientStops [ 0 ] . color ) ;
79
- const toColor = getTailwindFromFigmaRGB ( fill . gradientStops [ 1 ] . color ) ;
93
+ const fromColor = tailwindSolidColor ( fill . gradientStops [ 0 ] ) ;
94
+ const toColor = tailwindSolidColor ( fill . gradientStops [ 1 ] ) ;
80
95
81
96
return `${ direction } from-${ fromColor } to-${ toColor } ` ;
82
97
} else {
83
- const fromColor = getTailwindFromFigmaRGB ( fill . gradientStops [ 0 ] . color ) ;
98
+ const fromColor = tailwindSolidColor ( fill . gradientStops [ 0 ] ) ;
84
99
85
100
// middle (second color)
86
- const viaColor = getTailwindFromFigmaRGB ( fill . gradientStops [ 1 ] . color ) ;
101
+ const viaColor = tailwindSolidColor ( fill . gradientStops [ 1 ] ) ;
87
102
88
103
// last
89
- const toColor = getTailwindFromFigmaRGB (
90
- fill . gradientStops [ fill . gradientStops . length - 1 ] . color
91
- ) ;
104
+ const toColor = tailwindSolidColor ( fill . gradientStops [ fill . gradientStops . length - 1 ] ) ;
92
105
93
106
return `${ direction } from-${ fromColor } via-${ viaColor } to-${ toColor } ` ;
94
107
}
@@ -368,16 +381,17 @@ export const tailwindNearestColor = nearestColorFrom(
368
381
) ;
369
382
370
383
// figma uses r,g,b in [0, 1], while nearestColor uses it in [0, 255]
371
- export const getTailwindFromFigmaRGB = ( color : RGB ) : string => {
384
+ export const getTailwindFromFigmaRGB = ( color : RGB ) => {
372
385
const colorMultiplied = {
373
386
r : color . r * 255 ,
374
387
g : color . g * 255 ,
375
388
b : color . b * 255 ,
376
389
} ;
377
-
378
- return tailwindColors [ tailwindNearestColor ( colorMultiplied ) ] ;
390
+ const value = tailwindNearestColor ( colorMultiplied ) ;
391
+ const name = tailwindColors [ value ] ;
392
+ return { name, value } ;
379
393
} ;
380
394
381
- export const getTailwindColor = ( color : string | RGB ) : string => {
382
- return tailwindColors [ tailwindNearestColor ( color ) ] ;
395
+ export const getTailwindFromVariable = ( alias : VariableAlias ) => {
396
+ return figma . variables . getVariableById ( alias . id ) ?. name . replaceAll ( "/" , "-" ) || alias . id ;
383
397
} ;
0 commit comments