import { darken , getLuminance , parseToHsl } from 'polished' ; import { Color } from '@brix-ui/types/palette' ; import type { Theme } from '../../../entity' ; import type { MiscellaneousProps } from '../../miscellaneous' ; export class ColorHelper { public constructor ( private readonly palette , private readonly miscellaneous ) { } public parseColor ( color = '' ) { const fromTheme = this . palette [ color ] || this . miscellaneous [ color . replace ( /-([a-z])/g , ( [ , match ] ) => match . toUpperCase ( ) ) ] ; return fromTheme || color ; } public getContrastingColor ( color ) { return this . isBrightColor ( color ) ? this . palette [ Color . TextBaseStrong ] : this . palette [ Color . TextBaseWeak ] ; } public isBrightColor ( color ) { return getLuminance ( this . flattenAlpha ( color ) ) > 0.46 ; } public flattenAlpha ( color ) { const parsedColor = this . parseColor ( color ) ; const { alpha : amount = 0 } = parseToHsl ( parsedColor ) ; return darken ( amount , parsedColor ) ; } }