commit 80bdb0f49c9e4e66e4a427bb0a716da23692a987
parent 3a94b2e741fa2af1bfe1df073b9216cbc666fd7e
Author: triesap <137732411+triesap@users.noreply.github.com>
Date: Sat, 15 Feb 2025 06:30:46 +0000
themes: add parse theme util
Diffstat:
2 files changed, 31 insertions(+), 18 deletions(-)
diff --git a/themes/src/index.ts b/themes/src/index.ts
@@ -1,5 +1,5 @@
export { theme_colors } from "./colors";
export { themes } from "./theme";
export type { ColorMode, Theme, ThemeKey, ThemeLayer } from "./types";
-export { parse_color_mode, parse_theme_key } from "./utils";
+export { parse_color_mode, parse_theme, parse_theme_key } from "./utils";
diff --git a/themes/src/utils.ts b/themes/src/utils.ts
@@ -1,34 +1,47 @@
-import type { ColorMode, HslTuple, ThemeDaisy, ThemeKey, ThemeLayers } from "./types";
+import type { ColorMode, HslTuple, Theme, ThemeDaisy, ThemeKey, ThemeLayers } from "./types";
export const hsl = (c: HslTuple): string => `${c[0]}deg ${c[1]}% ${c[2]}%`;
export const hsl_lighten = (c: HslTuple, amt: number = 25): string => hsl([c[0], c[1], c[2] + amt]);
export const hsl_darken = (c: HslTuple, amt: number = 25): string => hsl([c[0], c[1], c[2] - amt]);
export const hsl_css = (c: HslTuple): string => `hsl(${hsl(c)})`;
export const write_daisy = (obj_c: ThemeDaisy): Record<string, string> => Object.fromEntries(
- Object.entries(obj_c).map(([key, val]) => [key, typeof val === `string` ? val : hsl_css(val)])
+ Object.entries(obj_c).map(([key, val]) => [key, typeof val === `string` ? val : hsl_css(val)])
);
+export const parse_theme = (key?: string, color_mode?: string): Theme => {
+ const theme = `${parse_theme_key(key)}_${parse_color_mode(color_mode)}`;
+ switch (theme) {
+ case `theme_os_light`:
+ case `theme_os_dark`:
+ case `theme_garden_light`:
+ case `theme_garden_dark`:
+ return theme;
+ default:
+ return `theme_os_light`;
+ }
+};
+
export const parse_theme_key = (key?: string): ThemeKey => {
- switch (key) {
- case "os":
- case "garden":
- return key;
- default:
- return "os";
- }
+ switch (key) {
+ case "os":
+ case "garden":
+ return key;
+ default:
+ return "os";
+ }
};
export const parse_color_mode = (color_mode?: string): ColorMode => {
- switch (color_mode) {
- case "light":
- case "dark":
- return color_mode;
- default:
- return "light";
- }
+ switch (color_mode) {
+ case "light":
+ case "dark":
+ return color_mode;
+ default:
+ return "light";
+ }
};
-export const write_layers = ({ layer_0: { surface: l0_s, glyphs: l0_g }, layer_1: { surface: l1_s, glyphs: l1_g }, layer_2: { surface: l2_s, glyphs: l2_g }}: ThemeLayers): Record<string, string> => ({
+export const write_layers = ({ layer_0: { surface: l0_s, glyphs: l0_g }, layer_1: { surface: l1_s, glyphs: l1_g }, layer_2: { surface: l2_s, glyphs: l2_g } }: ThemeLayers): Record<string, string> => ({
"--radroots-accent-focus": hsl([331, 68, 48]),
"--layer-0-surface": hsl(l0_s._),
"--layer-0-surface_a": hsl(l0_s._a),