Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/ps/games/snakesladders/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
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)!);
8 changes: 4 additions & 4 deletions src/utils/color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ export function normalizeHue(hue: number): number {
// region Conversion Methods

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;
hex = hex.replace(/^#/, '');
if (![3, 4, 6, 8].includes(hex.length)) return null;
if (/^[0-9a-f]+$/i.test(hex)) return hex 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