From cd3820a3932099d3e39af9e4f98bbafc39b240a9 Mon Sep 17 00:00:00 2001 From: Bloctans <93960460+Bloctans@users.noreply.github.com> Date: Mon, 29 Jan 2024 16:50:52 -0600 Subject: [PATCH 1/6] little overhaul to custom themes --- src/lib/CustomTheme.js | 117 ++++++++++++++------- src/lib/modals/settings/CustomTheme.svelte | 9 +- src/pages/_layout.svelte | 6 +- 3 files changed, 89 insertions(+), 43 deletions(-) diff --git a/src/lib/CustomTheme.js b/src/lib/CustomTheme.js index bcb26de3..3ce881eb 100644 --- a/src/lib/CustomTheme.js +++ b/src/lib/CustomTheme.js @@ -1,39 +1,6 @@ // Custom themes. -export let fallback = { - v: 1, - orange: "#f9a636", - orangeLight: "#ffce8c", - orangeDark: "#b46d34", - background: "#ffffff", - foreground: "#000000", - foregroundOrange: "#ffffff", - tinting: "#252525", -}; - -import {useCustomTheme, customTheme as currentCustomTheme} from "./stores.js"; - -export function themeToString(theme) { - return "custom:" + JSON.stringify(theme); -} - -export function stringToTheme(string) { - try { - const json = JSON.parse(string.replace("custom:", "")); - return { - v: json.v || fallback.v, - orange: json.orange || fallback.orange, - background: json.background || fallback.background, - foreground: json.foreground || fallback.foreground, - foregroundOrange: - json.foregroundOrange || fallback.foregroundOrange, - tinting: json.tinting || fallback.tinting, - }; - } catch (e) { - console.error("Error loading custom theme: " + e); - return fallback; - } -} +// Helper functions to remove from hex color export function extractOctets(hex) { const octetsRegex = /^([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i; @@ -70,6 +37,51 @@ export function removeHexColor(c1, c2) { .join(""); } +// Actual code + +export let fallback = { + v: 2, + orange: "#f9a636", + background: "#ffffff", + foreground: "#000000", + foregroundOrange: "#ffffff", + tinting: "#252525", +}; + +export let fallback_fallback = { + v: 2, + orange: "#f9a636", + background: "#ffffff", + foreground: "#000000", + foregroundOrange: "#ffffff", + tinting: "#252525", +}; // for some reason fallback can be overwritten + +import {useCustomTheme, customTheme as currentCustomTheme} from "./stores.js"; + +export function themeToString(theme) { + return "custom:" + JSON.stringify(theme); +} + +export function stringToTheme(string) { + try { + const json = JSON.parse(string.replace("custom:", "")); + return { + v: json.v || fallback.v, + orange: json.orange || fallback.orange, + background: json.background || fallback.background, + foreground: json.foreground || fallback.foreground, + foregroundOrange: + json.foregroundOrange || fallback.foregroundOrange, + tinting: json.tinting || fallback.tinting, + }; + } catch (e) { + console.error("Error loading custom theme: " + e); + return fallback; + } +} + + export function applyTheme(theme) { if (!theme.v) throw new Error("required field 'v' is not defined or is null"); @@ -90,19 +102,46 @@ export function applyTheme(theme) { if (!theme.tinting) throw new Error("required field 'tinting' is not defined or is null"); - if (![1].includes(theme.v)) + if (![1,2].includes(theme.v)) throw new Error(`invalid theme version (${theme.v})`); - theme.orangeLight = - "#" + addHexColor(theme.orange.slice(1), theme.tinting.slice(1)); - theme.orangeDark = - "#" + removeHexColor(theme.orange.slice(1), theme.tinting.slice(1)); + if (theme.v == 1) { + theme.orangeLight = + "#" + addHexColor(theme.orange.slice(1), theme.tinting.slice(1)); + theme.orangeDark = + "#" + removeHexColor(theme.orange.slice(1), theme.tinting.slice(1)); + } currentCustomTheme.set(theme); useCustomTheme.set(true); } +export function getTintColor(dark,theme) { + if (theme.v > 1) { + if (dark) { + return "#" + addHexColor(theme.orange.slice(1), theme.tinting.slice(1)); + } else { + return "#" + removeHexColor(theme.orange.slice(1), theme.tinting.slice(1)); + } + } else { + if (dark) { + return theme.orangeLight + } else { + return theme.orangeDark + } + } +} + export function removeTheme() { useCustomTheme.set(false); currentCustomTheme.set(fallback); } + +export function get_True_Fallback() { + if (fallback == fallback_fallback) { + return fallback + } else { + console.warn("Fallback theme is not correct, resetting to fallback 2"); + return fallback_fallback + } +} \ No newline at end of file diff --git a/src/lib/modals/settings/CustomTheme.svelte b/src/lib/modals/settings/CustomTheme.svelte index a28da326..0164b217 100644 --- a/src/lib/modals/settings/CustomTheme.svelte +++ b/src/lib/modals/settings/CustomTheme.svelte @@ -3,9 +3,9 @@ import {user, customTheme} from "../../stores.js"; import { - fallback, stringToTheme, themeToString, + get_True_Fallback, applyTheme, removeTheme, } from "../../CustomTheme.js"; @@ -55,7 +55,12 @@

- + +

{#if error}