|
| 1 | +import { getLuminance, getContrastRatio } from '../utils/tools.js' |
| 2 | +import { HSLToRGB } from '../utils/convert.js' |
| 3 | + |
| 4 | +const blackLum = getLuminance(HSLToRGB([0, 0, 0])) |
| 5 | + |
| 6 | +export function generateSingleColorCSS(H, S, L) { |
| 7 | + const baseLum = getLuminance(HSLToRGB([H, S, L])) |
| 8 | + const contrast = getContrastRatio(blackLum, baseLum) |
| 9 | + const textL = contrast > 7 ? 10 : 90 |
| 10 | + const textL_REV = contrast < 7 ? 10 : 90 |
| 11 | + |
| 12 | + const lines = [] |
| 13 | + |
| 14 | + const SELECTOR = `.sf-c-${H}\\:${S}\\:${L}` |
| 15 | + const HSL = `hsl(${H}, ${S}%, ${L}%)` |
| 16 | + const LUM = `hsl(${H}, ${S}%, ${textL}%)` |
| 17 | + |
| 18 | + const TX_LUM_REV = `hsl(${H}, ${S}%, ${textL_REV}%)` |
| 19 | + const BG_LUM_REV = `hsl(${H}, ${S}%, ${100 - L}%)` |
| 20 | + |
| 21 | + // var |
| 22 | + lines.push(`${SELECTOR}\\:var { --sf-c-${H}-${S}-${L}: ${H} ${S} ${L} }`) |
| 23 | + |
| 24 | + // light |
| 25 | + lines.push(`${SELECTOR}, ${SELECTOR}\\:st { color: ${LUM}; background-color: ${HSL} }`) |
| 26 | + |
| 27 | + // Text |
| 28 | + // lines.push(`${SELECTOR}\\:txt { color: hsl(${H}, ${S}%, ${L > 50 ? 10 : L}%) }`) |
| 29 | + lines.push(`${SELECTOR}\\:txt, ${SELECTOR}\\:txt\\:st, ${SELECTOR}\\:txt\\:rv { color: ${HSL} }`) |
| 30 | + |
| 31 | + lines.push('') |
| 32 | + |
| 33 | + // dark |
| 34 | + lines.push(`html[data-theme='dark'] ${SELECTOR}, html.dark ${SELECTOR} { color: ${TX_LUM_REV}; background-color: ${BG_LUM_REV} }`) |
| 35 | + lines.push(`html[data-theme='dark'] ${SELECTOR}\\:st, html.dark ${SELECTOR}\\:st { color: ${LUM}; background-color: ${HSL} }`) |
| 36 | + // lines.push(`html[data-theme='dark'] ${SELECTOR}\\:txt, html.dark ${SELECTOR}\\:txt { color: hsl(${H}, ${S}%, ${L < 50 ? 100 - L : L}%) }`) |
| 37 | + lines.push(`html[data-theme='dark'] ${SELECTOR}\\:txt, html.dark ${SELECTOR}\\:txt { color: hsl(${H}, ${S}%, ${90}%) }`) |
| 38 | + lines.push(`html[data-theme='dark'] ${SELECTOR}\\:txt\\:st, html.dark ${SELECTOR}\\:txt\\:st { color: ${HSL} }`) |
| 39 | + lines.push(`html[data-theme='dark'] ${SELECTOR}\\:txt\\:rv, html.dark ${SELECTOR}\\:txt\\:rv { color: hsl(${H}, ${S}%, ${10}%) }`) |
| 40 | + |
| 41 | + lines.push('') |
| 42 | + |
| 43 | + // auto |
| 44 | + lines.push(`@media (prefers-color-scheme: dark) {`) |
| 45 | + lines.push(` html[data-theme='auto'] ${SELECTOR}, html.auto ${SELECTOR} { color: ${TX_LUM_REV}; background-color: ${BG_LUM_REV} }`) |
| 46 | + lines.push(` html[data-theme='auto'] ${SELECTOR}\\:st, html.auto ${SELECTOR}\\:st { color: ${LUM}; background-color: ${HSL} }`) |
| 47 | + // lines.push(` html[data-theme='auto'] ${SELECTOR}\\:txt, html.auto ${SELECTOR}\\:txt { color: hsl(${H}, ${S}%, ${L < 50 ? 100 - L : L}%) }`) |
| 48 | + lines.push(` html[data-theme='auto'] ${SELECTOR}\\:txt, html.auto ${SELECTOR}\\:txt { color: hsl(${H}, ${S}%, ${90}%) }`) |
| 49 | + lines.push(` html[data-theme='auto'] ${SELECTOR}\\:txt\\:st, html.auto ${SELECTOR}\\:txt\\:st { color: ${HSL} }`) |
| 50 | + lines.push(` html[data-theme='auto'] ${SELECTOR}\\:txt\\:rv, html.auto ${SELECTOR}\\:txt\\:rv { color: hsl(${H}, ${S}%, ${10}%) }`) |
| 51 | + lines.push(`}`) |
| 52 | + |
| 53 | + return lines.join('\n') |
| 54 | +} |
0 commit comments