diff --git a/src/animation.ts b/src/animation.ts index 80cd18c..78fe100 100644 --- a/src/animation.ts +++ b/src/animation.ts @@ -33,7 +33,6 @@ export interface FinaleFrameState { } const BACKGROUND = "#0d1117"; -const CHARACTER = "#ffdf00"; const EYE = "#242424"; const SCORE_COLOR_STOPS = [ @@ -52,7 +51,7 @@ const SCORE_COLOR_STOPS = [ * Map scoring progress to the requested color journey: * yellow -> orange -> light/dark blue -> light/dark green. */ -export function getScoreProgressColor(score: number, totalScores: number): string { +export function getProgressColor(score: number, totalScores: number): string { const progress = totalScores <= 0 ? 0 : Math.max(0, Math.min(1, score / totalScores)); @@ -72,6 +71,11 @@ export function getScoreProgressColor(score: number, totalScores: number): strin return `#${channels.map((channel) => channel.toString(16).padStart(2, "0")).join("").toUpperCase()}`; } +/** Backward-compatible name for code specifically referring to score popups. */ +export function getScoreProgressColor(score: number, totalScores: number): string { + return getProgressColor(score, totalScores); +} + const BASE_PALETTE: number[][] = [ [13, 17, 23], [22, 27, 34], @@ -89,7 +93,7 @@ const BASE_PALETTE: number[][] = [ // changing popup colors instead of quantizing every score back to yellow. const SCORE_PROGRESS_PALETTE = Array.from( { length: 101 }, - (_, index) => hexToRgb(getScoreProgressColor(index, 100)), + (_, index) => hexToRgb(getProgressColor(index, 100)), ); const PALETTE: number[][] = [...BASE_PALETTE, ...SCORE_PROGRESS_PALETTE]; @@ -361,6 +365,18 @@ export function buildScoreEvents( return events; } +/** + * Return the cumulative score at a frame. Only green-cell score events advance it. + */ +export function getScoreAtFrame(scoreEvents: readonly ScoreEvent[], frameIndex: number): number { + let score = 0; + for (const event of scoreEvents) { + if (event.eatenAtFrame > frameIndex) break; + score = event.score; + } + return score; +} + /** * Return score popups that are visible in a frame, with their age for animation. */ @@ -534,6 +550,7 @@ export function renderCommitManGif( radius, heading, MOUTH_SEQUENCE[frameIndex % MOUTH_SEQUENCE.length]!, + getProgressColor(getScoreAtFrame(scoreEvents, frameIndex), scoreEvents.length), ); for (const popup of getScorePopupsForFrame(scoreEvents, frameIndex)) { @@ -547,7 +564,7 @@ export function renderCommitManGif( cellY, popup.score, popup.age, - getScoreProgressColor(popup.score, scoreEvents.length), + getProgressColor(popup.score, scoreEvents.length), ); } @@ -598,6 +615,7 @@ export function renderCommitManGif( radius, finalHeading, MOUTH_SEQUENCE[frameIndex % MOUTH_SEQUENCE.length]!, + getProgressColor(scoreEvents.length, scoreEvents.length), ); } @@ -794,7 +812,7 @@ function drawBubbleRing( } /** - * Draw a short-lived yellow +N label that rises above the eaten contribution. + * Draw a short-lived progress-colored +N label above the eaten contribution. * A compact 3x5 bitmap font keeps rendering deterministic without extra assets. */ function drawScorePopup( @@ -890,8 +908,9 @@ function drawCommitMan( radius: number, heading: number, mouthAngle: number, + characterColor: string, ): void { - const yellow = hexToRgb(CHARACTER); + const character = hexToRgb(characterColor); const eye = hexToRgb(EYE); const left = Math.floor(centerX - radius); const right = Math.ceil(centerX + radius); @@ -913,7 +932,7 @@ function drawCommitMan( if (Math.abs(angleDifference) < mouthAngle && dx * dx + dy * dy > squaredRadius * 0.12) { continue; } - setPixel(pixels, width, height, x, y, yellow); + setPixel(pixels, width, height, x, y, character); } } diff --git a/tests/animation.test.ts b/tests/animation.test.ts index 9af61ac..7bab781 100644 --- a/tests/animation.test.ts +++ b/tests/animation.test.ts @@ -8,6 +8,8 @@ import { getCommitPauseFramesForFrame, getContributionLevelForFrame, getFinaleFrameState, + getProgressColor, + getScoreAtFrame, getScorePopupsForFrame, getScoreProgressColor, renderCommitManGif, @@ -154,16 +156,39 @@ test("adds a deterministic one- or two-frame pause before eating green cells onl assert.equal(getCommitPauseFramesForFrame([], 0), 0); }); -test("progresses score colors from yellow through orange and blues to deep green", () => { - assert.equal(getScoreProgressColor(0, 100), "#FFDF00"); - assert.equal(getScoreProgressColor(15, 100), "#FF9800"); - assert.equal(getScoreProgressColor(28, 100), "#F97316"); - assert.equal(getScoreProgressColor(38, 100), "#7DD3FC"); - assert.equal(getScoreProgressColor(52, 100), "#3B82F6"); - assert.equal(getScoreProgressColor(65, 100), "#1D4ED8"); - assert.equal(getScoreProgressColor(76, 100), "#86EFAC"); - assert.equal(getScoreProgressColor(88, 100), "#22C55E"); - assert.equal(getScoreProgressColor(100, 100), "#006D32"); +test("progresses the shared character-and-popup color from yellow through orange and blues to deep green", () => { + assert.equal(getProgressColor(0, 100), "#FFDF00"); + assert.equal(getProgressColor(15, 100), "#FF9800"); + assert.equal(getProgressColor(28, 100), "#F97316"); + assert.equal(getProgressColor(38, 100), "#7DD3FC"); + assert.equal(getProgressColor(52, 100), "#3B82F6"); + assert.equal(getProgressColor(65, 100), "#1D4ED8"); + assert.equal(getProgressColor(76, 100), "#86EFAC"); + assert.equal(getProgressColor(88, 100), "#22C55E"); + assert.equal(getProgressColor(100, 100), "#006D32"); + assert.equal(getScoreProgressColor(52, 100), getProgressColor(52, 100)); +}); + +test("keeps Commit-Man and the popup exactly synchronized at every eaten-cell event", () => { + const events = [ + { position: { column: 1, row: 0 }, score: 1, eatenAtFrame: 2 }, + { position: { column: 2, row: 0 }, score: 2, eatenAtFrame: 5 }, + { position: { column: 3, row: 0 }, score: 3, eatenAtFrame: 9 }, + ]; + + assert.equal(getScoreAtFrame(events, 0), 0); + assert.equal(getScoreAtFrame(events, 1), 0); + assert.equal(getScoreAtFrame(events, 2), 1); + assert.equal(getScoreAtFrame(events, 4), 1, "empty-cell movement must not advance color"); + assert.equal(getScoreAtFrame(events, 5), 2); + assert.equal(getScoreAtFrame(events, 8), 2); + assert.equal(getScoreAtFrame(events, 9), 3); + + for (const event of events) { + const characterColor = getProgressColor(getScoreAtFrame(events, event.eatenAtFrame), events.length); + const popupColor = getProgressColor(event.score, events.length); + assert.equal(characterColor, popupColor, "color mismatch at score " + event.score); + } }); test("clamps score-color progress and defaults empty totals to bright yellow", () => {