day-card 0.1.2: a day just begun reads <0.1×, an app's drawn icon prints as a drawing
Found by trying Copy image and Save PNG in the real app with real usage: - VS AVERAGE DAY read "0.0×" early in a day ($0.07 against a ~$180 average). Any ratio above 0 and under 0.1 now reads "<0.1×". - Alma's icon (a drawing on its own light ground) printed as a black square, because every colour icon was filled with the ink. A picture that is mostly light with something dark drawn on it now prints in the ink with its near-white parts left the paper's colour. Mono logos and colour marks with no ground of their own (Claude, DeepSeek) are still filled solid. Run: - mote-run bun test packages/day-card: 12 pass, 0 fail (two new versus cases: 0.07/180 → "<0.1×", 0.5/5 → "0.1×"). - Real app: magpie 0.1.3443 (/Applications) in a sandbox HOME on 127.0.0.1:3486, the owner's usage.jsonl copied in, this app.js and view.js in the installed package. The page shows "<0.1×" and Alma's drawing; Claude and DeepSeek marks unchanged. Copy image showed "Copied" and put a 1708x2278 PNG on the clipboard; Save PNG said "Saved to ~/Downloads/magpie 2026-11-10-2.png" (the first save's file kept, not overwritten), byte-identical to the clipboard PNG. Not tried: Windows and Linux webviews.
This commit is contained in:
@@ -156,6 +156,9 @@ test("streak, and the day against the average day before it", () => {
|
||||
expect(V.versus({ cost: 500 }, { cost: 2, tokens: 1 })).toBe("250×")
|
||||
// nothing priced: by tokens
|
||||
expect(V.versus({ cost: 0, input: 300 }, { cost: 0, tokens: 100 })).toBe("3.0×")
|
||||
// early in a day: a sliver of the average, not none
|
||||
expect(V.versus({ cost: 0.07 }, { cost: 180, tokens: 1 })).toBe("<0.1×")
|
||||
expect(V.versus({ cost: 0.5 }, { cost: 5, tokens: 1 })).toBe("0.1×")
|
||||
expect(V.versus({ cost: 1 }, null)).toBe("–")
|
||||
})
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@magpie-community/page-day-card",
|
||||
"version": "0.1.1",
|
||||
"version": "0.1.2",
|
||||
"description": "A page in magpie's window that draws a day 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"],
|
||||
"keywords": ["magpie", "magpie-page", "usage", "tokens", "cost", "card"],
|
||||
|
||||
@@ -60,9 +60,10 @@
|
||||
}
|
||||
return state.icons.get(ic.src);
|
||||
}
|
||||
// A mark (a mono logo, or a colour one on a clear ground) is filled with
|
||||
// the ink; a picture with a ground of its own prints in the ink, only its
|
||||
// near-white parts left the paper's.
|
||||
// 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.
|
||||
function drawIcon(ctx, ic, x, y, size, scale) {
|
||||
if (!ic) {
|
||||
ctx.fillStyle = INK;
|
||||
@@ -81,15 +82,22 @@
|
||||
try {
|
||||
const d = o.getImageData(0, 0, px, px);
|
||||
const p = d.data;
|
||||
let empty = 0;
|
||||
for (let i = 3; i < p.length; i += 4) if (p[i] < 16) empty++;
|
||||
clear = empty / (px * px) > 0.2;
|
||||
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;
|
||||
for (let i = 0; i < p.length; i += 4) {
|
||||
if (p[i + 3] < 128) continue;
|
||||
solid++;
|
||||
const v = lum(i);
|
||||
if (v > 0.8) light++;
|
||||
else if (v < 0.5) dark++;
|
||||
}
|
||||
// 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;
|
||||
if (!clear) {
|
||||
const ink = [0x1f, 0x1d, 0x20], paper = [0xf7, 0xf4, 0xed];
|
||||
for (let i = 0; i < p.length; i += 4) {
|
||||
// only what is near white stays light: a logo prints solid
|
||||
const v = (0.2126 * p[i] + 0.7152 * p[i + 1] + 0.0722 * p[i + 2]) / 255;
|
||||
const t = Math.min(1, Math.max(0, (v - 0.65) / 0.2));
|
||||
const t = Math.min(1, Math.max(0, (lum(i) - 0.65) / 0.2));
|
||||
const l = t * t * (3 - 2 * t);
|
||||
for (let k = 0; k < 3; k++) p[i + k] = ink[k] + (paper[k] - ink[k]) * l;
|
||||
}
|
||||
|
||||
@@ -193,6 +193,8 @@
|
||||
if (avg.cost > 0 && Number(day.cost) > 0) r = Number(day.cost) / avg.cost;
|
||||
else if (avg.tokens > 0) r = tokensOf(day) / avg.tokens;
|
||||
else return "–";
|
||||
// a day just begun is a sliver of the average, not none of it
|
||||
if (r > 0 && r < 0.1) return "<0.1×";
|
||||
return (r >= 100 ? Math.round(r).toLocaleString("en-US") : r.toFixed(1)) + "×";
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user