From 45ea91cbb4919eae42550afd46af8e5c20fe3f4b Mon Sep 17 00:00:00 2001 From: Claas Date: Sun, 16 Feb 2025 20:16:27 +0100 Subject: [PATCH] Add contrast options --- index.js | 63 ++++---- theme test.snapshot.json | 336 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 368 insertions(+), 31 deletions(-) diff --git a/index.js b/index.js index e83c8da..41a97f1 100644 --- a/index.js +++ b/index.js @@ -2,18 +2,16 @@ import { argbFromHex, hexFromArgb, TonalPalette, - DynamicScheme, SchemeTonalSpot, Hct, MaterialDynamicColors, DynamicColor, - // Using the repository as dependency as it includes color utilities not yet released } from "@material/material-color-utilities"; import plugin from "tailwindcss/plugin.js"; import defaultConfiguration from "./default.config.js"; -//TODO check back if Tailwind CSS exports types after 4.0.6 +//TODO update Tailwind CSS to a version after 4.0.6 to fix types /** * @import {Config} from "tailwindcss" */ @@ -74,7 +72,7 @@ function createPalettes(materialPalettes) { /** * - * @param {{ light: DynamicScheme, dark: DynamicScheme }} schemes + * @param {Schemes} schemes * @returns {Record>} */ function createColors(schemes) { @@ -113,42 +111,43 @@ function createColors(schemes) { return colors; } -/** @param {string} sourceColor */ -function createTheme(sourceColor) { - // Use new DynamicScheme as in - // https://github.com/material-foundation/material-color-utilities/blob/main/make_schemes.md +/** @typedef {"light-reduced-contrast" | "light" | "light-medium-contrast" | "light-high-contrast" | "dark-reduced-contrast" | "dark" | "dark-medium-contrast" | "dark-high-contrast"} ContrastLevel */ +/** @typedef {Record} Schemes */ + +/** + * Using contrast values as recommended https://github.com/material-foundation/material-color-utilities/blob/9889de141b3b5194b8574f9e378e55f4428bdb5e/dev_guide/creating_color_scheme.md + * + * @param {string} sourceColor + * @returns {Schemes} + */ +function createSchemes(sourceColor) { const color = Hct.fromInt(argbFromHex(sourceColor)); - const light = new SchemeTonalSpot(color, false, 0); - const dark = new SchemeTonalSpot(color, true, 0); - const colors = createColors({ light, dark }); + return { + "light-reduced-contrast": new SchemeTonalSpot(color, false, -1), + light: new SchemeTonalSpot(color, false, 0), + "light-medium-contrast": new SchemeTonalSpot(color, false, 0.5), + "light-high-contrast": new SchemeTonalSpot(color, false, 1), + "dark-reduced-contrast": new SchemeTonalSpot(color, true, -1), + dark: new SchemeTonalSpot(color, true, 0), + "dark-medium-contrast": new SchemeTonalSpot(color, true, 0.5), + "dark-high-contrast": new SchemeTonalSpot(color, true, 1), + }; +} + +/** @param {string} sourceColor */ +function createTheme(sourceColor) { + const schemes = createSchemes(sourceColor); + const colors = createColors(schemes); // The palettes are the same for light and dark /** @type {PaletteArray} */ - const sourcePalettes = Object.entries(light) + const sourcePalettes = Object.entries(schemes.light) .filter(([, value]) => value instanceof TonalPalette) // Remove "palette" postfix .map(([key, value]) => [key.replace("Palette", ""), value]); const palettes = createPalettes(sourcePalettes); - //TODO find out what tone high and medium contrast have - // console.debug("materialTheme", materialTheme); - // let source = Hct.fromInt(0x673ab7); - // const lightPrimary = Hct.fromInt(0x68548e); - // const lightMediumPrimary = Hct.fromInt(0x4b3970); - // const lightHighPrimary = Hct.fromInt(0x2a164d); - // const lightSecondary = Hct.fromInt(0x635b70); - // const lightMediumSecondary = Hct.fromInt(0x473f54); - // const lightHighSecondary = Hct.fromInt(0x251f32); - // console.log("hct", { - // source, - // lightPrimary, - // lightMediumPrimary, - // lightHighPrimary, - // lightSecondary, - // lightMediumSecondary, - // lightHighSecondary, - // }); const tailwindTheme = defaultConfiguration; @@ -180,6 +179,8 @@ export default function materialTailwind(configuration) { //TODO use plugin.withOptions /** @type {Parameters[0]} */ - const creator = function (_api) {}; + const creator = function (api) { + api.matchVariant; + }; return plugin(creator, { theme: tailwindTheme }); } diff --git a/theme test.snapshot.json b/theme test.snapshot.json index 82b13ec..cb607f9 100644 --- a/theme test.snapshot.json +++ b/theme test.snapshot.json @@ -198,6 +198,62 @@ }, "colors": { "source": "#0c1445", + "light-reduced-contrast": { + "primary-palette-key-color": "#6b73ad", + "secondary-palette-key-color": "#74768c", + "tertiary-palette-key-color": "#926c86", + "neutral-palette-key-color": "#77767d", + "neutral-variant-palette-key-color": "#767680", + "background": "#fbf8ff", + "on-background": "#908f96", + "surface": "#fbf8ff", + "surface-dim": "#dbd9e0", + "surface-bright": "#fbf8ff", + "surface-container-lowest": "#ffffff", + "surface-container-low": "#f5f2fa", + "surface-container": "#efedf4", + "surface-container-high": "#e9e7ef", + "surface-container-highest": "#e4e1e9", + "on-surface": "#5f5e65", + "surface-variant": "#e3e1ec", + "on-surface-variant": "#7a7a83", + "inverse-surface": "#303036", + "inverse-on-surface": "#99979e", + "outline": "#b2b1bb", + "outline-variant": "#d9d8e2", + "shadow": "#000000", + "scrim": "#000000", + "surface-tint": "#525a92", + "primary": "#6971aa", + "on-primary": "#fffbff", + "primary-container": "#d3d6ff", + "on-primary-container": "#6e76b0", + "inverse-primary": "#6e76b0", + "secondary": "#717389", + "on-secondary": "#fffbff", + "secondary-container": "#d6d7f0", + "on-secondary-container": "#77788e", + "tertiary": "#8f6983", + "on-tertiary": "#fffbff", + "tertiary-container": "#f9ccea", + "on-tertiary-container": "#956e89", + "error": "#da342e", + "on-error": "#fffbff", + "error-container": "#ffcdc7", + "on-error-container": "#e23a32", + "primary-fixed": "#d3d6ff", + "primary-fixed-dim": "#b1b9f8", + "on-primary-fixed": "#40487e", + "on-primary-fixed-variant": "#5a629b", + "secondary-fixed": "#d6d7f0", + "secondary-fixed-dim": "#babbd3", + "on-secondary-fixed": "#494b5f", + "on-secondary-fixed-variant": "#63657a", + "tertiary-fixed": "#f9ccea", + "tertiary-fixed-dim": "#dcb0cd", + "on-tertiary-fixed": "#63425a", + "on-tertiary-fixed-variant": "#805b75" + }, "light": { "primary-palette-key-color": "#6b73ad", "secondary-palette-key-color": "#74768c", @@ -254,6 +310,174 @@ "on-tertiary-fixed": "#2d1227", "on-tertiary-fixed-variant": "#5d3c54" }, + "light-medium-contrast": { + "primary-palette-key-color": "#6b73ad", + "secondary-palette-key-color": "#74768c", + "tertiary-palette-key-color": "#926c86", + "neutral-palette-key-color": "#77767d", + "neutral-variant-palette-key-color": "#767680", + "background": "#fbf8ff", + "on-background": "#1b1b21", + "surface": "#fbf8ff", + "surface-dim": "#c7c5cd", + "surface-bright": "#fbf8ff", + "surface-container-lowest": "#ffffff", + "surface-container-low": "#f5f2fa", + "surface-container": "#e9e7ef", + "surface-container-high": "#dedce3", + "surface-container-highest": "#d3d0d8", + "on-surface": "#101116", + "surface-variant": "#e3e1ec", + "on-surface-variant": "#35353e", + "inverse-surface": "#303036", + "inverse-on-surface": "#f2eff7", + "outline": "#52525b", + "outline-variant": "#6c6c76", + "shadow": "#000000", + "scrim": "#000000", + "surface-tint": "#525a92", + "primary": "#293267", + "on-primary": "#ffffff", + "primary-container": "#6169a2", + "on-primary-container": "#ffffff", + "inverse-primary": "#bbc3ff", + "secondary": "#333548", + "on-secondary": "#ffffff", + "secondary-container": "#6a6c81", + "on-secondary-container": "#ffffff", + "tertiary": "#4b2c43", + "on-tertiary": "#ffffff", + "tertiary-container": "#87627c", + "on-tertiary-container": "#ffffff", + "error": "#740006", + "on-error": "#ffffff", + "error-container": "#cf2c27", + "on-error-container": "#ffffff", + "primary-fixed": "#6169a2", + "primary-fixed-dim": "#495188", + "on-primary-fixed": "#ffffff", + "on-primary-fixed-variant": "#ffffff", + "secondary-fixed": "#6a6c81", + "secondary-fixed-dim": "#515368", + "on-secondary-fixed": "#ffffff", + "on-secondary-fixed-variant": "#ffffff", + "tertiary-fixed": "#87627c", + "tertiary-fixed-dim": "#6d4a63", + "on-tertiary-fixed": "#ffffff", + "on-tertiary-fixed-variant": "#ffffff" + }, + "light-high-contrast": { + "primary-palette-key-color": "#6b73ad", + "secondary-palette-key-color": "#74768c", + "tertiary-palette-key-color": "#926c86", + "neutral-palette-key-color": "#77767d", + "neutral-variant-palette-key-color": "#767680", + "background": "#fbf8ff", + "on-background": "#1b1b21", + "surface": "#fbf8ff", + "surface-dim": "#b9b8bf", + "surface-bright": "#fbf8ff", + "surface-container-lowest": "#ffffff", + "surface-container-low": "#f2eff7", + "surface-container": "#e4e1e9", + "surface-container-high": "#d5d3db", + "surface-container-highest": "#c7c5cd", + "on-surface": "#000000", + "surface-variant": "#e3e1ec", + "on-surface-variant": "#000000", + "inverse-surface": "#303036", + "inverse-on-surface": "#ffffff", + "outline": "#2b2c34", + "outline-variant": "#484851", + "shadow": "#000000", + "scrim": "#000000", + "surface-tint": "#525a92", + "primary": "#1f275c", + "on-primary": "#ffffff", + "primary-container": "#3d457b", + "on-primary-container": "#ffffff", + "inverse-primary": "#bbc3ff", + "secondary": "#292b3d", + "on-secondary": "#ffffff", + "secondary-container": "#46485c", + "on-secondary-container": "#ffffff", + "tertiary": "#402239", + "on-tertiary": "#ffffff", + "tertiary-container": "#603f57", + "on-tertiary-container": "#ffffff", + "error": "#600004", + "on-error": "#ffffff", + "error-container": "#98000a", + "on-error-container": "#ffffff", + "primary-fixed": "#3d457b", + "primary-fixed-dim": "#262e63", + "on-primary-fixed": "#ffffff", + "on-primary-fixed-variant": "#ffffff", + "secondary-fixed": "#46485c", + "secondary-fixed-dim": "#2f3144", + "on-secondary-fixed": "#ffffff", + "on-secondary-fixed-variant": "#ffffff", + "tertiary-fixed": "#603f57", + "tertiary-fixed-dim": "#47283f", + "on-tertiary-fixed": "#ffffff", + "on-tertiary-fixed-variant": "#ffffff" + }, + "dark-reduced-contrast": { + "primary-palette-key-color": "#6b73ad", + "secondary-palette-key-color": "#74768c", + "tertiary-palette-key-color": "#926c86", + "neutral-palette-key-color": "#77767d", + "neutral-variant-palette-key-color": "#767680", + "background": "#131318", + "on-background": "#626268", + "surface": "#131318", + "surface-dim": "#131318", + "surface-bright": "#39393f", + "surface-container-lowest": "#0d0e13", + "surface-container-low": "#1b1b21", + "surface-container": "#1f1f25", + "surface-container-high": "#29292f", + "surface-container-highest": "#34343a", + "on-surface": "#a3a2a9", + "surface-variant": "#46464f", + "on-surface-variant": "#83828c", + "inverse-surface": "#e4e1e9", + "inverse-on-surface": "#64646a", + "outline": "#53535c", + "outline-variant": "#393942", + "shadow": "#000000", + "scrim": "#000000", + "surface-tint": "#bbc3ff", + "primary": "#858dc8", + "on-primary": "#1c255a", + "primary-container": "#2d366b", + "on-primary-container": "#7880bb", + "inverse-primary": "#757db7", + "secondary": "#8e8fa6", + "on-secondary": "#26283b", + "secondary-container": "#37394c", + "on-secondary-container": "#818399", + "tertiary": "#ad85a0", + "on-tertiary": "#3d2036", + "tertiary-container": "#4f3047", + "on-tertiary-container": "#9f7893", + "error": "#ff5449", + "on-error": "#5c0003", + "error-container": "#7b0007", + "on-error-container": "#f0443b", + "primary-fixed": "#454d84", + "primary-fixed-dim": "#2d366b", + "on-primary-fixed": "#b8c0ff", + "on-primary-fixed-variant": "#949cd9", + "secondary-fixed": "#4e5064", + "secondary-fixed-dim": "#37394c", + "on-secondary-fixed": "#c1c2da", + "on-secondary-fixed-variant": "#9d9eb5", + "tertiary-fixed": "#68465f", + "tertiary-fixed-dim": "#4f3047", + "on-tertiary-fixed": "#e3b7d4", + "on-tertiary-fixed-variant": "#bd94b0" + }, "dark": { "primary-palette-key-color": "#6b73ad", "secondary-palette-key-color": "#74768c", @@ -310,6 +534,118 @@ "on-tertiary-fixed": "#2d1227", "on-tertiary-fixed-variant": "#5d3c54" }, + "dark-medium-contrast": { + "primary-palette-key-color": "#6b73ad", + "secondary-palette-key-color": "#74768c", + "tertiary-palette-key-color": "#926c86", + "neutral-palette-key-color": "#77767d", + "neutral-variant-palette-key-color": "#767680", + "background": "#131318", + "on-background": "#e4e1e9", + "surface": "#131318", + "surface-dim": "#131318", + "surface-bright": "#44444a", + "surface-container-lowest": "#07070c", + "surface-container-low": "#1d1d23", + "surface-container": "#27272d", + "surface-container-high": "#323238", + "surface-container-highest": "#3d3d43", + "on-surface": "#ffffff", + "surface-variant": "#46464f", + "on-surface-variant": "#dddbe6", + "inverse-surface": "#e4e1e9", + "inverse-on-surface": "#292a2f", + "outline": "#b2b1bb", + "outline-variant": "#908f99", + "shadow": "#000000", + "scrim": "#000000", + "surface-tint": "#bbc3ff", + "primary": "#d7daff", + "on-primary": "#182055", + "primary-container": "#858dc8", + "on-primary-container": "#000000", + "inverse-primary": "#3c447a", + "secondary": "#dadaf3", + "on-secondary": "#222437", + "secondary-container": "#8e8fa6", + "on-secondary-container": "#000000", + "tertiary": "#fdcfed", + "on-tertiary": "#391c32", + "tertiary-container": "#ad85a0", + "on-tertiary-container": "#000000", + "error": "#ffd2cc", + "on-error": "#540003", + "error-container": "#ff5449", + "on-error-container": "#000000", + "primary-fixed": "#dfe0ff", + "primary-fixed-dim": "#bbc3ff", + "on-primary-fixed": "#010841", + "on-primary-fixed-variant": "#293267", + "secondary-fixed": "#e0e1f9", + "secondary-fixed-dim": "#c4c5dd", + "on-secondary-fixed": "#0d1021", + "on-secondary-fixed-variant": "#333548", + "tertiary-fixed": "#ffd7f0", + "tertiary-fixed-dim": "#e6bad7", + "on-tertiary-fixed": "#21071c", + "on-tertiary-fixed-variant": "#4b2c43" + }, + "dark-high-contrast": { + "primary-palette-key-color": "#6b73ad", + "secondary-palette-key-color": "#74768c", + "tertiary-palette-key-color": "#926c86", + "neutral-palette-key-color": "#77767d", + "neutral-variant-palette-key-color": "#767680", + "background": "#131318", + "on-background": "#e4e1e9", + "surface": "#131318", + "surface-dim": "#131318", + "surface-bright": "#504f56", + "surface-container-lowest": "#000000", + "surface-container-low": "#1f1f25", + "surface-container": "#303036", + "surface-container-high": "#3b3b41", + "surface-container-highest": "#46464c", + "on-surface": "#ffffff", + "surface-variant": "#46464f", + "on-surface-variant": "#ffffff", + "inverse-surface": "#e4e1e9", + "inverse-on-surface": "#000000", + "outline": "#f1effa", + "outline-variant": "#c3c1cc", + "shadow": "#000000", + "scrim": "#000000", + "surface-tint": "#bbc3ff", + "primary": "#efeeff", + "on-primary": "#000000", + "primary-container": "#b7bffd", + "on-primary-container": "#000434", + "inverse-primary": "#3c447a", + "secondary": "#efeeff", + "on-secondary": "#000000", + "secondary-container": "#c0c1d9", + "on-secondary-container": "#070a1b", + "tertiary": "#ffebf5", + "on-tertiary": "#000000", + "tertiary-container": "#e2b6d3", + "on-tertiary-container": "#1a0316", + "error": "#ffece9", + "on-error": "#000000", + "error-container": "#ffaea4", + "on-error-container": "#220001", + "primary-fixed": "#dfe0ff", + "primary-fixed-dim": "#bbc3ff", + "on-primary-fixed": "#000000", + "on-primary-fixed-variant": "#010841", + "secondary-fixed": "#e0e1f9", + "secondary-fixed-dim": "#c4c5dd", + "on-secondary-fixed": "#000000", + "on-secondary-fixed-variant": "#0d1021", + "tertiary-fixed": "#ffd7f0", + "tertiary-fixed-dim": "#e6bad7", + "on-tertiary-fixed": "#000000", + "on-tertiary-fixed-variant": "#21071c" + }, "primary-palette-key-color": "light-dark(#6b73ad, #6b73ad)", "secondary-palette-key-color": "light-dark(#74768c, #74768c)", "tertiary-palette-key-color": "light-dark(#926c86, #926c86)", -- knotmirror 0.0.1