diff options
| author | Danilo M. <danix@danix.xyz> | 2026-11-16 11:02:00 +0100 |
|---|---|---|
| committer | Danilo M. <danix@danix.xyz> | 2026-11-16 11:02:00 +0100 |
| commit | acc837d722d6905b47f0b673b84b02345867fb6e (patch) | |
| tree | 21767eeb3f63a695cb0570ec23c1ddac31129b14 /desktop/modules/appearance/WallColors.js | |
| parent | 42788ad568e5b600c2dfd11feb2bf7942cf6d584 (diff) | |
| download | quickshell-acc837d722d6905b47f0b673b84b02345867fb6e.tar.gz quickshell-acc837d722d6905b47f0b673b84b02345867fb6e.zip | |
feat(wallpapers): filter by colour, one folder open at a time
A row of colour circles under the mock screens filters the thumbnails
to the wallpapers a colour dominates; a second click clears it, and
folders left empty drop out. magick quantizes a 64 px copy of each
image to six colours, in parallel, and WallColors.js files every colour
under a hue bucket, or dark or light when it has little saturation.
Each image matches one bucket only: the largest colour bucket when it
covers a quarter of the image, else the larger of dark and light.
Matching any bucket over a fifth mixed a red sunset in with blue skies.
Saturation is HSV's, since HSL reads a pale lavender as strongly
coloured. The raw colours are cached by path in
~/.cache/keystone/wallcolors.tsv, so only new files are analysed; the
first run over 843 images took about 20 seconds.
The circles use fixed colours rather than the theme's: they name hue
buckets that do not move with the scheme, and the live scheme reused
one colour under several names, drawing three pairs of twin circles.
Opening a folder now folds whichever was open. A filter that empties
the open folder folds it for real and opens the first folder with
matches; merely hidden, it came back beside the next one opened once
the filter was cleared.
Run: node desktop/modules/appearance/test_wallcolors.js
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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; +} |
