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
6 changes: 4 additions & 2 deletions src/ps/games/snakesladders/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Hex } from '@/utils/color';
import { StringToHex } from '@/utils/color';

export const TOKEN_COLORS = ['#ff0000', '#ff8000', '#ffff00', '#00ff00', '#00ffff', '#0000ff', '#9e00ff', '#ff00ff'] as Hex[];
export const TOKEN_COLORS = ['#ff0000', '#ff8000', '#ffff00', '#00ff00', '#00ffff', '#0000ff', '#9e00ff', '#ff00ff'].map(
color => StringToHex(color)!
);
4 changes: 2 additions & 2 deletions src/utils/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function normalizeHue(hue: number): number {
export function StringToHex(hex: string): Hex | null {
const hexVal = hex.replace(/^#/, '');
if (![3, 4, 6, 8].includes(hexVal.length)) return null;
if (/^[0-9a-f]+$/i.test(hexVal)) return hex as Hex;
if (/^[0-9a-f]+$/i.test(hexVal)) return hexVal as Hex;
return null;
}

Expand All @@ -125,7 +125,7 @@ export function HexToRgb(hex: Hex): Rgb {
}

export function RgbToHex({ R, G, B, a }: Rgb): Hex {
return `#${[R, G, B, ...(a! < 1 ? [Math.round(a! * 255)] : [])]
return `${[R, G, B, ...(a! < 1 ? [Math.round(a! * 255)] : [])]
.map(n => (Number.isNaN(n) ? 10 : Math.round(Math.min(Math.max(n, 0), 255))).toString(16).padStart(2, '0'))
.join('')}` as Hex;
}
Expand Down