Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 26 additions & 7 deletions src/animation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ export interface FinaleFrameState {
}

const BACKGROUND = "#0d1117";
const CHARACTER = "#ffdf00";
const EYE = "#242424";

const SCORE_COLOR_STOPS = [
Expand All @@ -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));
Expand All @@ -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],
Expand All @@ -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];

Expand Down Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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)) {
Expand All @@ -547,7 +564,7 @@ export function renderCommitManGif(
cellY,
popup.score,
popup.age,
getScoreProgressColor(popup.score, scoreEvents.length),
getProgressColor(popup.score, scoreEvents.length),
);
}

Expand Down Expand Up @@ -598,6 +615,7 @@ export function renderCommitManGif(
radius,
finalHeading,
MOUTH_SEQUENCE[frameIndex % MOUTH_SEQUENCE.length]!,
getProgressColor(scoreEvents.length, scoreEvents.length),
);
}

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand Down
45 changes: 35 additions & 10 deletions tests/animation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
getCommitPauseFramesForFrame,
getContributionLevelForFrame,
getFinaleFrameState,
getProgressColor,
getScoreAtFrame,
getScorePopupsForFrame,
getScoreProgressColor,
renderCommitManGif,
Expand Down Expand Up @@ -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", () => {
Expand Down
Loading