diff options
Diffstat (limited to 'desktop/modules/appearance/WallColors.js')
| -rw-r--r-- | desktop/modules/appearance/WallColors.js | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/desktop/modules/appearance/WallColors.js b/desktop/modules/appearance/WallColors.js new file mode 100644 index 0000000..749f5cb --- /dev/null +++ b/desktop/modules/appearance/WallColors.js @@ -0,0 +1,70 @@ +.pragma library +// Copyright (C) 2026 Danilo M. <danix@danix.xyz> +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License version 2 as +// published by the Free Software Foundation. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// The wallpaper page's colour filter, tested by test_wallcolors.js under node. +// Each image is quantized to six colours by magick (Wallpapers.qml), and each +// colour falls in one bucket here. An image matches one bucket only, its +// dominant one: the largest colour bucket when it covers a quarter of the +// image, else the larger of dark and light. Matching any bucket over a share +// mixed a red sunset into blue for its sky. +// ponytail: six colours and fixed hue cuts; tune the cuts and MIN if the +// matches feel off. + +var BUCKETS = ["red", "peach", "yellow", "green", "teal", "blue", "mauve", "dark", "light"]; +var MIN = 0.25; + +// "#RRGGBB" -> a bucket name, or "" for a mid grey. +function bucket(hex) { + const r = parseInt(hex.slice(1, 3), 16) / 255, + g = parseInt(hex.slice(3, 5), 16) / 255, + b = parseInt(hex.slice(5, 7), 16) / 255; + const max = Math.max(r, g, b), min = Math.min(r, g, b), d = max - min; + const l = (max + min) / 2; + // HSV saturation: HSL reads a pale lavender like #cad3f5 as strongly + // coloured, which is the "light" the swatch means. + const s = max === 0 ? 0 : d / max; + if (l < 0.15) return "dark"; + if (s < 0.2) return l < 0.35 ? "dark" : l > 0.65 ? "light" : ""; + let h = max === r ? ((g - b) / d) % 6 : max === g ? (b - r) / d + 2 : (r - g) / d + 4; + h = (h * 60 + 360) % 360; + return h < 15 || h >= 335 ? "red" : h < 40 ? "peach" : h < 70 ? "yellow" + : h < 160 ? "green" : h < 200 ? "teal" : h < 255 ? "blue" : "mauve"; +} + +// "#RRGGBB:count,..." -> { bucket: fraction of the image }. +function parse(line) { + const out = {}; + let total = 0; + for (const part of line.split(",")) { + const i = part.indexOf(":"); + if (i < 0) continue; + const n = +part.slice(i + 1), k = bucket(part.slice(0, i)); + total += n; + if (k) out[k] = (out[k] || 0) + n; + } + for (const k in out) out[k] /= total; + return out; +} + +// The bucket an image is filed under, "" for none (mostly mid grey). +function dominant(fractions) { + let best = "", bestF = 0; + for (const k in fractions) + if (k !== "dark" && k !== "light" && fractions[k] > bestF) { best = k; bestF = fractions[k]; } + if (bestF >= MIN) return best; + const d = fractions.dark || 0, l = fractions.light || 0; + return d === 0 && l === 0 ? "" : d >= l ? "dark" : "light"; +} + +function matches(fractions, key) { + return !!fractions && dominant(fractions) === key; +} |
