Skip to content

Commit 0d3c6f5

Browse files
authored
fix: Fix hex parsing/stringifying in color utilities (#95)
* converted TOKEN_COLORS to Hex[] using StringToHex() function Fixed bug of StringToHex() and RgbToHex() including '#' in the output * fix: update code to satisfy ESLint rules and improve variable handling * chore: Prettify
1 parent b82551b commit 0d3c6f5

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
import type { Hex } from '@/utils/color';
1+
import { StringToHex } from '@/utils/color';
22

3-
export const TOKEN_COLORS = ['#ff0000', '#ff8000', '#ffff00', '#00ff00', '#00ffff', '#0000ff', '#9e00ff', '#ff00ff'] as Hex[];
3+
export const TOKEN_COLORS = ['#ff0000', '#ff8000', '#ffff00', '#00ff00', '#00ffff', '#0000ff', '#9e00ff', '#ff00ff'].map(
4+
color => StringToHex(color)!
5+
);

src/utils/color.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export function normalizeHue(hue: number): number {
106106
export function StringToHex(hex: string): Hex | null {
107107
const hexVal = hex.replace(/^#/, '');
108108
if (![3, 4, 6, 8].includes(hexVal.length)) return null;
109-
if (/^[0-9a-f]+$/i.test(hexVal)) return hex as Hex;
109+
if (/^[0-9a-f]+$/i.test(hexVal)) return hexVal as Hex;
110110
return null;
111111
}
112112

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

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

0 commit comments

Comments
 (0)