aboutsummaryrefslogtreecommitdiffstats
path: root/desktop/modules/appearance/WallColors.js
blob: 749f5cbd5d5ada13787b1b35f6d78b6e2cadb139 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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;
}