aboutsummaryrefslogtreecommitdiff
path: root/programs/cwal/wallpaper-palette
blob: 102979861c3f95dd243ae068168d378f2eae9f74 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
#!/usr/bin/env bash

# wallpaper-palette - derive cwal color overrides from a wallpaper
# Copyright (C) 2026 deltegui
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

# Generate cwal color overrides from one wallpaper image.

set -euo pipefail

usage()
{
    printf 'Usage: %s <wallpaper-image>\n' "${0##*/}"
}

if (( $# == 1 )) && [[ $1 == '-h' || $1 == '--help' ]]; then
    usage
    exit 0
fi
if (( $# != 1 )); then
    usage >&2
    exit 2
fi
if ! command -v magick >/dev/null 2>&1; then
    printf 'wallpaper-palette: ImageMagick 7 (magick) is required\n' >&2
    exit 1
fi
if [[ ! -f $1 || ! -r $1 ]]; then
    printf 'wallpaper-palette: wallpaper is not a readable regular file: %s\n' \
        "$1" >&2
    exit 1
fi

palette=$(magick "$1" \
    -auto-orient \
    -alpha remove -background '#000000' -alpha off \
    -resize '192x192>' \
    -colorspace sRGB -dither None -colors 32 -depth 8 \
    -format '%c' histogram:info:- |
    awk '
function clamp(value) {
    return value < 0 ? 0 : (value > 255 ? 255 : value)
}

function absolute(value) {
    return value < 0 ? -value : value
}

function hex_digit(character) {
    return index("0123456789abcdef", tolower(character)) - 1
}

function hex_byte(value, position) {
    return hex_digit(substr(value, position, 1)) * 16 + \
           hex_digit(substr(value, position + 1, 1))
}

function luminance(red, green, blue) {
    return (0.2126 * red + 0.7152 * green + 0.0722 * blue) / 255
}

function color_saturation(red, green, blue, maximum, minimum) {
    maximum = red
    if (green > maximum)
        maximum = green
    if (blue > maximum)
        maximum = blue
    minimum = red
    if (green < minimum)
        minimum = green
    if (blue < minimum)
        minimum = blue
    return maximum == 0 ? 0 : (maximum - minimum) / maximum
}

function color_hue(red, green, blue, maximum, minimum, delta, hue) {
    maximum = red
    if (green > maximum)
        maximum = green
    if (blue > maximum)
        maximum = blue
    minimum = red
    if (green < minimum)
        minimum = green
    if (blue < minimum)
        minimum = blue
    delta = maximum - minimum
    if (delta == 0)
        return -1
    if (maximum == red)
        hue = 60 * (((green - blue) / delta) % 6)
    else if (maximum == green)
        hue = 60 * (((blue - red) / delta) + 2)
    else
        hue = 60 * (((red - green) / delta) + 4)
    return hue < 0 ? hue + 360 : hue
}

function hue_distance(first, second, distance) {
    distance = first - second
    if (distance < 0)
        distance = -distance
    return distance > 180 ? 360 - distance : distance
}

function hsv_to_rgb(hue, saturation, value, chroma, scaled, middle, red, green, blue) {
    chroma = value * saturation
    scaled = hue / 60
    middle = chroma * (1 - absolute((scaled % 2) - 1))
    if (scaled < 1) {
        red = chroma
        green = middle
        blue = 0
    } else if (scaled < 2) {
        red = middle
        green = chroma
        blue = 0
    } else if (scaled < 3) {
        red = 0
        green = chroma
        blue = middle
    } else if (scaled < 4) {
        red = 0
        green = middle
        blue = chroma
    } else if (scaled < 5) {
        red = middle
        green = 0
        blue = chroma
    } else {
        red = chroma
        green = 0
        blue = middle
    }
    middle = value - chroma
    hsv_red = (red + middle) * 255
    hsv_green = (green + middle) * 255
    hsv_blue = (blue + middle) * 255
}

function make_tone(red, green, blue, target, desaturate, current, scale, gray) {
    current = luminance(red, green, blue)
    if (current < target) {
        scale = current == 1 ? 0 : (target - current) / (1 - current)
        red += (255 - red) * scale
        green += (255 - green) * scale
        blue += (255 - blue) * scale
    } else if (current > target) {
        scale = target / current
        red *= scale
        green *= scale
        blue *= scale
    }
    gray = luminance(red, green, blue) * 255
    tone_red = clamp(red * (1 - desaturate) + gray * desaturate)
    tone_green = clamp(green * (1 - desaturate) + gray * desaturate)
    tone_blue = clamp(blue * (1 - desaturate) + gray * desaturate)
}

function to_hex(red, green, blue) {
    return sprintf("%02x%02x%02x", clamp(int(red + 0.5)),
                   clamp(int(green + 0.5)), clamp(int(blue + 0.5)))
}

function emit(name, red, green, blue, target, desaturate) {
    make_tone(red, green, blue, target, desaturate)
    printf "%s=%s\n", name, to_hex(tone_red, tone_green, tone_blue)
}

function candidate_for_hue(target, candidate, distance, score, best, best_score) {
    best = 0
    best_score = -1
    for (candidate = 1; candidate <= color_count; candidate++) {
        if (saturation[candidate] < 0.20 || hue[candidate] < 0)
            continue
        distance = hue_distance(hue[candidate], target)
        if (distance > 55)
            continue
        score = (1 - distance / 55) * saturation[candidate] * \
                (0.25 + frequency[candidate] / highest_frequency)
        if (score > best_score) {
            best = candidate
            best_score = score
        }
    }
    return best
}

function emit_role(name, target_hue, target_luminance, candidate) {
    candidate = candidate_for_hue(target_hue)
    if (candidate != 0) {
        emit(name, red[candidate], green[candidate], blue[candidate], target_luminance, 0)
    } else {
        hsv_to_rgb(target_hue, fallback_saturation, 0.82)
        emit(name, hsv_red, hsv_green, hsv_blue, target_luminance, 0)
    }
}

{
    count = $1
    sub(/:$/, "", count)
    value = ""
    for (field = 1; field <= NF; field++) {
        if ($field ~ /^#[0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f][0-9A-Fa-f]$/) {
            value = substr($field, 2)
            break
        }
    }
    if (value == "")
        next

    color_count++
    frequency[color_count] = count + 0
    red[color_count] = hex_byte(value, 1)
    green[color_count] = hex_byte(value, 3)
    blue[color_count] = hex_byte(value, 5)
    luma[color_count] = luminance(red[color_count], green[color_count], blue[color_count])
    saturation[color_count] = color_saturation(red[color_count], green[color_count], blue[color_count])
    hue[color_count] = color_hue(red[color_count], green[color_count], blue[color_count])
}

END {
    if (color_count == 0)
        exit 1

    highest_frequency = 0
    base_index = 1
    text_index = 1
    accent_index = 0
    best_text_score = -1
    best_accent_score = -1
    for (candidate = 1; candidate <= color_count; candidate++) {
        if (frequency[candidate] > highest_frequency) {
            highest_frequency = frequency[candidate]
            base_index = candidate
        }
    }
    for (candidate = 1; candidate <= color_count; candidate++) {
        score = luma[candidate] * \
                (0.25 + frequency[candidate] / highest_frequency)
        if (score > best_text_score) {
            text_index = candidate
            best_text_score = score
        }
        score = saturation[candidate] * \
                (0.25 + frequency[candidate] / highest_frequency)
        if (score > best_accent_score) {
            accent_index = candidate
            best_accent_score = score
        }
    }

    emit("waybar_background", red[base_index], green[base_index], blue[base_index], 0.055, 0.30)
    emit("waybar_text", red[text_index], green[text_index], blue[text_index], 0.90, 0.65)
    emit("crust", red[base_index], green[base_index], blue[base_index], 0.045, 0.32)
    emit("base", red[base_index], green[base_index], blue[base_index], 0.105, 0.24)
    emit("surface0", red[base_index], green[base_index], blue[base_index], 0.160, 0.28)
    emit("surface1", red[base_index], green[base_index], blue[base_index], 0.225, 0.32)
    emit("surface2", red[base_index], green[base_index], blue[base_index], 0.300, 0.36)
    emit("overlay0", red[base_index], green[base_index], blue[base_index], 0.430, 0.45)
    emit("subtext0", red[text_index], green[text_index], blue[text_index], 0.620, 0.62)
    emit("subtext1", red[text_index], green[text_index], blue[text_index], 0.740, 0.64)
    emit("text", red[text_index], green[text_index], blue[text_index], 0.900, 0.68)

    if (accent_index == 0 || saturation[accent_index] < 0.12) {
        hsv_to_rgb(215, 0.60, 0.82)
        accent_red = hsv_red
        accent_green = hsv_green
        accent_blue = hsv_blue
        fallback_saturation = 0.60
    } else {
        accent_red = red[accent_index]
        accent_green = green[accent_index]
        accent_blue = blue[accent_index]
        fallback_saturation = saturation[accent_index]
        if (fallback_saturation < 0.55)
            fallback_saturation = 0.55
    }
    emit("accent", accent_red, accent_green, accent_blue, 0.620, 0)
    emit_role("red", 0, 0.600)
    emit_role("green", 120, 0.620)
    emit_role("yellow", 55, 0.720)
    emit_role("blue", 215, 0.580)
    emit_role("pink", 325, 0.620)
    emit_role("teal", 180, 0.620)
    emit_role("peach", 28, 0.660)
}
') || {
    printf 'wallpaper-palette: could not extract colors from %s\n' "$1" >&2
    exit 1
}

if [[ -z $palette ]]; then
    printf 'wallpaper-palette: no colors were extracted from %s\n' "$1" >&2
    exit 1
fi
printf '%s\n' "$palette"