11import { nearestColorFrom } from "../../nearest-color/nearestColor" ;
22import { retrieveTopFill } from "../../common/retrieveFill" ;
3- import { gradientAngle } from "../../common/color" ;
3+ import { gradientAngle , rgbTo6hex } from "../../common/color" ;
44import { nearestOpacity , nearestValue } from "../conversionTables" ;
5+ import { localTailwindSettings } from "../tailwindMain" ;
56
67// retrieve the SOLID color for tailwind
78export const tailwindColorFromFills = (
@@ -13,7 +14,7 @@ export const tailwindColorFromFills = (
1314
1415 const fill = retrieveTopFill ( fills ) ;
1516 if ( fill && fill . type === "SOLID" ) {
16- return tailwindSolidColor ( fill . color , fill . opacity , kind ) ;
17+ return tailwindSolidColor ( fill , kind ) ;
1718 } else if (
1819 fill &&
1920 ( fill . type === "GRADIENT_LINEAR" ||
@@ -22,31 +23,45 @@ export const tailwindColorFromFills = (
2223 fill . type === "GRADIENT_DIAMOND" )
2324 ) {
2425 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 ) ;
3027 }
3128 }
3229 return "" ;
3330} ;
3431
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+ }
4452
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+ }
4757
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 } ` ;
5065} ;
5166
5267/**
@@ -71,24 +86,22 @@ export const tailwindGradient = (fill: GradientPaint): string => {
7186 const direction = gradientDirection ( gradientAngle ( fill ) ) ;
7287
7388 if ( fill . gradientStops . length === 1 ) {
74- const fromColor = getTailwindFromFigmaRGB ( fill . gradientStops [ 0 ] . color ) ;
89+ const fromColor = tailwindSolidColor ( fill . gradientStops [ 0 ] ) ;
7590
7691 return `${ direction } from-${ fromColor } ` ;
7792 } 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 ] ) ;
8095
8196 return `${ direction } from-${ fromColor } to-${ toColor } ` ;
8297 } else {
83- const fromColor = getTailwindFromFigmaRGB ( fill . gradientStops [ 0 ] . color ) ;
98+ const fromColor = tailwindSolidColor ( fill . gradientStops [ 0 ] ) ;
8499
85100 // middle (second color)
86- const viaColor = getTailwindFromFigmaRGB ( fill . gradientStops [ 1 ] . color ) ;
101+ const viaColor = tailwindSolidColor ( fill . gradientStops [ 1 ] ) ;
87102
88103 // last
89- const toColor = getTailwindFromFigmaRGB (
90- fill . gradientStops [ fill . gradientStops . length - 1 ] . color
91- ) ;
104+ const toColor = tailwindSolidColor ( fill . gradientStops [ fill . gradientStops . length - 1 ] ) ;
92105
93106 return `${ direction } from-${ fromColor } via-${ viaColor } to-${ toColor } ` ;
94107 }
@@ -368,16 +381,17 @@ export const tailwindNearestColor = nearestColorFrom(
368381) ;
369382
370383// 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 ) => {
372385 const colorMultiplied = {
373386 r : color . r * 255 ,
374387 g : color . g * 255 ,
375388 b : color . b * 255 ,
376389 } ;
377-
378- return tailwindColors [ tailwindNearestColor ( colorMultiplied ) ] ;
390+ const value = tailwindNearestColor ( colorMultiplied ) ;
391+ const name = tailwindColors [ value ] ;
392+ return { name, value } ;
379393} ;
380394
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 ;
383397} ;
0 commit comments