diff --git a/README.md b/README.md index ccb6a1e..c700664 100644 --- a/README.md +++ b/README.md @@ -158,8 +158,6 @@ WallWiz -e -a -d ~/Pictures - `swww` wallpaper daemon handler script. - VS Code theme extension script. - Firefox theme extension script. -- Dunst notification theme extension script. -- `fzf` theme extension script. ## Contributing diff --git a/src/main.js b/src/main.js index 9dbebb7..34fe762 100644 --- a/src/main.js +++ b/src/main.js @@ -167,7 +167,7 @@ class WallWiz { "-d ~/Pics/wallpapers -s 42x10", "-l -p 4x4", ]) - .ver("0.0.1-alpha.6") + .ver("0.0.1-alpha.7") .parse(); // Convert parsed arguments to a more convenient object format diff --git a/themeExtensionScripts/dunst@5hubham5ingh.js b/themeExtensionScripts/dunst@5hubham5ingh.js new file mode 100644 index 0000000..bf1e3dd --- /dev/null +++ b/themeExtensionScripts/dunst@5hubham5ingh.js @@ -0,0 +1,134 @@ +import { getenv, loadFile, open } from "std"; +import { exec } from "os"; + +function hexToRGB(hex) { + const r = parseInt(hex.slice(1, 3), 16); + const g = parseInt(hex.slice(3, 5), 16); + const b = parseInt(hex.slice(5, 7), 16); + return [r, g, b]; +} + +function rgbToHSL(r, g, b) { + r /= 255; + g /= 255; + b /= 255; + const max = Math.max(r, g, b); + const min = Math.min(r, g, b); + let h, s, l = (max + min) / 2; + + if (max === min) { + h = s = 0; + } else { + const d = max - min; + s = l > 0.5 ? d / (2 - max - min) : d / (max + min); + switch (max) { + case r: + h = (g - b) / d + (g < b ? 6 : 0); + break; + case g: + h = (b - r) / d + 2; + break; + case b: + h = (r - g) / d + 4; + break; + } + h /= 6; + } + + return [h * 360, s * 100, l * 100]; +} + +function generateTheme(colors, isDark) { + const sortedColors = [...colors].sort((a, b) => { + const [, , lA] = rgbToHSL(...hexToRGB(a)); + const [, , lB] = rgbToHSL(...hexToRGB(b)); + return isDark ? lA - lB : lB - lA; + }); + + const backgroundIndex = isDark ? 0 : sortedColors.length - 1; + const foregroundIndex = isDark ? sortedColors.length - 1 : 0; + + const background = sortedColors[backgroundIndex]; + const foreground = sortedColors[foregroundIndex]; + + const midIndex = Math.floor(sortedColors.length / 2); + const frameColor = sortedColors[midIndex]; + const criticalColor = sortedColors[Math.floor(midIndex * 0.75)]; + + return { + background, + foreground, + frameColor, + criticalColor, + }; +} + +function getThemeConf(colours, isDark) { + const theme = generateTheme(colours, isDark); + + const config = ` +# WallWiz theme +[global] +frame_color = "${theme.frameColor}" +separator_color = frame + +[urgency_low] +background = "${theme.background}" +foreground = "${theme.foreground}" + +[urgency_normal] +background = "${theme.background}" +foreground = "${theme.foreground}" + +[urgency_critical] +background = "${theme.background}" +foreground = "${theme.foreground}" +frame_color = "${theme.criticalColor}" +# end + `.trim(); + + return config; +} + +function getDarkThemeConf(colours) { + return getThemeConf(colours, true); +} + +function getLightThemeConf(colours) { + return getThemeConf(colours, false); +} + +function setTheme(newThemeConfigPath) { + const dunstConfigPath = getenv("HOME").concat("/.config/dunst/dunstrc"); + const dunstOldConfig = loadFile(dunstConfigPath); + const newThemeConfig = loadFile(newThemeConfigPath); + let filterOut = false; + let updated = false; + const updatedConfig = dunstOldConfig.split("\n") + .filter((line) => { + if (line.includes("# WallWiz theme")) { + filterOut = true; + updated = true; + return false; + } + if (filterOut) { + if (line.includes("# end")) { + filterOut = false; + return false; + } + return false; + } + return true; + }) + .join("\n") + "\n" + newThemeConfig; + + if (!updated) dunstOldConfig.concat("\n", newThemeConfig); + const dunstConfig = open(dunstConfigPath, "w+"); + + dunstConfig.puts(updatedConfig); + dunstConfig.close(); + + exec(["pkill", "dunst"]); +} + +export { getDarkThemeConf, getLightThemeConf, setTheme };