import { ClassParser } from 'windicss/utils/parser' ; import { keyOrder } from './order' ; import type { colorObject , DictStr } from 'windicss/types/interfaces' ; export function flatColors ( colors , head ? ) { let flatten = { } ; for ( const [ key , value ] of Object . entries ( colors ) ) { if ( typeof value === 'string' ) { flatten [ ( head && key === 'DEFAULT' ) ? head : head ? `${ head }-${ key }` : key ] = value ; } else if ( typeof value === 'function' ) { flatten [ ( head && key === 'DEFAULT' ) ? head : head ? `${ head }-${ key }` : key ] = 'currentColor' ; } else { flatten = { ... flatten , ... flatColors ( value , head ? `${ head }-${ key }` : key ) } ; } } return flatten ; } export function hex2RGB ( hex ) { const RGB_HEX = /^#?(?:([\da-f]{3})[\da-f]?|([\da-f]{6})(?:[\da-f]{2})?)$/i ; const [ , short , long ] = String ( hex ) . match ( RGB_HEX ) || [ ] ; if ( long ) { const value = Number . parseInt ( long , 16 ) ; return [ value >> 16 , ( value >> 8 ) & 0xff , value & 0xff ] ; } else if ( short ) { return Array . from ( short , ( s ) => Number . parseInt ( s , 16 ) ) . map ( ( n ) => ( n << 4 ) | n ) ; } } export function connectList < T > ( list ) { return list . reduce ( ( previous , current ) => previous . concat ( current ) , [ ] ) ; } export function sortClassNames ( classNames , variantsMap ) { const variantsArray = Object . keys ( variantsMap ) ; const ast = new ClassParser ( classNames , ':' , variantsArray ) . parse ( ) ; return ast . map ( ( { raw , variants , important } ) => { const head = variants . join ( ':' ) + ':' ; const utility = raw . replace ( head , '' ) ; const key = utility . match ( /\w+/ ) ; const hasDynamicValue = utility . match ( /\d+/ ) ; const offset = variants . map ( i => variantsMap [ i ] * 100 ) . reduce ( ( p , c ) => p + c , 0 ) + ( important ? 500 : 0 ) + ( hasDynamicValue ? 25 : 0 ) ; if ( key === null ) return { raw , weight : offset } ; return { raw , weight : ( keyOrder [ key [ 0 ] ] ?? 300 ) + offset } ; } ) . sort ( ( a , b ) => a . weight - b . weight ) . map ( i => i . raw ) ; } export function getAllSeparators ( text ) { const separators = [ '' ] ; let last = '' ; if ( ! /\s/g . test ( text [ 0 ] ) ) { separators . push ( '' ) ; } for ( const ch of text ) { if ( /\s/ . test ( ch ) ) { separators [ separators . length - 1 ] += ch ; last = ch ; } else { if ( /\s/g . test ( last ) ) { separators . push ( '' ) ; last = ch ; } } } return separators ; } export function combineSeparators ( separators , sortedP ) { let ret = '' ; let i = 0 ; for ( i = 0 ; i < sortedP . length ; i ++ ) { ret += separators [ i ] ; ret += sortedP [ i ] ; } ret += separators [ i ] ; return ret ; } export function rem2px ( str ? ) { if ( ! str ) return ; let index = 0 ; const output = [ ] ; while ( index < str . length ) { const rem = str . slice ( index ) . match ( /-?[\d.]+rem;/ ) ; if ( ! rem || ! rem . index ) break ; const px = ` /* ${ parseFloat ( rem [ 0 ] . slice ( 0 , - 4 ) ) * 16 }px */` ; const end = index + rem . index + rem [ 0 ] . length ; output . push ( str . slice ( index , end ) ) ; output . push ( px ) ; index = end ; } output . push ( str . slice ( index ) ) ; return output . join ( '' ) ; } function componentToHex ( c ) { const hex = c . toString ( 16 ) ; return hex . length === 1 ? '0' + hex : hex ; } export function rgb2Hex ( r , g , b ) { return '#' + componentToHex ( r * 255 ) + componentToHex ( g * 255 ) + componentToHex ( b * 255 ) ; } function match ( a , b ) { return a [ 0 ] === b [ 0 ] && a [ 1 ] === b [ 1 ] && a [ 2 ] === b [ 2 ] ; } export function isDarkColor ( r , g , b ) { if ( match ( [ r , g , b ] , [ 20 , 184 , 166 ] ) || match ( [ r , g , b ] , [ 245 , 158 , 11 ] ) || match ( [ r , g , b ] , [ 249 , 115 , 22 ] ) || match ( [ r , g , b ] , [ 217 , 70 , 239 ] ) || match ( [ r , g , b ] , [ 6 , 182 , 212 ] ) || match ( [ r , g , b ] , [ 132 , 204 , 22 ] ) ) return true ; const hsp = Math . sqrt ( 0.299 * ( r * r ) + 0.587 * ( g * g ) + 0.114 * ( b * b ) ) ; return hsp <= 150 ; } export function arrayEqual ( array1 , array2 ) { return array1 . length === array2 . length && array1 . every ( ( value , index ) => value === array2 [ index ] ) ; }