@@ -184,3 +184,55 @@ export const getColorStyle = (color) => {
184184 // Otherwise, treat it as a CSS variable name (fallback to inherit if not defined)
185185 return `var(--${ color } , inherit)`
186186}
187+
188+ /**
189+ * Convert Tailwind color definitions to HSL format suitable for CSS style statements
190+ * Accepts Tailwind color names (e.g., "amber-200", "slate-800") or any chroma-parseable color format
191+ * @param {string } color - Tailwind color name (e.g., "amber-200", "slate-800") or hex/rgb/rgba string
192+ * @param {boolean } includeAlpha - Whether to include alpha channel in output (default: false)
193+ * @returns {string } - HSL color string in format "hsl(h, s%, l%)" or "hsla(h, s%, l%, a)"
194+ * @example
195+ * tailwindToHsl('amber-200') // returns "hsl(45, 93%, 77%)"
196+ * tailwindToHsl('slate-800') // returns "hsl(222, 47%, 11%)"
197+ * tailwindToHsl('blue-500') // returns "hsl(217, 91%, 60%)"
198+ * tailwindToHsl('#3b82f6', true) // returns "hsla(217, 91%, 60%, 1)"
199+ */
200+ // NOTE: DO NOT FLOW TYPE THIS FUNCTION. IT IS IMPORTED BY JSX FILE AND FOR SOME REASON, ROLLUP CHOKES ON FLOW
201+ export const tailwindToHsl = ( color , includeAlpha = false ) => {
202+ if ( ! color ) return null
203+
204+ try {
205+ let colorValue = color
206+
207+ // Check if it's a Tailwind color name (e.g., "amber-200")
208+ if ( typeof color === 'string' && / ^ [ a - z ] + - \d + $ / i. test ( color ) ) {
209+ const [ colorName , shade ] = color . split ( '-' )
210+ const shadeNum = parseInt ( shade , 10 )
211+
212+ if ( TAILWIND_COLORS [ colorName ] && TAILWIND_COLORS [ colorName ] [ shadeNum ] ) {
213+ colorValue = TAILWIND_COLORS [ colorName ] [ shadeNum ]
214+ } else {
215+ // Invalid Tailwind color name
216+ return null
217+ }
218+ }
219+
220+ const chromaColor = chroma ( colorValue )
221+ const hsl = chromaColor . hsl ( )
222+
223+ // chroma returns [h, s, l] where h is 0-360, s and l are 0-1
224+ const h = Math . round ( hsl [ 0 ] || 0 )
225+ const s = Math . round ( hsl [ 1 ] * 100 )
226+ const l = Math . round ( hsl [ 2 ] * 100 )
227+
228+ if ( includeAlpha ) {
229+ const alpha = chromaColor . alpha ( )
230+ return `hsla(${ h } , ${ s } %, ${ l } %, ${ alpha } )`
231+ }
232+
233+ return `hsl(${ h } , ${ s } %, ${ l } %)`
234+ } catch ( error ) {
235+ // If chroma can't parse the color, return null
236+ return null
237+ }
238+ }
0 commit comments