import { darken , getLuminance , lighten } from 'polished' ; import { Platform } from '../../libs/platform' ; export function computeThemeColor ( color ) { return Platform . OS === 'web' && color && color . includes ( 'var(--' ) ? typeof getComputedStyle === 'function' ? getComputedStyle ( document . body ) . getPropertyValue ( color . replace ( /var\((.+)\)$/ , '$1' ) ) . trim ( ) : undefined : color ; } export function getLuminanceDifference ( colorA , colorB ) { return getLuminance ( colorA ) - getLuminance ( colorB ) ; } export function getReadableColor ( color , backgroundColor , minimumContrastRatio = 0.4 ) { if ( ! ( color && backgroundColor && minimumContrastRatio > 0 ) ) return color ; try { const luminanceDiff = getLuminanceDifference ( color , backgroundColor ) ; const luminanceDiffAbs = Math . abs ( luminanceDiff ) ; if ( luminanceDiffAbs >= minimumContrastRatio ) return color ; const isDark = getLuminance ( backgroundColor ) <= 0.2 ; return isDark ? lighten ( Math . abs ( minimumContrastRatio - luminanceDiffAbs ) , color ) : darken ( Math . abs ( minimumContrastRatio - luminanceDiffAbs ) , color ) ; } catch ( error ) { console . error ( error ) ; return color ; } } export function fixColorHexWithoutHash ( color ) { if ( color && ( color . length === 6 || color . length === 3 ) && color [ 0 ] !== '#' ) return `#${ color }` ; return color || '' ; }