diff options
Diffstat (limited to 'desktop/modules')
| -rw-r--r-- | desktop/modules/appearance/README.md | 19 | ||||
| -rw-r--r-- | desktop/modules/appearance/WallColors.js | 70 | ||||
| -rw-r--r-- | desktop/modules/appearance/WallpaperTab.qml | 90 | ||||
| -rw-r--r-- | desktop/modules/appearance/Wallpapers.qml | 71 | ||||
| -rw-r--r-- | desktop/modules/appearance/test_wallcolors.js | 47 |
5 files changed, 292 insertions, 5 deletions
diff --git a/desktop/modules/appearance/README.md b/desktop/modules/appearance/README.md index 2c9a6ad..f6f0356 100644 --- a/desktop/modules/appearance/README.md +++ b/desktop/modules/appearance/README.md @@ -84,7 +84,10 @@ where there is not, and the hovered thumbnail on the targeted screen while the pointer is over it. The thumbnails are grouped by folder, each a header that opens and closes -it, closed to begin with and remembered while the shell runs. The local +it, closed to begin with and remembered while the shell runs; opening one +folds whichever was open, so only one is open at a time. A colour filter +that leaves the open folder empty folds it and opens the first folder with +matches instead. The local groups are the top-level folders of `~/Pictures/wallpapers`, with the images loose in it as a group of its own; after them come the distribution's, `/usr/share/wallpapers`, as `system` for its loose images and `Plasma` for @@ -105,6 +108,20 @@ how far it enlarges: not at all is green, up to 1.33x yellow, past that red. Under the mock screens a note in the same colour says why, for the hovered image or else the staged one. The staged image keeps the accent border. +The row of colour circles under the mock screens filters the list: a click +shows only the wallpapers that carry that colour, folders left empty drop +out, and a second click shows them all again. Each image is quantized to six +colours by `magick` on a 64 px copy, run in parallel, and each colour lands +in a hue bucket, or dark or light when it has little saturation +(`WallColors.js`, tested by `node test_wallcolors.js`). Each image is filed +under one bucket, its dominant one: the largest colour bucket when it covers a +quarter of the image, else the larger of dark and light. A first version +matched any bucket over a fifth, and a blue sky with a red sunset showed +under both. The raw colours are cached by path in +`~/.cache/keystone/wallcolors.tsv`, so only new files are analysed: the first +run over 843 images filled it in about 20 seconds. Saturation is HSV's, not +HSL's: HSL reads a pale lavender like `#cad3f5` as strongly coloured. + "Open wallhaven-dl" under the mock screens launches the wallhaven-dl app, which searches wallhaven.cc and finds larger copies of low-res wallpapers, and closes the settings window, as a successful Apply does, so the desk 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; +} diff --git a/desktop/modules/appearance/WallpaperTab.qml b/desktop/modules/appearance/WallpaperTab.qml index 136b24d..cf41a4d 100644 --- a/desktop/modules/appearance/WallpaperTab.qml +++ b/desktop/modules/appearance/WallpaperTab.qml @@ -205,9 +205,59 @@ Column { } } + // The colour filter: a click shows only the wallpapers that carry the + // colour (Wallpapers.colors), a second click shows them all again. + Row { + id: colorRow + spacing: 8 + Text { + anchors.verticalCenter: parent.verticalCenter + text: "Colour" + font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 2 } + color: Theme.subtext + } + Repeater { + // Fixed colours, not the theme's: they name WallColors.js's hue + // buckets, which do not move with the scheme, and a scheme can + // reuse one colour under several names (one had peach = yellow, + // blue = lavender, mauve = pink, three pairs of twin circles). + model: [ + { key: "red", color: "#ed8796" }, { key: "peach", color: "#f5a97f" }, + { key: "yellow", color: "#eed49f" }, { key: "green", color: "#a6da95" }, + { key: "teal", color: "#8bd5ca" }, { key: "blue", color: "#8aadf4" }, + { key: "mauve", color: "#c6a0f6" }, { key: "dark", color: "#181926" }, + { key: "light", color: "#cad3f5" }, + ] + delegate: Rectangle { + required property var modelData + readonly property bool on: Wallpapers.colorFilter === modelData.key + anchors.verticalCenter: parent.verticalCenter + width: 22 + height: 22 + radius: width / 2 + color: modelData.color + border.width: on ? 3 : swatchHover.hovered ? 2 : 1 + border.color: on ? Theme.accent : Qt.alpha(Theme.text, swatchHover.hovered ? 0.6 : 0.2) + HoverHandler { id: swatchHover } + MouseArea { + anchors.fill: parent + cursorShape: Qt.PointingHandCursor + onClicked: Wallpapers.colorFilter = parent.on ? "" : modelData.key + } + } + } + Text { + anchors.verticalCenter: parent.verticalCenter + visible: Wallpapers.analysing + text: "analysing colours…" + font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 } + color: Theme.overlay + } + } + Column { width: parent.width - height: parent.height - mockPane.height - parent.spacing + height: parent.height - mockPane.height - colorRow.height - parent.spacing * 2 spacing: 12 Row { @@ -232,6 +282,8 @@ Column { anchors.verticalCenter: parent.verticalCenter text: Wallpapers.scanning ? "scanning…" : Wallpapers.applying ? "applying…" + : Wallpapers.colorFilter !== "" + ? Wallpapers.groups.reduce((n, g) => n + g.files.length, 0) + " of " + Wallpapers.files.length + " wallpapers" : Wallpapers.files.length + " wallpapers" font { family: Theme.fontFamily; pixelSize: Theme.fontSize - 4 } color: Theme.overlay @@ -268,18 +320,50 @@ Column { return out; } function rebuild() { + // An open folder the colour filter hid is folded for real, + // not just hidden, and unless another is open the first + // folder left opens in its place. Left open, it came back + // beside the next one opened once the filter was cleared. + // Not while the list is empty, before the first scan. + const keys = Wallpapers.groups.map(g => g.key); + const hidden = Object.keys(Wallpapers.expanded) + .filter(k => Wallpapers.expanded[k] && keys.indexOf(k) < 0); + if (keys.length > 0 && hidden.length > 0) { + hidden.forEach(k => Wallpapers.toggle(k)); + if (!keys.some(k => Wallpapers.expanded[k])) Wallpapers.toggle(keys[0]); + } rows.clear(); for (const g of Wallpapers.groups) { rows.append({ key: g.key, start: -1 }); if (Wallpapers.expanded[g.key]) rows.append(rowsOf(g)); } } + // One folder open at a time: opening one first folds any other, + // from the bottom up so the indexes above stay put, then scrolls + // to the new one's header, which a fold above it would move. function toggle(index) { const g = group(rows.get(index).key); const body = rowsOf(g); - if (Wallpapers.expanded[g.key]) rows.remove(index + 1, body.length); - else body.forEach((r, i) => rows.insert(index + 1 + i, r)); + if (Wallpapers.expanded[g.key]) { + rows.remove(index + 1, body.length); + Wallpapers.toggle(g.key); + return; + } + let folded = false; + for (let i = rows.count - 1; i >= 0; i--) { + const r = rows.get(i); + if (r.start >= 0 || r.key === g.key || !Wallpapers.expanded[r.key]) continue; + rows.remove(i + 1, rowsOf(group(r.key)).length); + Wallpapers.toggle(r.key); + if (i < index) folded = true; + } + if (folded) { + index = 0; + while (rows.get(index).key !== g.key) index++; + } + body.forEach((r, i) => rows.insert(index + 1 + i, r)); Wallpapers.toggle(g.key); + if (folded) positionViewAtIndex(index, ListView.Beginning); } // The width settles after creation, and a new column count diff --git a/desktop/modules/appearance/Wallpapers.qml b/desktop/modules/appearance/Wallpapers.qml index 2b8faf7..f24aca4 100644 --- a/desktop/modules/appearance/Wallpapers.qml +++ b/desktop/modules/appearance/Wallpapers.qml @@ -15,6 +15,7 @@ import Quickshell import Quickshell.Io import QtQuick import "../.." +import "WallColors.js" as WallColors // The wallpaper side. Setting one is `wallp`'s job: it knows about the two // outputs, the saved state and the accent run, and none of that is worth @@ -93,6 +94,70 @@ Singleton { sizeProc.running = false; sizeProc.command = ["sh", "-c", 'printf "%s\\0" "$@" | xargs -0 -r file -N -0', "sh"].concat(root.files); sizeProc.running = true; + root.analyse(); + } + } + } + + // Each image's six main colours, for the page's colour filter + // (WallColors.js), as fractions by bucket. magick quantizes a 64 px copy, + // decoded small with jpeg:size, in parallel; each child prints its line + // in one write, so lines do not interleave on the pipe. The raw lines are + // cached by path in colorCache: only new files are analysed. An upscale + // rewrites a file in place but keeps its colours, so the path is the key. + property var colors: ({}) + property var colorLines: ({}) + property string colorFilter: "" + readonly property bool analysing: colorProc.running + readonly property string colorCache: + (Quickshell.env("XDG_CACHE_HOME") || home + "/.cache") + "/keystone/wallcolors.tsv" + + function analyse() { + if (!colorViewReady) return; // the cache read calls again + const missing = files.filter(f => !(f in colorLines)); + if (missing.length === 0 || colorProc.running) return; + colorProc.command = ["sh", "-c", + `printf '%s\\0' "$@" | xargs -0 -r -P"$(nproc)" -n1 sh -c '` + + `c=$(magick -define jpeg:size=256x256 "$1[0]" -alpha off -resize 64x64 -colors 6 ` + + `-format %c histogram:info: 2>/dev/null | sort -rn | ` + + `awk "{sub(\\":\\",\\"\\",\\$1); printf \\"%s%s:%s\\", s, substr(\\$3,1,7), \\$1; s=\\",\\"}"); ` + + `printf "%s\\t%s\\n" "$1" "$c"' sh`, "sh"].concat(missing); + colorProc.running = true; + } + + // Raw cache lines to colorLines and colors, reassigned whole so bindings + // see the change. + function takeColors(text) { + const lines = Object.assign({}, colorLines), cols = Object.assign({}, colors); + for (const line of text.split("\n")) { + const t = line.indexOf("\t"); + if (t < 0) continue; + const p = line.slice(0, t), c = line.slice(t + 1); + lines[p] = c; + cols[p] = WallColors.parse(c); + } + colorLines = lines; + colors = cols; + } + + FileView { + id: colorView + path: root.colorCache + printErrors: false // absent until the first analysis + onLoaded: { root.takeColors(text()); root.colorViewReady = true; root.analyse(); } + onLoadFailed: { root.colorViewReady = true; root.analyse(); } + } + property bool colorViewReady: false + + Process { + id: colorProc + stdout: StdioCollector { + onStreamFinished: { + root.takeColors(text); + let out = ""; + for (const p in root.colorLines) out += `${p}\t${root.colorLines[p]}\n`; + // Never empty: analyse ran only with files to add. + colorView.setText(out); } } } @@ -249,7 +314,11 @@ Singleton { // `expanded` is indexed by, so a local folder named "system" stays apart. readonly property var groups: { const local = {}, system = [], plasma = {}; - for (const f of files) { + // With a colour picked, only the images it dominates; one not yet + // analysed is left out until it is. + const shownFiles = colorFilter === "" ? files + : files.filter(f => WallColors.matches(colors[f], colorFilter)); + for (const f of shownFiles) { if (f.startsWith(dir + "/")) { const rel = f.slice(dir.length + 1); const key = rel.includes("/") ? rel.slice(0, rel.indexOf("/")) : ""; diff --git a/desktop/modules/appearance/test_wallcolors.js b/desktop/modules/appearance/test_wallcolors.js new file mode 100644 index 0000000..aa05b61 --- /dev/null +++ b/desktop/modules/appearance/test_wallcolors.js @@ -0,0 +1,47 @@ +// 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. + +// Run: node desktop/modules/appearance/test_wallcolors.js +// WallColors.js is a QML library, so its .pragma line is stripped before node +// evaluates it. + +const fs = require("fs"); +const assert = require("assert"); + +const src = fs.readFileSync(__dirname + "/WallColors.js", "utf8").replace(/^\.pragma library$/m, ""); +const C = new Function(src + "\nreturn { bucket, parse, matches, dominant };")(); + +assert.strictEqual(C.bucket("#ed8796"), "red"); +assert.strictEqual(C.bucket("#f5a97f"), "peach"); +assert.strictEqual(C.bucket("#eed49f"), "yellow"); +assert.strictEqual(C.bucket("#a6da95"), "green"); +assert.strictEqual(C.bucket("#8bd5ca"), "teal"); +assert.strictEqual(C.bucket("#8aadf4"), "blue"); +assert.strictEqual(C.bucket("#2a3a6a"), "blue"); // navy, dark but coloured +assert.strictEqual(C.bucket("#c6a0f6"), "mauve"); +assert.strictEqual(C.bucket("#181926"), "dark"); +assert.strictEqual(C.bucket("#cad3f5"), "light"); +assert.strictEqual(C.bucket("#808080"), ""); // mid grey: no bucket + +const f = C.parse("#8aadf4:30,#181926:60,#808080:10"); +assert.ok(Math.abs(f.blue - 0.3) < 1e-9 && Math.abs(f.dark - 0.6) < 1e-9); +// One bucket per image: blue covers over a quarter, so it wins over the +// larger dark. +assert.ok(C.matches(f, "blue") && !C.matches(f, "dark")); +assert.ok(!C.matches(f, "red") && !C.matches(undefined, "blue")); +// Two colours: the larger wins, the smaller does not match. +const sunset = C.parse("#8aadf4:60,#ed8796:30,#181926:10"); +assert.ok(C.matches(sunset, "blue") && !C.matches(sunset, "red")); +// No colour covers a quarter: dark or light decides. +assert.strictEqual(C.dominant(C.parse("#ed8796:10,#181926:60,#cad3f5:30")), "dark"); +assert.strictEqual(C.dominant(C.parse("#808080:1")), ""); + +console.log("ok"); |
