From dc9ba596c0e339d47d21e33ad9d80b30e2a93f67 Mon Sep 17 00:00:00 2001 From: Rustom Ichhaporia <45826892+rustom@users.noreply.github.com> Date: Sat, 8 May 2021 19:06:32 -0500 Subject: [PATCH] Add dark and light switching functionality with tinycolor --- src/js/theme-controller.js | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/js/theme-controller.js b/src/js/theme-controller.js index 92a915e7b..208189c9b 100644 --- a/src/js/theme-controller.js +++ b/src/js/theme-controller.js @@ -4,6 +4,7 @@ import * as Misc from "./misc"; import * as Notifications from "./notifications"; import Config from "./config"; import * as UI from "./ui"; +import tinycolor from "tinycolor2"; let isPreviewingTheme = false; export let randomTheme = null; @@ -132,12 +133,21 @@ export function clearPreview() { export function randomizeTheme() { var randomList; Misc.getThemesList().then((themes) => { - randomList = themes.map((t) => { - return t.name; - }); - - if (Config.randomTheme === "fav" && Config.favThemes.length > 0) + if (Config.randomTheme === "fav" && Config.favThemes.length > 0) { randomList = Config.favThemes; + } else if (Config.randomTheme === "light") { + randomList = themes + .filter((t) => tinycolor(t.bgColor).isLight()) + .map((t) => t.name); + } else if (Config.randomTheme === "dark") { + randomList = themes + .filter((t) => tinycolor(t.bgColor).isDark()) + .map((t) => t.name); + } else { + randomList = themes.map((t) => { + return t.name; + }); + } const previousTheme = randomTheme; randomTheme = randomList[Math.floor(Math.random() * randomList.length)];