import { get, writable } from "svelte/store"; import { DataBase, setDatabase } from "../storage/database"; import { downloadFile } from "../storage/globalApi"; import { BufferToText, selectSingleFile } from "../util"; import { alertError } from "../alert"; import { isLite } from "../lite"; import { CustomCSSStore, SafeModeStore } from "../stores"; export interface ColorScheme{ bgcolor: string; darkbg: string; borderc: string; selected: string; draculared: string; textcolor: string; textcolor2: string; darkBorderc: string; darkbutton: string; type:'light'|'dark'; } export const defaultColorScheme: ColorScheme = { bgcolor: "#282a36", darkbg: "#21222c", borderc: "#6272a4", selected: "#44475a", draculared: "#ff5555", textcolor: "#f8f8f2", textcolor2: "#64748b", darkBorderc: "#4b5563", darkbutton: "#374151", type:'dark' } const colorShemes = { "default": defaultColorScheme, "light": { bgcolor: "#f0f0f0", darkbg: "#ffffff", borderc: "#0f172a", selected: "#e0e0e0", draculared: "#ff5555", textcolor: "#0f172a", textcolor2: "#64748b", darkBorderc: "#d1d5db", darkbutton: "#e5e7eb", type:'light' }, "cherry": { bgcolor: "#450a0a", darkbg: "#7f1d1d", borderc: "#ea580c", selected: "#d97706", draculared: "#ff5555", textcolor: "#f8f8f2", textcolor2: "#fca5a5", darkBorderc: "#92400e", darkbutton: "#b45309", type:'dark' }, "galaxy": { bgcolor: "#0f172a", darkbg: "#1f2a48", borderc: "#8be9fd", selected: "#457b9d", draculared: "#ff5555", textcolor: "#f8f8f2", textcolor2: "#8be9fd", darkBorderc: "#457b9d", darkbutton: "#1f2a48", type:'dark' }, "nature": { bgcolor: "#1b4332", darkbg: "#2d6a4f", borderc: "#a8dadc", selected: "#4d908e", draculared: "#ff5555", textcolor: "#f8f8f2", textcolor2: "#4d908e", darkBorderc: "#457b9d", darkbutton: "#2d6a4f", type:'dark' }, "realblack": { bgcolor: "#000000", darkbg: "#000000", borderc: "#6272a4", selected: "#44475a", draculared: "#ff5555", textcolor: "#f8f8f2", textcolor2: "#64748b", darkBorderc: "#4b5563", darkbutton: "#374151", type:'dark' } } as const export const ColorSchemeTypeStore = writable('dark' as 'dark'|'light') export const colorSchemeList = Object.keys(colorShemes) as (keyof typeof colorShemes)[] export function changeColorScheme(colorScheme: string){ let db = get(DataBase) if(colorScheme !== 'custom'){ db.colorScheme = structuredClone(colorShemes[colorScheme]) } db.colorSchemeName = colorScheme setDatabase(db) updateColorScheme() } export function updateColorScheme(){ let db = get(DataBase) let colorScheme = db.colorScheme if(colorScheme == null){ colorScheme = structuredClone(defaultColorScheme) } if(get(isLite)){ colorScheme = structuredClone(colorShemes.light) } //set css variables document.documentElement.style.setProperty("--risu-theme-bgcolor", colorScheme.bgcolor); document.documentElement.style.setProperty("--risu-theme-darkbg", colorScheme.darkbg); document.documentElement.style.setProperty("--risu-theme-borderc", colorScheme.borderc); document.documentElement.style.setProperty("--risu-theme-selected", colorScheme.selected); document.documentElement.style.setProperty("--risu-theme-draculared", colorScheme.draculared); document.documentElement.style.setProperty("--risu-theme-textcolor", colorScheme.textcolor); document.documentElement.style.setProperty("--risu-theme-textcolor2", colorScheme.textcolor2); document.documentElement.style.setProperty("--risu-theme-darkborderc", colorScheme.darkBorderc); document.documentElement.style.setProperty("--risu-theme-darkbutton", colorScheme.darkbutton); ColorSchemeTypeStore.set(colorScheme.type) } export function exportColorScheme(){ let db = get(DataBase) let json = JSON.stringify(db.colorScheme) downloadFile('colorScheme.json', json) } export async function importColorScheme(){ const uarray = await selectSingleFile(['json']) if(uarray == null){ return } const string = BufferToText(uarray.data) let colorScheme: ColorScheme try{ colorScheme = JSON.parse(string) if( typeof colorScheme.bgcolor !== 'string' || typeof colorScheme.darkbg !== 'string' || typeof colorScheme.borderc !== 'string' || typeof colorScheme.selected !== 'string' || typeof colorScheme.draculared !== 'string' || typeof colorScheme.textcolor !== 'string' || typeof colorScheme.textcolor2 !== 'string' || typeof colorScheme.darkBorderc !== 'string' || typeof colorScheme.darkbutton !== 'string' || typeof colorScheme.type !== 'string' ){ alertError('Invalid color scheme') return } changeColorScheme('custom') let db = get(DataBase) db.colorScheme = colorScheme setDatabase(db) updateColorScheme() } catch(e){ alertError('Invalid color scheme') return } } export function updateTextThemeAndCSS(){ let db = get(DataBase) const root = document.querySelector(':root') as HTMLElement; if(!root){ return } let textTheme = get(isLite) ? 'standard' : db.textTheme let colorScheme = get(isLite) ? 'light' : db.colorScheme.type switch(textTheme){ case "standard":{ if(colorScheme === 'dark'){ root.style.setProperty('--FontColorStandard', '#fafafa'); root.style.setProperty('--FontColorItalic', '#8C8D93'); root.style.setProperty('--FontColorBold', '#fafafa'); root.style.setProperty('--FontColorItalicBold', '#8C8D93'); root.style.setProperty('--FontColorQuote1', '#8BE9FD'); root.style.setProperty('--FontColorQuote2', '#FFB86C'); }else{ root.style.setProperty('--FontColorStandard', '#0f172a'); root.style.setProperty('--FontColorItalic', '#8C8D93'); root.style.setProperty('--FontColorBold', '#0f172a'); root.style.setProperty('--FontColorItalicBold', '#8C8D93'); root.style.setProperty('--FontColorQuote1', '#8BE9FD'); root.style.setProperty('--FontColorQuote2', '#FFB86C'); } break } case "highcontrast":{ if(colorScheme === 'dark'){ root.style.setProperty('--FontColorStandard', '#f8f8f2'); root.style.setProperty('--FontColorItalic', '#F1FA8C'); root.style.setProperty('--FontColorBold', '#8BE9FD'); root.style.setProperty('--FontColorItalicBold', '#FFB86C'); root.style.setProperty('--FontColorQuote1', '#8BE9FD'); root.style.setProperty('--FontColorQuote2', '#FFB86C'); } else{ root.style.setProperty('--FontColorStandard', '#0f172a'); root.style.setProperty('--FontColorItalic', '#F1FA8C'); root.style.setProperty('--FontColorBold', '#8BE9FD'); root.style.setProperty('--FontColorItalicBold', '#FFB86C'); root.style.setProperty('--FontColorQuote1', '#8BE9FD'); root.style.setProperty('--FontColorQuote2', '#FFB86C'); } break } case "custom":{ root.style.setProperty('--FontColorStandard', db.customTextTheme.FontColorStandard); root.style.setProperty('--FontColorItalic', db.customTextTheme.FontColorItalic); root.style.setProperty('--FontColorBold', db.customTextTheme.FontColorBold); root.style.setProperty('--FontColorItalicBold', db.customTextTheme.FontColorItalicBold); root.style.setProperty('--FontColorQuote1', db.customTextTheme.FontColorQuote1 ?? '#8BE9FD'); root.style.setProperty('--FontColorQuote2', db.customTextTheme.FontColorQuote2 ?? '#FFB86C'); break } } switch(db.font){ case "default":{ root.style.setProperty('--risu-font-family', 'Arial, sans-serif'); break } case "timesnewroman":{ root.style.setProperty('--risu-font-family', 'Times New Roman, serif'); break } case "custom":{ root.style.setProperty('--risu-font-family', db.customFont); break } } if(!get(SafeModeStore)){ CustomCSSStore.set(db.customCSS ?? '') } else{ CustomCSSStore.set('') } }