diff options
| author | Henry Jameson <me@hjkos.com> | 2020-02-11 00:34:30 +0200 |
|---|---|---|
| committer | Henry Jameson <me@hjkos.com> | 2020-02-11 00:34:30 +0200 |
| commit | 38c34b8b51136e3d73506cf5347aa57541b36653 (patch) | |
| tree | 743dbebe9309ddc9e71bcd2a06bd7efc10f9af13 /src/services/theme_data/theme_data.service.js | |
| parent | 611da13a4b252c10f1613d70d877e2d039ba64b7 (diff) | |
fixed eslint, made `mod` work properly depending on context including in shadows
Diffstat (limited to 'src/services/theme_data/theme_data.service.js')
| -rw-r--r-- | src/services/theme_data/theme_data.service.js | 59 |
1 files changed, 30 insertions, 29 deletions
diff --git a/src/services/theme_data/theme_data.service.js b/src/services/theme_data/theme_data.service.js index e9ed3781..7479a55e 100644 --- a/src/services/theme_data/theme_data.service.js +++ b/src/services/theme_data/theme_data.service.js @@ -259,13 +259,34 @@ export const computeDynamicColor = (sourceColor, getColor, mod) => { * value and uses inheritance data to figure out color needed for the slot. */ export const getColors = (sourceColors, sourceOpacity) => SLOT_ORDERED.reduce(({ colors, opacity }, key) => { - const value = SLOT_INHERITANCE[key] - const isObject = typeof value === 'object' - const isString = typeof value === 'string' const sourceColor = sourceColors[key] - const variant = value.variant || value.layer || 'bg' - const isLightOnDark = relativeLuminance(colors[variant] || sourceColors[variant]) < 0.5 + const value = expandSlotValue(SLOT_INHERITANCE[key]) + const deps = getDependencies(key, SLOT_INHERITANCE) + const isTextColor = !!value.textColor + const variant = value.variant || value.layer + + let backgroundColor = null + + if (isTextColor) { + backgroundColor = alphaBlendLayers( + { ...(colors[deps[0]] || convert(sourceColors[key] || '#FF00FF').rgb) }, + getLayers( + getLayerSlot(key) || 'bg', + variant || 'bg', + getOpacitySlot(variant), + colors, + opacity + ) + ) + } else if (variant && variant !== key) { + backgroundColor = colors[variant] || convert(sourceColors[variant]).rgb + } else { + backgroundColor = colors.bg || convert(sourceColors.bg) + } + + const isLightOnDark = relativeLuminance(backgroundColor) < 0.5 const mod = isLightOnDark ? 1 : -1 + let outputColor = null if (sourceColor) { // Color is defined in source color @@ -280,7 +301,6 @@ export const getColors = (sourceColors, sourceOpacity) => SLOT_ORDERED.reduce(({ opacity ).slice(0, -1) targetColor = { - // TODO: try to use alpha-blended background here ...alphaBlendLayers( convert('#FF00FF').rgb, layers @@ -297,43 +317,24 @@ export const getColors = (sourceColors, sourceOpacity) => SLOT_ORDERED.reduce(({ targetColor = convert(targetColor).rgb } outputColor = { ...targetColor } - } else if (isString && value.startsWith('#')) { - // slot: '#000000' shorthand - outputColor = convert(value).rgb - } else if (isObject && value.default) { + } else if (value.default) { // same as above except in object form outputColor = convert(value.default).rgb } else { // calculate color const defaultColorFunc = (mod, dep) => ({ ...dep }) - const deps = getDependencies(key, SLOT_INHERITANCE) - const colorFunc = (isObject && value.color) || defaultColorFunc + const colorFunc = value.color || defaultColorFunc if (value.textColor) { - // textColor case - const bg = alphaBlendLayers( - { ...colors[deps[0]] }, - getLayers( - value.layer, - value.variant || value.layer, - getOpacitySlot(value.variant || value.layer), - colors, - opacity - ) - ) - const isLightOnDark = relativeLuminance(bg) > 0.5 - const mod = isLightOnDark ? 1 : -1 - if (value.textColor === 'bw') { - outputColor = contrastRatio(bg).rgb + outputColor = contrastRatio(backgroundColor).rgb } else { let color = { ...colors[deps[0]] } if (value.color) { color = colorFunc(mod, ...deps.map((dep) => ({ ...colors[dep] }))) } - outputColor = getTextColor( - bg, + backgroundColor, { ...color }, value.textColor === 'preserve' ) |
