Files
pages/static/js/home-palette-core.js
T
Anya Tokugawa 0964fdc5b7
Build and publish container images / images (push) Successful in 10m56s
Release container images / release (push) Successful in 18s
feat: adapt home appearance to wallpaper colors
2026-11-07 22:51:56 +09:00

59 lines
3.7 KiB
JavaScript

/* Bounded, dependency-free palette math; also exercised by node --test. */
((root) => {
'use strict';
const clamp = (x, a = 0, b = 1) => Math.max(a, Math.min(b, x));
const mix = (a, b, n) => a.map((x, i) => x * (1 - n) + b[i] * n);
const luminance = c => c.map(x => { x /= 255; return x <= .04045 ? x / 12.92 : ((x + .055) / 1.055) ** 2.4; }).reduce((a, x, i) => a + x * [.2126, .7152, .0722][i], 0);
const contrast = (a, b) => (Math.max(luminance(a), luminance(b)) + .05) / (Math.min(luminance(a), luminance(b)) + .05);
const ink = bg => contrast([255,255,255], bg) >= contrast([0,0,0], bg) ? [255,255,255] : [0,0,0];
const readable = (c, bg, ratio) => {
const end = ink(bg);
for (let n = 0; n <= 20; n++) { const v = mix(c, end, n / 20); if (contrast(v, bg) >= ratio) return v; }
return end;
};
const css = c => '#' + c.map(v => Math.round(clamp(v,0,255)).toString(16).padStart(2,'0')).join('');
function extract(pixels) {
if (!pixels || pixels.length > 64 * 64 * 4 || pixels.length % 4) return null;
const bins = new Map(), sum = [0,0,0]; let weight = 0;
for (let i = 0; i < pixels.length; i += 4) {
const a = pixels[i+3] / 255; if (a < .06) continue;
const c = [pixels[i], pixels[i+1], pixels[i+2]], key = c.map(x => x >> 5).join(',');
const bin = bins.get(key) || {sum:[0,0,0], weight:0};
c.forEach((v,j) => { sum[j] += v*a; bin.sum[j] += v*a; });
bin.weight += a; weight += a; bins.set(key, bin);
}
if (!weight) return null;
const average = sum.map(x => x / weight); let accent = average, best = 0;
for (const bin of bins.values()) {
const c = bin.sum.map(x => x / bin.weight), hi = Math.max(...c), lo = Math.min(...c);
const saturation = hi ? (hi-lo)/hi : 0, light = luminance(c);
const score = Math.sqrt(bin.weight) * saturation * (light > .025 && light < .85 ? 1 : .1);
if (score > best) { best = score; accent = c; }
}
return {average, accent, coverage:weight / (pixels.length / 4)};
}
function colors(p, base, opts) {
if (!p || opts.mode === 'off' || !opts.strength || opts.opacity <= 0) return {};
const strength = clamp(opts.strength / 100), opacity = clamp(opts.opacity * p.coverage);
const backdrop = mix(base.bg, p.average, opacity);
const dark = opts.brightness === 'auto' && opts.mode === 'surface' ? luminance(backdrop) < .36 : luminance(base.bg) < .36;
const neutral = dark ? [18,20,26] : [244,246,250];
const hue = mix(p.average, p.accent, .35);
const tinted = mix(neutral, hue, dark ? .23 : .12);
const bg = opts.mode === 'surface' ? mix(opts.brightness === 'auto' ? neutral : base.bg, tinted, strength) : base.bg;
const behind = mix(bg, p.average, opacity), glass = mix(behind, bg, clamp(opts.glass));
const accent = readable(mix(base.accent, p.accent, strength), glass, 3);
const result = {'--accent':css(accent), '--link':css(readable(accent, glass, 4.5)), '--link-hover':css(readable(accent,glass,7))};
if (opts.mode !== 'surface') return result;
const fg = readable(dark ? [238,242,248] : [22,25,32], glass, 7);
Object.assign(result, {'--bg':css(bg), '--panel':css(bg), '--fg':css(fg), '--muted':css(readable(mix(fg,glass,.3),glass,4.5)),
'--border':css(mix(bg,accent,.32)), '--border-soft':css(mix(bg,accent,.18)),
'--input-bg':css(bg), '--input-fg':css(fg), '--input-border':css(mix(bg,accent,.4)),
'--button-bg':css(bg), '--button-fg':css(fg), '--button-border':css(mix(bg,accent,.4)), '--button-hover-bg':css(mix(bg,accent,.12))});
return result;
}
const api = {extract, colors, luminance, contrast, mix};
if (typeof module !== 'undefined' && module.exports) module.exports = api;
else root.HomePalette = api;
})(globalThis);