|
| 1 | +/* |
| 2 | + * The MIT License (MIT) |
| 3 | + * |
| 4 | + * Copyright (c) 2015 - present Instructure, Inc. |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + */ |
| 24 | +import { colorToHsla } from '@instructure/ui-color-utils' |
| 25 | + |
| 26 | +type ModifyColor = { |
| 27 | + value: string |
| 28 | + modify: { |
| 29 | + type: 'lighten' | 'darken' |
| 30 | + value: number |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// Matches Tokens Studio's HSL modifier math: move L by `amount` (0–1) of the |
| 35 | +// remaining distance toward the endpoint, so the step shrinks as it approaches |
| 36 | +// black/white and never overshoots. |
| 37 | +const modifyLightness = ( |
| 38 | + l: number, |
| 39 | + amount: number, |
| 40 | + type: 'darken' | 'lighten' |
| 41 | +): number => |
| 42 | + type === 'darken' |
| 43 | + ? Math.max(0, l - l * amount) |
| 44 | + : Math.min(1, l + (1 - l) * amount) |
| 45 | + |
| 46 | +const formatHsla = (h: number, s: number, l: number, a: number): string => { |
| 47 | + const hh = Math.round(h) |
| 48 | + const ss = +(s * 100).toFixed(2) |
| 49 | + const ll = +(l * 100).toFixed(2) |
| 50 | + return a < 1 |
| 51 | + ? `hsla(${hh}, ${ss}%, ${ll}%, ${a})` |
| 52 | + : `hsl(${hh}, ${ss}%, ${ll}%)` |
| 53 | +} |
| 54 | + |
| 55 | +/** |
| 56 | + * Resolves a component theme object by applying color modifiers to any entries |
| 57 | + * shaped as `{ value, modify: { type, value } }`. Entries with `modify.type` of |
| 58 | + * `'darken'` or `'lighten'` are transformed in HSL space using the same math |
| 59 | + * Tokens Studio applies (`space: "hsl"`): `amount` is a 0–1 fraction of the |
| 60 | + * remaining distance to black/white. Plain string values and unrecognized |
| 61 | + * modifier types are passed through unchanged. |
| 62 | + * |
| 63 | + * @param componentTheme - Theme map whose values are either plain CSS color strings |
| 64 | + * or `ModifyColor` objects describing a base color and a darken/lighten modifier. |
| 65 | + * @returns A new theme object with the same keys, where modifier objects have been |
| 66 | + * collapsed to their final resolved color string. |
| 67 | + */ |
| 68 | +const applyColorModifiers = ( |
| 69 | + componentTheme: Record<string, string | ModifyColor> | undefined | null |
| 70 | +) => { |
| 71 | + if (componentTheme == null) return {} |
| 72 | + return Object.keys(componentTheme).reduce<Record<string, string>>( |
| 73 | + (res, k) => { |
| 74 | + const entry = componentTheme[k] |
| 75 | + if (typeof entry === 'object' && entry !== null) { |
| 76 | + const { value, modify } = entry |
| 77 | + if (modify.type === 'darken' || modify.type === 'lighten') { |
| 78 | + const { h, s, l, a } = colorToHsla(value) |
| 79 | + const newL = modifyLightness(l, modify.value, modify.type) |
| 80 | + return { ...res, [k]: formatHsla(h, s, newL, a) } |
| 81 | + } |
| 82 | + return { ...res, [k]: value } |
| 83 | + } |
| 84 | + return { ...res, [k]: entry } |
| 85 | + }, |
| 86 | + {} |
| 87 | + ) |
| 88 | +} |
| 89 | + |
| 90 | +export default applyColorModifiers |
| 91 | +export { applyColorModifiers } |
0 commit comments