Files
canopy-game/src/utils.ts
2026-04-08 16:44:16 -04:00

42 lines
1.1 KiB
TypeScript

import type { NodeKey, Position } from "./types";
export function keyFor(row: number, column: number): NodeKey {
return `${row}:${column}`;
}
export function parseKey(key: string): Position {
const [row, column] = key.split(":").map(Number);
return { row, column };
}
export function hexToRgb(hex: string) {
const value = hex.replace("#", "");
const normalized = value.length === 3 ? value.split("").map((part) => part + part).join("") : value;
const int = Number.parseInt(normalized, 16);
return {
r: (int >> 16) & 255,
g: (int >> 8) & 255,
b: int & 255,
};
}
export function tint(hex: string, alpha = 0.16) {
const { r, g, b } = hexToRgb(hex);
return `rgba(${r}, ${g}, ${b}, ${alpha})`;
}
export function wait(milliseconds: number) {
return new Promise<void>((resolve) => window.setTimeout(resolve, milliseconds));
}
export function shuffleArray<T>(items: T[]) {
const next = [...items];
for (let index = next.length - 1; index > 0; index -= 1) {
const swapIndex = Math.floor(Math.random() * (index + 1));
[next[index], next[swapIndex]] = [next[swapIndex], next[index]];
}
return next;
}