Skip to content

Commit 99fbfce

Browse files
authored
Merge pull request #10 from maitamdev/feat/color-utils
feat(utils): add color utilities
2 parents 4f9c214 + 6148fa5 commit 99fbfce

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/utils/colorUtils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Color utility functions
2+
export function hexToRgb(hex: string): { r: number; g: number; b: number } | null {
3+
const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
4+
return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) } : null;
5+
}
6+
7+
export function getContrastColor(hex: string): string {
8+
const rgb = hexToRgb(hex);
9+
if (!rgb) return '#000000';
10+
const brightness = (rgb.r * 299 + rgb.g * 587 + rgb.b * 114) / 1000;
11+
return brightness > 128 ? '#000000' : '#ffffff';
12+
}
13+

0 commit comments

Comments
 (0)