diff --git a/packages/day-card/day-card.test.mjs b/packages/day-card/day-card.test.mjs index 375c71f..c9f66f8 100644 --- a/packages/day-card/day-card.test.mjs +++ b/packages/day-card/day-card.test.mjs @@ -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("–") }) diff --git a/packages/day-card/package.json b/packages/day-card/package.json index 3a12cf3..b3f091e 100644 --- a/packages/day-card/package.json +++ b/packages/day-card/package.json @@ -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"], diff --git a/packages/day-card/page/app.js b/packages/day-card/page/app.js index bee2fe8..e5a4697 100644 --- a/packages/day-card/page/app.js +++ b/packages/day-card/page/app.js @@ -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; } diff --git a/packages/day-card/page/view.js b/packages/day-card/page/view.js index c7b9d6d..aac4ba3 100644 --- a/packages/day-card/page/view.js +++ b/packages/day-card/page/view.js @@ -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)) + "×"; }