import Color from "tinycolor2" ; import { memoizedGet as get , Dict , isEmptyObject } from "@chakra-ui/utils" ; param theme param color param fallback export const getColor = ( theme , color , fallback ? ) => { const hex = get ( theme , `colors.${ color }` , color ) ; const isValid = Color ( hex ) . isValid ( ) ; return isValid ? hex : fallback ; } ; param color export const tone = ( color ) => ( theme ) => { const hex = getColor ( theme , color ) ; const isDark = Color ( hex ) . isDark ( ) ; return isDark ? "dark" : "light" ; } ; param color export const isDark = ( color ) => ( theme ) => tone ( color ) ( theme ) === "dark" ; param color export const isLight = ( color ) => ( theme ) => tone ( color ) ( theme ) === "light" ; param color param amount export const transparentize = ( color , opacity ) => ( theme ) => { const raw = getColor ( theme , color ) ; return Color ( raw ) . setAlpha ( opacity ) . toRgbString ( ) ; } ; param color param amount export const whiten = ( color , amount ) => ( theme ) => { const raw = getColor ( theme , color ) ; return Color . mix ( raw , "#fff" , amount ) . toHexString ( ) ; } ; param color param amount export const blacken = ( color , amount ) => ( theme ) => { const raw = getColor ( theme , color ) ; return Color . mix ( raw , "#000" , amount ) . toHexString ( ) ; } ; param color param amount export const darken = ( color , amount ) => ( theme ) => { const raw = getColor ( theme , color ) ; return Color ( raw ) . darken ( amount ) . toHexString ( ) ; } ; param color param amount export const lighten = ( color , amount ) => ( theme ) => Color ( getColor ( theme , color ) ) . lighten ( amount ) . toHexString ( ) ; param fg param bg export const contrast = ( fg , bg ) => ( theme ) => Color . readability ( getColor ( theme , bg ) , getColor ( theme , fg ) ) ; param fg param bg export const isAccessible = ( textColor , bgColor , options ? ) => ( theme ) => Color . isReadable ( getColor ( theme , bgColor ) , getColor ( theme , textColor ) , options ) ; export const complementary = ( color ) => ( theme ) => Color ( getColor ( theme , color ) ) . complement ( ) . toHexString ( ) ; export function generateStripe ( size = "1rem" , color = "rgba(255, 255, 255, 0.15)" ) { return { backgroundImage : `linear-gradient(
    45deg,
    ${ color } 25%,
    transparent 25%,
    transparent 50%,
    ${ color } 50%,
    ${ color } 75%,
    transparent 75%,
    transparent
  )` , backgroundSize : `${ size } ${ size }` , } ; } interface RandomColorOptions { /**
     * If passed, string will be used to generate
     * random color
     */ string ? : string ; /**
     * List of colors to pick from at random
     */ colors ? : string [ ] ; } export function randomColor ( opts ? ) { const fallback = Color . random ( ) . toHexString ( ) ; if ( ! opts || isEmptyObject ( opts ) ) { return fallback ; } if ( opts . string && opts . colors ) { return randomColorFromList ( opts . string , opts . colors ) ; } if ( opts . string && ! opts . colors ) { return randomColorFromString ( opts . string ) ; } if ( opts . colors && ! opts . string ) { return randomFromList ( opts . colors ) ; } return fallback ; } function randomColorFromString ( str ) { let hash = 0 ; if ( str . length === 0 ) return hash . toString ( ) ; for ( let i = 0 ; i < str . length ; i += 1 ) { hash = str . charCodeAt ( i ) + ( ( hash << 5 ) - hash ) ; hash = hash & hash ; } let color = "#" ; for ( let j = 0 ; j < 3 ; j += 1 ) { const value = ( hash >> ( j * 8 ) ) & 255 ; color += `00${ value . toString ( 16 ) }` . substr ( - 2 ) ; } return color ; } function randomColorFromList ( str , list ) { let index = 0 ; if ( str . length === 0 ) return list [ 0 ] ; for ( let i = 0 ; i < str . length ; i += 1 ) { index = str . charCodeAt ( i ) + ( ( index << 5 ) - index ) ; index = index & index ; } index = ( ( index % list . length ) + list . length ) % list . length ; return list [ index ] ; } function randomFromList ( list ) { return list [ Math . floor ( Math . random ( ) * list . length ) ] ; }