Changesets · Branches · Tags · Compare
| Changeset | de93f0eb46204e52a004fc5584052169587291f25de85c539b91c8793c5ed2d0 |
|---|---|
| Author | WaylandYang <145302500+WaylandYang@users.noreply.github.com> · 2026-09-02 20:28 +08:00 |
| Committer | GitHub <noreply@github.com> · 2026-09-02 20:28 +08:00 |
| Parents | e8031b33dc5a |
3 files changed
modified .github/workflows/star-history.yml +5 −3
| @@ -49,16 +49,18 @@ | ||
| 49 | 49 | git clone --depth 1 --branch assets \ |
| 50 | 50 | "https://x-access-token:${{ github.token }}@github.com/${{ github.repository }}.git" out |
| 51 | 51 | mkdir -p out/assets |
| 52 | - cp chart/star-history.svg out/assets/star-history.svg | |
| 52 | + # 两张:深色主题用白线那张,浅色用深线那张(README 里 `<picture>` | |
| 53 | + # 按主题选)。白线在 GitHub 浅色底上是看不见的,所以少一张都不行 | |
| 54 | + cp chart/star-history.svg chart/star-history-light.svg out/assets/ | |
| 53 | 55 | cd out |
| 54 | 56 | # 星标没变化的那天不留空提交——每天一条 "no change" 会把 |
| 55 | 57 | # 这个分支的历史淹掉 |
| 56 | - if git diff --quiet -- assets/star-history.svg; then | |
| 58 | + if git diff --quiet -- assets/; then | |
| 57 | 59 | echo "chart unchanged, nothing to commit" |
| 58 | 60 | exit 0 |
| 59 | 61 | fi |
| 60 | 62 | git config user.name "github-actions[bot]" |
| 61 | 63 | git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 62 | - git add assets/star-history.svg | |
| 64 | + git add assets/ | |
| 63 | 65 | git commit -m "Star history: $(date -u +%Y-%m-%d)" |
| 64 | 66 | git push |
modified README.md +4 −1
| @@ -119,7 +119,10 @@ | ||
| 119 | 119 | |
| 120 | 120 | <div align="center"> |
| 121 | 121 | |
| 122 | -<img src="https://raw.githubusercontent.com/deeplethe/utopia/assets/assets/star-history.svg" alt="Star History" width="820"> | |
| 122 | +<picture> | |
| 123 | + <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/deeplethe/utopia/assets/assets/star-history.svg"> | |
| 124 | + <img src="https://raw.githubusercontent.com/deeplethe/utopia/assets/assets/star-history-light.svg" alt="Star History" width="820"> | |
| 125 | +</picture> | |
| 123 | 126 | |
| 124 | 127 | </div> |
| 125 | 128 |
modified scripts/star-history.mjs +78 −19
| @@ -137,15 +137,59 @@ | ||
| 137 | 137 | const fmtDate = (t) => |
| 138 | 138 | new Date(t).toLocaleDateString("en-US", { month: "short", day: "numeric", timeZone: "UTC" }); |
| 139 | 139 | |
| 140 | -const line = series.map((p, i) => `${i === 0 ? "M" : "L"}${x(i).toFixed(1)},${y(p.v).toFixed(1)}`).join(""); | |
| 141 | -const area = `${line}L${x(series.length - 1).toFixed(1)},${(PAD.top + plotH).toFixed(1)}L${x(0).toFixed(1)},${(PAD.top + plotH).toFixed(1)}Z`; | |
| 140 | +/** 平滑成三次贝塞尔,用**单调**插值(Fritsch–Carlson)。 | |
| 141 | + * | |
| 142 | + * 不用普通的 Catmull-Rom:累计星标只增不减,而普通样条会在斜率突变处 | |
| 143 | + * 过冲,画出一段下凹——**那等于在图上说星标掉了**。单调插值把每段的 | |
| 144 | + * 切线夹在不制造极值的范围内,曲线因此永远不会往回走。 | |
| 145 | + * | |
| 146 | + * 平的那几天(当天零新增)切线为零,接上去也不会鼓出来。 */ | |
| 147 | +function smoothPath(pts) { | |
| 148 | + const n = pts.length; | |
| 149 | + if (n < 2) return `M${pts[0].x.toFixed(1)},${pts[0].y.toFixed(1)}`; | |
| 150 | + // 每段的斜率 | |
| 151 | + const dx = [], dy = [], slope = []; | |
| 152 | + for (let i = 0; i < n - 1; i++) { | |
| 153 | + dx.push(pts[i + 1].x - pts[i].x); | |
| 154 | + dy.push(pts[i + 1].y - pts[i].y); | |
| 155 | + slope.push(dy[i] / dx[i]); | |
| 156 | + } | |
| 157 | + // 每个点的切线:相邻两段异号(或有一段是平的)时取零,那正是不过冲的条件 | |
| 158 | + const m = [slope[0]]; | |
| 159 | + for (let i = 1; i < n - 1; i++) { | |
| 160 | + m.push(slope[i - 1] * slope[i] <= 0 ? 0 : (slope[i - 1] + slope[i]) / 2); | |
| 161 | + } | |
| 162 | + m.push(slope[n - 2]); | |
| 163 | + // Fritsch–Carlson:把切线限制在每段斜率的三倍以内 | |
| 164 | + for (let i = 0; i < n - 1; i++) { | |
| 165 | + if (slope[i] === 0) { | |
| 166 | + m[i] = 0; | |
| 167 | + m[i + 1] = 0; | |
| 168 | + continue; | |
| 169 | + } | |
| 170 | + const a = m[i] / slope[i]; | |
| 171 | + const b = m[i + 1] / slope[i]; | |
| 172 | + const s = a * a + b * b; | |
| 173 | + if (s > 9) { | |
| 174 | + const t = (3 / Math.sqrt(s)) * slope[i]; | |
| 175 | + m[i] = t * a; | |
| 176 | + m[i + 1] = t * b; | |
| 177 | + } | |
| 178 | + } | |
| 179 | + let d = `M${pts[0].x.toFixed(1)},${pts[0].y.toFixed(1)}`; | |
| 180 | + for (let i = 0; i < n - 1; i++) { | |
| 181 | + const h = dx[i] / 3; | |
| 182 | + d += | |
| 183 | + `C${(pts[i].x + h).toFixed(1)},${(pts[i].y + m[i] * h).toFixed(1)} ` + | |
| 184 | + `${(pts[i + 1].x - h).toFixed(1)},${(pts[i + 1].y - m[i + 1] * h).toFixed(1)} ` + | |
| 185 | + `${pts[i + 1].x.toFixed(1)},${pts[i + 1].y.toFixed(1)}`; | |
| 186 | + } | |
| 187 | + return d; | |
| 188 | +} | |
| 142 | 189 | |
| 143 | -/* 颜色不跟随主题。GitHub 的 README 里 SVG 是当图片渲染的, | |
| 144 | - `prefers-color-scheme` 不一定生效——挑一组在浅色和深色底上都读得出的中间调, | |
| 145 | - 比赌媒体查询可靠 */ | |
| 146 | -const INK = "#8b949e"; | |
| 147 | -const ACCENT = "#e3b341"; | |
| 148 | -const GRID = "#8b949e33"; | |
| 190 | +const pts = series.map((p, i) => ({ x: x(i), y: y(p.v) })); | |
| 191 | +const line = smoothPath(pts); | |
| 192 | +const area = `${line}L${x(series.length - 1).toFixed(1)},${(PAD.top + plotH).toFixed(1)}L${x(0).toFixed(1)},${(PAD.top + plotH).toFixed(1)}Z`; | |
| 149 | 193 | |
| 150 | 194 | /* 末点与它的数值。**贴着右边缘时把文字改成右对齐**, |
| 151 | 195 | 否则一个四位数会伸出画布外——SVG 不会替你裁,它就是没了 */ |
| @@ -157,21 +201,36 @@ | ||
| 157 | 201 | Array.from({ length: 6 }, (_, i) => Math.round((i * (series.length - 1)) / 5)), |
| 158 | 202 | )]; |
| 159 | 203 | |
| 160 | -const svg = `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"> | |
| 204 | +/* 出两张,深浅各一,README 用 `<picture>` 按主题选。 | |
| 205 | + * | |
| 206 | + * **白线在浅色主题上是看不见的**——GitHub 浅色底就是白的。想要白色就 | |
| 207 | + * 必须分两张:`prefers-color-scheme` 写在 SVG 里不算数,README 里的 SVG | |
| 208 | + * 是当图片加载的,那条媒体查询问的是操作系统,不是 GitHub 的主题设置, | |
| 209 | + * 两者不一致的人就会看到一张空白的图。`<picture>` 问的才是 GitHub 自己。 */ | |
| 210 | +const THEMES = { | |
| 211 | + dark: { ink: "#8b949e", accent: "#ffffff", grid: "#8b949e33" }, | |
| 212 | + light: { ink: "#6e7781", accent: "#1f2328", grid: "#6e778133" }, | |
| 213 | +}; | |
| 214 | + | |
| 215 | +function render({ ink, accent, grid }) { | |
| 216 | + return `<svg xmlns="http://www.w3.org/2000/svg" width="${W}" height="${H}" viewBox="0 0 ${W} ${H}" font-family="-apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif"> | |
| 161 | 217 | <defs><linearGradient id="fill" x1="0" y1="0" x2="0" y2="1"> |
| 162 | -<stop offset="0%" stop-color="${ACCENT}" stop-opacity="0.28"/> | |
| 163 | -<stop offset="100%" stop-color="${ACCENT}" stop-opacity="0"/> | |
| 218 | +<stop offset="0%" stop-color="${accent}" stop-opacity="0.22"/> | |
| 219 | +<stop offset="100%" stop-color="${accent}" stop-opacity="0"/> | |
| 164 | 220 | </linearGradient></defs> |
| 165 | -<text x="${PAD.left}" y="24" fill="${INK}" font-size="13">${owner}/${repo}</text> | |
| 166 | -${ticks(maxV).map((v) => `<g><line x1="${PAD.left}" y1="${y(v).toFixed(1)}" x2="${W - PAD.right}" y2="${y(v).toFixed(1)}" stroke="${GRID}"/><text x="${PAD.left - 10}" y="${(y(v) + 4).toFixed(1)}" fill="${INK}" font-size="11" text-anchor="end">${v.toLocaleString("en-US")}</text></g>`).join("")} | |
| 167 | -${xTickIdx.map((i) => `<text x="${x(i).toFixed(1)}" y="${H - 16}" fill="${INK}" font-size="11" text-anchor="middle">${fmtDate(series[i].t)}</text>`).join("")} | |
| 221 | +<text x="${PAD.left}" y="24" fill="${ink}" font-size="13">${owner}/${repo}</text> | |
| 222 | +${ticks(maxV).map((v) => `<g><line x1="${PAD.left}" y1="${y(v).toFixed(1)}" x2="${W - PAD.right}" y2="${y(v).toFixed(1)}" stroke="${grid}"/><text x="${PAD.left - 10}" y="${(y(v) + 4).toFixed(1)}" fill="${ink}" font-size="11" text-anchor="end">${v.toLocaleString("en-US")}</text></g>`).join("")} | |
| 223 | +${xTickIdx.map((i) => `<text x="${x(i).toFixed(1)}" y="${H - 16}" fill="${ink}" font-size="11" text-anchor="middle">${fmtDate(series[i].t)}</text>`).join("")} | |
| 168 | 224 | <path d="${area}" fill="url(#fill)"/> |
| 169 | -<path d="${line}" fill="none" stroke="${ACCENT}" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/> | |
| 170 | -<circle cx="${endX.toFixed(1)}" cy="${endY.toFixed(1)}" r="3.5" fill="${ACCENT}"/> | |
| 171 | -<text x="${endX.toFixed(1)}" y="${(endY - 12).toFixed(1)}" fill="${ACCENT}" font-size="14" font-weight="600" text-anchor="${endAnchor}">${maxV.toLocaleString("en-US")}</text> | |
| 225 | +<path d="${line}" fill="none" stroke="${accent}" stroke-width="2" stroke-linejoin="round" stroke-linecap="round"/> | |
| 226 | +<circle cx="${endX.toFixed(1)}" cy="${endY.toFixed(1)}" r="3.5" fill="${accent}"/> | |
| 227 | +<text x="${endX.toFixed(1)}" y="${(endY - 12).toFixed(1)}" fill="${accent}" font-size="14" font-weight="600" text-anchor="${endAnchor}">${maxV.toLocaleString("en-US")}</text> | |
| 172 | 228 | </svg> |
| 173 | 229 | `; |
| 230 | +} | |
| 174 | 231 | |
| 175 | 232 | mkdirSync(dirname(out), { recursive: true }); |
| 176 | -writeFileSync(out, svg); | |
| 177 | -console.log(`${series.length} days, ${maxV} stars → ${out}`); | |
| 233 | +const lightOut = out.replace(/\.svg$/, "-light.svg"); | |
| 234 | +writeFileSync(out, render(THEMES.dark)); | |
| 235 | +writeFileSync(lightOut, render(THEMES.light)); | |
| 236 | +console.log(`${series.length} days, ${maxV} stars → ${out} + ${lightOut}`); |