day-card 0.3.2: an app's picture icon prints in greys, not as a black block

ZacharyZhang on X: Cindy's logo printed as a black rounded square. Its
icon is a full-colour illustration on a rounded tile with clear corners.
drawIcon took anything under 80% solid and without a light ground for a
mark and filled its shape with the ink, so the whole tile went black.
mlx-serve (a jellyfish on a dark tile) and mothx (a glyph on a pink
disc) did the same.

Now an icon that fills its own box (a square, rounded or not, or a disc:
at least 75% of its bounding box, and the box at least 40% of the icon)
is a tile. A tile with no light ground prints in greys from its own
darkest tone (3rd percentile) to its lightest (97th), ink to paper; a
tile of one flat tone still takes the old near-white threshold. Marks and
pictures on a light ground print as before.

Run: a harness drew 18 icons (magpie's PNG agent icons, several colour
SVG logos and the bundled Amp mark) with the old and new drawIcon at 18px
x3 in Chromium and WebKit (Playwright 1.62.0, wsl-run on magpie-desktop),
no page errors. Changed: cindy, mlx-serve, mothx now show their picture;
atomcode is a grey tile instead of a black one. Every other icon is the
same as before. bun test packages/day-card: 18 pass, 0 fail.
Not tried: the real magpie app's webviews; crush's pixel heart still
prints as a ragged silhouette, as before.
This commit is contained in:
yetone
2026-11-12 14:18:17 +08:00
parent 2b7da486ac
commit 3632d53be7
2 changed files with 32 additions and 6 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@magpie-community/page-day-card",
"version": "0.3.1",
"version": "0.3.2",
"description": "A page in magpie's window that draws a day, week, month or all of your AI usage as a card: tokens, cost, each agent and its models, your streak and your average day. Copy it as a picture or save it as a PNG",
"files": ["page", "README.md", "NOTICE"],
"keywords": ["magpie", "magpie-page", "usage", "tokens", "cost", "card"],
+31 -5
View File
@@ -64,7 +64,9 @@
// A mark (a mono logo, or a colour one with no ground of its own) is
// filled with the ink; a picture on a light ground of its own (an app's
// tile, a drawing) prints in the ink, only its near-white parts left the
// paper's, and what is clear around it stays clear.
// paper's, and what is clear around it stays clear. A tile with no light
// ground (an app icon, an illustration like Cindy's) prints in greys from
// its own darkest to its lightest, so it doesn't fill in as a block.
function drawIcon(ctx, ic, x, y, size, scale) {
if (!ic) {
ctx.fillStyle = INK;
@@ -87,21 +89,45 @@
const p = d.data;
const lum = (i) => (0.2126 * p[i] + 0.7152 * p[i + 1] + 0.0722 * p[i + 2]) / 255;
let solid = 0, light = 0, dark = 0;
let x0 = px, y0 = px, x1 = -1, y1 = -1;
const tones = [];
for (let i = 0; i < p.length; i += 4) {
if (p[i + 3] < 128) continue;
solid++;
const v = lum(i);
tones.push(v);
if (v > 0.8) light++;
else if (v < 0.5) dark++;
const cx = (i / 4) % px, cy = Math.floor(i / 4 / px);
if (cx < x0) x0 = cx;
if (cx > x1) x1 = cx;
if (cy < y0) y0 = cy;
if (cy > y1) y1 = cy;
}
// a ground: mostly light, with something dark drawn on it
clear = !(solid && light / solid > 0.35 && dark / solid > 0.05) && solid < px * px * 0.8;
const ground = solid && light / solid > 0.35 && dark / solid > 0.05;
// a tile: it fills its own box (a square, rounded or not, or a disc), and the
// box is most of the icon
const box = (x1 - x0 + 1) * (y1 - y0 + 1);
const tile = solid >= px * px * 0.8 || (x1 >= 0 && solid >= box * 0.75 && box >= px * px * 0.4);
clear = !ground && !tile;
if (!clear) {
const ink = [0x1f, 0x1d, 0x20], paper = [0xf7, 0xf4, 0xed];
let tone = null;
if (!ground) {
tones.sort((a, b) => a - b);
const lo = tones[Math.floor(tones.length * 0.03)];
const hi = tones[Math.min(tones.length - 1, Math.floor(tones.length * 0.97))];
if (hi - lo > 0.12) tone = (v) => Math.min(1, Math.max(0, (v - lo) / (hi - lo)));
}
for (let i = 0; i < p.length; i += 4) {
// only what is near white stays light: a logo prints solid
const t = Math.min(1, Math.max(0, (lum(i) - 0.65) / 0.2));
const l = t * t * (3 - 2 * t);
let l;
if (tone) l = tone(lum(i));
else {
// only what is near white stays light: a logo prints solid
const t = Math.min(1, Math.max(0, (lum(i) - 0.65) / 0.2));
l = t * t * (3 - 2 * t);
}
for (let k = 0; k < 3; k++) p[i + k] = ink[k] + (paper[k] - ink[k]) * l;
}
o.putImageData(d, 0, 0);