2020-01-12 01:44:06 +00:00
|
|
|
import { convert, brightness, contrastRatio } from 'chromatism'
|
2020-01-23 22:36:32 +00:00
|
|
|
import { alphaBlendLayers, getTextColor, relativeLuminance } from '../color_convert/color_convert.js'
|
2020-01-23 21:19:48 +00:00
|
|
|
import { LAYERS, DEFAULT_OPACITY, SLOT_INHERITANCE } from './pleromafe.js'
|
2020-01-12 01:44:06 +00:00
|
|
|
|
2020-01-19 18:59:54 +00:00
|
|
|
/*
|
|
|
|
* # What's all this?
|
|
|
|
* Here be theme engine for pleromafe. All of this supposed to ease look
|
|
|
|
* and feel customization, making widget styles and make developer's life
|
|
|
|
* easier when it comes to supporting themes. Like many other theme systems
|
|
|
|
* it operates on color definitions, or "slots" - for example you define
|
|
|
|
* "button" color slot and then in UI component Button's CSS you refer to
|
|
|
|
* it as a CSS3 Variable.
|
|
|
|
*
|
|
|
|
* Some applications allow you to customize colors for certain things.
|
|
|
|
* Some UI toolkits allow you to define colors for each type of widget.
|
|
|
|
* Most of them are pretty barebones and have no assistance for common
|
|
|
|
* problems and cases, and in general themes themselves are very hard to
|
|
|
|
* maintain in all aspects. This theme engine tries to solve all of the
|
|
|
|
* common problems with themes.
|
|
|
|
*
|
|
|
|
* You don't have redefine several similar colors if you just want to
|
|
|
|
* change one color - all color slots are derived from other ones, so you
|
|
|
|
* can have at least one or two "basic" colors defined and have all other
|
|
|
|
* components inherit and modify basic ones.
|
|
|
|
*
|
|
|
|
* You don't have to test contrast ratio for colors or pick text color for
|
|
|
|
* each element even if you have light-on-dark elements in dark-on-light
|
|
|
|
* theme.
|
|
|
|
*
|
|
|
|
* You don't have to maintain order of code for inheriting slots from othet
|
|
|
|
* slots - dependency graph resolving does it for you.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* This indicates that this version of code outputs similar theme data and
|
|
|
|
* should be incremented if output changes - for instance if getTextColor
|
|
|
|
* function changes and older themes no longer render text colors as
|
|
|
|
* author intended previously.
|
|
|
|
*/
|
2020-01-12 01:44:06 +00:00
|
|
|
export const CURRENT_VERSION = 3
|
2020-01-19 18:59:54 +00:00
|
|
|
|
2020-01-12 01:44:06 +00:00
|
|
|
export const getLayersArray = (layer, data = LAYERS) => {
|
|
|
|
let array = [layer]
|
|
|
|
let parent = data[layer]
|
|
|
|
while (parent) {
|
|
|
|
array.unshift(parent)
|
|
|
|
parent = data[parent]
|
|
|
|
}
|
|
|
|
return array
|
|
|
|
}
|
|
|
|
|
2020-01-16 19:34:33 +00:00
|
|
|
export const getLayers = (layer, variant = layer, opacitySlot, colors, opacity) => {
|
2020-01-12 01:44:06 +00:00
|
|
|
return getLayersArray(layer).map((currentLayer) => ([
|
|
|
|
currentLayer === layer
|
|
|
|
? colors[variant]
|
|
|
|
: colors[currentLayer],
|
2020-01-16 19:34:33 +00:00
|
|
|
currentLayer === layer
|
|
|
|
? opacity[opacitySlot] || 1
|
2020-01-13 19:30:55 +00:00
|
|
|
: opacity[currentLayer]
|
2020-01-12 01:44:06 +00:00
|
|
|
]))
|
|
|
|
}
|
|
|
|
|
|
|
|
const getDependencies = (key, inheritance) => {
|
|
|
|
const data = inheritance[key]
|
|
|
|
if (typeof data === 'string' && data.startsWith('--')) {
|
|
|
|
return [data.substring(2)]
|
|
|
|
} else {
|
|
|
|
if (data === null) return []
|
|
|
|
const { depends, layer, variant } = data
|
|
|
|
const layerDeps = layer
|
|
|
|
? getLayersArray(layer).map(currentLayer => {
|
|
|
|
return currentLayer === layer
|
|
|
|
? variant || layer
|
|
|
|
: currentLayer
|
|
|
|
})
|
|
|
|
: []
|
|
|
|
if (Array.isArray(depends)) {
|
|
|
|
return [...depends, ...layerDeps]
|
|
|
|
} else {
|
|
|
|
return [...layerDeps]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 18:59:54 +00:00
|
|
|
/**
|
|
|
|
* Sorts inheritance object topologically - dependant slots come after
|
|
|
|
* dependencies
|
|
|
|
*
|
|
|
|
* @property {Object} inheritance - object defining the nodes
|
|
|
|
* @property {Function} getDeps - function that returns dependencies for
|
|
|
|
* given value and inheritance object.
|
|
|
|
* @returns {String[]} keys of inheritance object, sorted in topological
|
2020-01-19 23:44:11 +00:00
|
|
|
* order. Additionally, dependency-less nodes will always be first in line
|
2020-01-19 18:59:54 +00:00
|
|
|
*/
|
2020-01-12 01:44:06 +00:00
|
|
|
export const topoSort = (
|
|
|
|
inheritance = SLOT_INHERITANCE,
|
|
|
|
getDeps = getDependencies
|
|
|
|
) => {
|
|
|
|
// This is an implementation of https://en.wikipedia.org/wiki/Tarjan%27s_strongly_connected_components_algorithm
|
|
|
|
|
|
|
|
const allKeys = Object.keys(inheritance)
|
|
|
|
const whites = new Set(allKeys)
|
|
|
|
const grays = new Set()
|
|
|
|
const blacks = new Set()
|
|
|
|
const unprocessed = [...allKeys]
|
|
|
|
const output = []
|
|
|
|
|
|
|
|
const step = (node) => {
|
|
|
|
if (whites.has(node)) {
|
|
|
|
// Make node "gray"
|
|
|
|
whites.delete(node)
|
|
|
|
grays.add(node)
|
|
|
|
// Do step for each node connected to it (one way)
|
|
|
|
getDeps(node, inheritance).forEach(step)
|
|
|
|
// Make node "black"
|
|
|
|
grays.delete(node)
|
|
|
|
blacks.add(node)
|
|
|
|
// Put it into the output list
|
|
|
|
output.push(node)
|
|
|
|
} else if (grays.has(node)) {
|
|
|
|
console.debug('Cyclic depenency in topoSort, ignoring')
|
|
|
|
output.push(node)
|
|
|
|
} else if (blacks.has(node)) {
|
|
|
|
// do nothing
|
|
|
|
} else {
|
|
|
|
throw new Error('Unintended condition in topoSort!')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (unprocessed.length > 0) {
|
|
|
|
step(unprocessed.pop())
|
|
|
|
}
|
2020-01-19 23:44:11 +00:00
|
|
|
return output.sort((a, b) => {
|
|
|
|
const depsA = getDeps(a, inheritance).length
|
|
|
|
const depsB = getDeps(b, inheritance).length
|
|
|
|
|
|
|
|
if (depsA === depsB || (depsB !== 0 && depsA !== 0)) return 0
|
|
|
|
if (depsA === 0 && depsB !== 0) return -1
|
|
|
|
if (depsB === 0 && depsA !== 0) return 1
|
|
|
|
})
|
2020-01-12 01:44:06 +00:00
|
|
|
}
|
|
|
|
|
2020-01-22 22:35:56 +00:00
|
|
|
const expandSlotValue = (value) => {
|
|
|
|
if (typeof value === 'object') return value
|
|
|
|
return {
|
|
|
|
depends: value.startsWith('--') ? [value.substring(2)] : [],
|
|
|
|
default: value.startsWith('#') ? value : undefined
|
|
|
|
}
|
|
|
|
}
|
2020-01-19 18:59:54 +00:00
|
|
|
/**
|
|
|
|
* retrieves opacity slot for given slot. This goes up the depenency graph
|
|
|
|
* to find which parent has opacity slot defined for it.
|
|
|
|
* TODO refactor this
|
|
|
|
*/
|
2020-01-16 18:53:05 +00:00
|
|
|
export const getOpacitySlot = (
|
2020-01-22 22:35:56 +00:00
|
|
|
k,
|
2020-01-16 18:53:05 +00:00
|
|
|
inheritance = SLOT_INHERITANCE,
|
|
|
|
getDeps = getDependencies
|
|
|
|
) => {
|
2020-01-22 22:35:56 +00:00
|
|
|
const value = expandSlotValue(inheritance[k])
|
2020-01-16 21:09:46 +00:00
|
|
|
if (value.opacity === null) return
|
2020-01-22 22:35:56 +00:00
|
|
|
if (value.opacity) return value.opacity
|
|
|
|
const findInheritedOpacity = (key, visited = [k]) => {
|
|
|
|
const depSlot = getDeps(key, inheritance)[0]
|
2020-01-16 18:53:05 +00:00
|
|
|
if (depSlot === undefined) return
|
2020-01-22 22:35:56 +00:00
|
|
|
const dependency = inheritance[depSlot]
|
2020-01-16 18:53:05 +00:00
|
|
|
if (dependency === undefined) return
|
|
|
|
if (dependency.opacity || dependency === null) {
|
|
|
|
return dependency.opacity
|
2020-01-22 22:35:56 +00:00
|
|
|
} else if (dependency.depends && visited.includes(depSlot)) {
|
|
|
|
return findInheritedOpacity(depSlot, [...visited, depSlot])
|
2020-01-16 18:53:05 +00:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
2020-01-16 21:09:46 +00:00
|
|
|
if (value.depends) {
|
2020-01-22 22:35:56 +00:00
|
|
|
return findInheritedOpacity(k)
|
2020-01-16 21:09:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 18:59:54 +00:00
|
|
|
/**
|
|
|
|
* retrieves layer slot for given slot. This goes up the depenency graph
|
|
|
|
* to find which parent has opacity slot defined for it.
|
|
|
|
* this is basically copypaste of getOpacitySlot except it checks if key is
|
|
|
|
* in LAYERS
|
|
|
|
* TODO refactor this
|
|
|
|
*/
|
2020-01-16 21:09:46 +00:00
|
|
|
export const getLayerSlot = (
|
|
|
|
k,
|
|
|
|
inheritance = SLOT_INHERITANCE,
|
|
|
|
getDeps = getDependencies
|
|
|
|
) => {
|
2020-01-22 22:35:56 +00:00
|
|
|
const value = expandSlotValue(inheritance[k])
|
2020-01-16 21:09:46 +00:00
|
|
|
if (LAYERS[k]) return k
|
|
|
|
if (value.layer === null) return
|
2020-01-22 22:35:56 +00:00
|
|
|
if (value.layer) return value.layer
|
|
|
|
const findInheritedLayer = (key, visited = [k]) => {
|
|
|
|
const depSlot = getDeps(key, inheritance)[0]
|
2020-01-16 21:09:46 +00:00
|
|
|
if (depSlot === undefined) return
|
2020-01-22 22:35:56 +00:00
|
|
|
const dependency = inheritance[depSlot]
|
2020-01-16 21:09:46 +00:00
|
|
|
if (dependency === undefined) return
|
|
|
|
if (dependency.layer || dependency === null) {
|
|
|
|
return dependency.layer
|
|
|
|
} else if (dependency.depends) {
|
2020-01-22 22:35:56 +00:00
|
|
|
return findInheritedLayer(dependency, [...visited, depSlot])
|
2020-01-16 21:09:46 +00:00
|
|
|
} else {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (value.depends) {
|
2020-01-22 22:35:56 +00:00
|
|
|
return findInheritedLayer(k)
|
2020-01-16 18:53:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-19 18:59:54 +00:00
|
|
|
/**
|
2020-01-19 23:44:11 +00:00
|
|
|
* topologically sorted SLOT_INHERITANCE
|
2020-01-19 18:59:54 +00:00
|
|
|
*/
|
2020-01-12 23:56:29 +00:00
|
|
|
export const SLOT_ORDERED = topoSort(
|
|
|
|
Object.entries(SLOT_INHERITANCE)
|
|
|
|
.sort(([aK, aV], [bK, bV]) => ((aV && aV.priority) || 0) - ((bV && bV.priority) || 0))
|
|
|
|
.reduce((acc, [k, v]) => ({ ...acc, [k]: v }), {})
|
2020-01-19 23:44:11 +00:00
|
|
|
)
|
2020-01-12 23:56:29 +00:00
|
|
|
|
2020-01-19 18:59:54 +00:00
|
|
|
/**
|
|
|
|
* All opacity slots used in color slots, their default values and affected
|
|
|
|
* color slots.
|
|
|
|
*/
|
2020-01-16 18:53:05 +00:00
|
|
|
export const OPACITIES = Object.entries(SLOT_INHERITANCE).reduce((acc, [k, v]) => {
|
2020-01-22 22:35:56 +00:00
|
|
|
const opacity = getOpacitySlot(k, SLOT_INHERITANCE, getDependencies)
|
2020-01-16 18:53:05 +00:00
|
|
|
if (opacity) {
|
|
|
|
return {
|
|
|
|
...acc,
|
|
|
|
[opacity]: {
|
|
|
|
defaultValue: DEFAULT_OPACITY[opacity] || 1,
|
|
|
|
affectedSlots: [...((acc[opacity] && acc[opacity].affectedSlots) || []), k]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return acc
|
|
|
|
}
|
|
|
|
}, {})
|
|
|
|
|
2020-01-19 22:34:49 +00:00
|
|
|
/**
|
|
|
|
* Handle dynamic color
|
|
|
|
*/
|
|
|
|
export const computeDynamicColor = (sourceColor, getColor, mod) => {
|
|
|
|
if (typeof sourceColor !== 'string' || !sourceColor.startsWith('--')) return sourceColor
|
|
|
|
let targetColor = null
|
|
|
|
// Color references other color
|
|
|
|
const [variable, modifier] = sourceColor.split(/,/g).map(str => str.trim())
|
|
|
|
const variableSlot = variable.substring(2)
|
|
|
|
targetColor = getColor(variableSlot)
|
|
|
|
if (modifier) {
|
|
|
|
targetColor = brightness(Number.parseFloat(modifier) * mod, targetColor).rgb
|
|
|
|
}
|
|
|
|
return targetColor
|
|
|
|
}
|
|
|
|
|
2020-01-19 18:59:54 +00:00
|
|
|
/**
|
2020-02-06 23:25:26 +00:00
|
|
|
* THE function you want to use. Takes provided colors and opacities
|
2020-01-19 18:59:54 +00:00
|
|
|
* value and uses inheritance data to figure out color needed for the slot.
|
|
|
|
*/
|
2020-02-06 23:25:26 +00:00
|
|
|
export const getColors = (sourceColors, sourceOpacity) => SLOT_ORDERED.reduce(({ colors, opacity }, key) => {
|
2020-01-12 23:56:29 +00:00
|
|
|
const sourceColor = sourceColors[key]
|
2020-02-10 22:34:30 +00:00
|
|
|
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
|
2020-02-06 23:25:26 +00:00
|
|
|
const mod = isLightOnDark ? 1 : -1
|
2020-02-10 22:34:30 +00:00
|
|
|
|
2020-01-16 18:53:05 +00:00
|
|
|
let outputColor = null
|
2020-01-12 23:56:29 +00:00
|
|
|
if (sourceColor) {
|
2020-01-16 18:53:05 +00:00
|
|
|
// Color is defined in source color
|
2020-01-12 23:56:29 +00:00
|
|
|
let targetColor = sourceColor
|
2020-01-16 18:53:05 +00:00
|
|
|
if (targetColor === 'transparent') {
|
2020-01-16 21:09:46 +00:00
|
|
|
// We take only layers below current one
|
|
|
|
const layers = getLayers(
|
2020-01-22 22:35:56 +00:00
|
|
|
getLayerSlot(key),
|
2020-01-16 21:09:46 +00:00
|
|
|
key,
|
2020-01-22 22:35:56 +00:00
|
|
|
getOpacitySlot(key) || key,
|
2020-01-16 21:09:46 +00:00
|
|
|
colors,
|
|
|
|
opacity
|
|
|
|
).slice(0, -1)
|
2020-01-16 18:53:05 +00:00
|
|
|
targetColor = {
|
2020-01-16 21:09:46 +00:00
|
|
|
...alphaBlendLayers(
|
|
|
|
convert('#FF00FF').rgb,
|
|
|
|
layers
|
|
|
|
),
|
2020-01-16 18:53:05 +00:00
|
|
|
a: 0
|
|
|
|
}
|
|
|
|
} else if (typeof sourceColor === 'string' && sourceColor.startsWith('--')) {
|
2020-01-19 22:34:49 +00:00
|
|
|
targetColor = computeDynamicColor(
|
|
|
|
sourceColor,
|
|
|
|
variableSlot => colors[variableSlot] || sourceColors[variableSlot],
|
|
|
|
mod
|
|
|
|
)
|
2020-01-16 18:53:05 +00:00
|
|
|
} else if (typeof sourceColor === 'string' && sourceColor.startsWith('#')) {
|
|
|
|
targetColor = convert(targetColor).rgb
|
2020-01-12 23:56:29 +00:00
|
|
|
}
|
2020-01-16 18:53:05 +00:00
|
|
|
outputColor = { ...targetColor }
|
2020-02-10 22:34:30 +00:00
|
|
|
} else if (value.default) {
|
2020-01-16 18:53:05 +00:00
|
|
|
// same as above except in object form
|
|
|
|
outputColor = convert(value.default).rgb
|
2020-01-12 01:44:06 +00:00
|
|
|
} else {
|
2020-01-16 18:53:05 +00:00
|
|
|
// calculate color
|
2020-01-12 01:44:06 +00:00
|
|
|
const defaultColorFunc = (mod, dep) => ({ ...dep })
|
2020-02-10 22:34:30 +00:00
|
|
|
const colorFunc = value.color || defaultColorFunc
|
2020-01-12 01:44:06 +00:00
|
|
|
|
|
|
|
if (value.textColor) {
|
|
|
|
if (value.textColor === 'bw') {
|
2020-02-10 22:34:30 +00:00
|
|
|
outputColor = contrastRatio(backgroundColor).rgb
|
2020-01-12 01:44:06 +00:00
|
|
|
} else {
|
2020-01-16 18:53:05 +00:00
|
|
|
let color = { ...colors[deps[0]] }
|
2020-01-13 19:30:55 +00:00
|
|
|
if (value.color) {
|
2020-01-23 22:56:47 +00:00
|
|
|
color = colorFunc(mod, ...deps.map((dep) => ({ ...colors[dep] })))
|
2020-01-13 19:30:55 +00:00
|
|
|
}
|
2020-01-16 18:53:05 +00:00
|
|
|
outputColor = getTextColor(
|
2020-02-10 22:34:30 +00:00
|
|
|
backgroundColor,
|
2020-01-16 18:53:05 +00:00
|
|
|
{ ...color },
|
|
|
|
value.textColor === 'preserve'
|
2020-01-12 01:44:06 +00:00
|
|
|
)
|
|
|
|
}
|
2020-01-16 18:53:05 +00:00
|
|
|
} else {
|
|
|
|
// background color case
|
|
|
|
outputColor = colorFunc(
|
|
|
|
mod,
|
|
|
|
...deps.map((dep) => ({ ...colors[dep] }))
|
|
|
|
)
|
2020-01-12 01:44:06 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 18:53:05 +00:00
|
|
|
if (!outputColor) {
|
|
|
|
throw new Error('Couldn\'t generate color for ' + key)
|
|
|
|
}
|
2020-01-22 22:35:56 +00:00
|
|
|
const opacitySlot = getOpacitySlot(key)
|
2020-02-20 16:13:40 +00:00
|
|
|
const ownOpacitySlot = value.opacity
|
|
|
|
if (opacitySlot && (outputColor.a === undefined || ownOpacitySlot)) {
|
2020-02-11 16:21:03 +00:00
|
|
|
const dependencySlot = deps[0]
|
|
|
|
if (dependencySlot && colors[dependencySlot] === 'transparent') {
|
2020-01-23 22:56:47 +00:00
|
|
|
outputColor.a = 0
|
|
|
|
} else {
|
2020-01-27 02:20:13 +00:00
|
|
|
outputColor.a = Number(sourceOpacity[opacitySlot]) || OPACITIES[opacitySlot].defaultValue || 1
|
2020-01-23 22:56:47 +00:00
|
|
|
}
|
2020-01-16 18:53:05 +00:00
|
|
|
}
|
2020-01-16 19:34:33 +00:00
|
|
|
if (opacitySlot) {
|
|
|
|
return {
|
|
|
|
colors: { ...colors, [key]: outputColor },
|
|
|
|
opacity: { ...opacity, [opacitySlot]: outputColor.a }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return {
|
|
|
|
colors: { ...colors, [key]: outputColor },
|
|
|
|
opacity
|
|
|
|
}
|
2020-01-16 18:53:05 +00:00
|
|
|
}
|
|
|
|
}, { colors: {}, opacity: {} })
|