repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

brightness: pure node-tested brightness.js (clamp/percent/role)

ca0884245ffbc760de0708aa000664ae89fa2b6a

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>

Changed files (2)

quickshell/nuguland/brightness.js

Added
new file mode 100644
1 +// brightness.js — pure brightness logic: clamp, percent, role thresholds.
2 +// NO I/O. Dual-mode: node `require` (tests) + QML import. Same pattern as clip.js/audio.js.
4 +// --- clamp a level to 0..1, null/NaN-safe ---
5 +function clampLevel(value) {
6 + var n = Number(value);
7 + if (!isFinite(n)) return 0;
8 + return n < 0 ? 0 : (n > 1 ? 1 : n);
9 +}
11 +// --- integer percent 0..100 for a level ---
12 +function levelPercent(value) {
13 + return Math.round(clampLevel(value) * 100);
16 +// --- compact/bar role color token by level band:
17 +// dim (<=15%) -> ui.minor, bright (>=85%) -> warning, else ui.accent ---
18 +function levelRole(value) {
19 + var v = clampLevel(value);
20 + if (v <= 0.15) return "ui.minor";
21 + if (v >= 0.85) return "warning";
22 + return "ui.accent";
25 +if (typeof module !== 'undefined' && module.exports) {
26 + module.exports = { clampLevel: clampLevel, levelPercent: levelPercent,
27 + levelRole: levelRole };

quickshell/nuguland/brightness.test.js

Added
new file mode 100644
1 +// brightness.test.js — node test for the pure brightness module. Prints ALL PASS.
2 +var assert = require('assert');
3 +var B = require('./brightness.js');
5 +// clampLevel: range + null/NaN safety
6 +assert.strictEqual(B.clampLevel(-1), 0);
7 +assert.strictEqual(B.clampLevel(2), 1);
8 +assert.strictEqual(B.clampLevel(0.5), 0.5);
9 +assert.strictEqual(B.clampLevel(null), 0);
10 +assert.strictEqual(B.clampLevel(undefined), 0);
11 +assert.strictEqual(B.clampLevel("0.3"), 0.3);
13 +// levelPercent: rounded integer percent
14 +assert.strictEqual(B.levelPercent(0), 0);
15 +assert.strictEqual(B.levelPercent(0.5), 50);
16 +assert.strictEqual(B.levelPercent(1), 100);
17 +assert.strictEqual(B.levelPercent(2), 100);
18 +assert.strictEqual(B.levelPercent(0.014), 1);
20 +// levelRole: band boundaries (<=0.15 minor, >=0.85 warning, else accent)
21 +assert.strictEqual(B.levelRole(0.10), "ui.minor");
22 +assert.strictEqual(B.levelRole(0.15), "ui.minor"); // inclusive low boundary
23 +assert.strictEqual(B.levelRole(0.16), "ui.accent");
24 +assert.strictEqual(B.levelRole(0.5), "ui.accent");
25 +assert.strictEqual(B.levelRole(0.84), "ui.accent");
26 +assert.strictEqual(B.levelRole(0.85), "warning"); // inclusive high boundary
27 +assert.strictEqual(B.levelRole(1), "warning");
28 +assert.strictEqual(B.levelRole(null), "ui.minor"); // 0 -> dim
30 +console.log('brightness.test.js: ALL PASS');