diff --git a/src/lib/CustomTheme.js b/src/lib/CustomTheme.js index bcb26de3..26610f10 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: 1, + orange: "#f9a636", + background: "#ffffff", + foreground: "#000000", + foregroundOrange: "#ffffff", + tinting: "#252525", +}; + +export let fallback_fallback = { + v: 1, + 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"); @@ -93,16 +105,28 @@ export function applyTheme(theme) { if (![1].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)); - currentCustomTheme.set(theme); useCustomTheme.set(true); } +export function getTintColor(dark,theme) { + if (dark) { + return "#" + addHexColor(theme.orange.slice(1), theme.tinting.slice(1)); + } else { + return "#" + removeHexColor(theme.orange.slice(1), theme.tinting.slice(1)); + } +} + 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/clmanager.js b/src/lib/clmanager.js index 72e5cd81..ab3bfef8 100644 --- a/src/lib/clmanager.js +++ b/src/lib/clmanager.js @@ -43,7 +43,12 @@ user.subscribe(v => { if (_userLoaded) { _user = v; if (_user.theme.startsWith("custom:")) { - applyTheme(stringToTheme(_user.theme)); + try { + applyTheme(stringToTheme(_user.theme)); + } catch (e) { + console.error(`Failed to apply custom theme: ${e}`); + removeTheme(); + } } else { removeTheme(); } diff --git a/src/lib/modals/settings/CustomTheme.svelte b/src/lib/modals/settings/CustomTheme.svelte index a28da326..901dbd72 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"; @@ -31,7 +31,12 @@ { if ($user.theme.includes("custom:")) { - applyTheme(stringToTheme($user.theme)); + try { + applyTheme(stringToTheme($user.theme)); + } catch (e) { + console.error(`Failed to apply custom theme: ${e}`); + removeTheme(); + } } else { removeTheme(); } @@ -55,7 +60,12 @@

- + +

{#if error}