forked from clerk/javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.ts
More file actions
178 lines (144 loc) · 4.57 KB
/
Copy pathcolor.ts
File metadata and controls
178 lines (144 loc) · 4.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import type { Color, HslaColor, RgbaColor, TransparentColor } from '@clerk/types';
const IS_HEX_COLOR_REGEX = /^#?([A-F0-9]{6}|[A-F0-9]{3})$/i;
const IS_RGB_COLOR_REGEX = /^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/i;
const IS_RGBA_COLOR_REGEX = /^rgba\((\d+),\s*(\d+),\s*(\d+)(,\s*\d+(\.\d+)?)\)$/i;
const IS_HSL_COLOR_REGEX = /^hsl\((\d+),\s*([\d.]+)%,\s*([\d.]+)%\)$/i;
const IS_HSLA_COLOR_REGEX = /^hsla\((\d+),\s*([\d.]+)%,\s*([\d.]+)%(,\s*\d+(\.\d+)?)*\)$/i;
export const isValidHexString = (s: string) => {
return !!s.match(IS_HEX_COLOR_REGEX);
};
export const isValidRgbaString = (s: string) => {
return !!(s.match(IS_RGB_COLOR_REGEX) || s.match(IS_RGBA_COLOR_REGEX));
};
export const isValidHslaString = (s: string) => {
return !!s.match(IS_HSL_COLOR_REGEX) || !!s.match(IS_HSLA_COLOR_REGEX);
};
export const isRGBColor = (c: Color): c is RgbaColor => {
return typeof c !== 'string' && 'r' in c;
};
export const isHSLColor = (c: Color): c is HslaColor => {
return typeof c !== 'string' && 'h' in c;
};
export const isTransparent = (c: Color): c is TransparentColor => {
return c === 'transparent';
};
export const hasAlpha = (color: Color): boolean => {
return typeof color !== 'string' && color.a != undefined && color.a < 1;
};
const CLEAN_HSLA_REGEX = /[hsla()]/g;
const CLEAN_RGBA_REGEX = /[rgba()]/g;
export const stringToHslaColor = (value: string): HslaColor | null => {
if (value === 'transparent') {
return { h: 0, s: 0, l: 0, a: 0 };
}
if (isValidHexString(value)) {
return hexStringToHslaColor(value);
}
if (isValidHslaString(value)) {
return parseHslaString(value);
}
if (isValidRgbaString(value)) {
return rgbaStringToHslaColor(value);
}
return null;
};
export const stringToSameTypeColor = (value: string): Color => {
value = value.trim();
if (isValidHexString(value)) {
return value.startsWith('#') ? value : `#${value}`;
}
if (isValidRgbaString(value)) {
return parseRgbaString(value);
}
if (isValidHslaString(value)) {
return parseHslaString(value);
}
if (isTransparent(value)) {
return value;
}
return '';
};
export const colorToSameTypeString = (color: Color): string | TransparentColor => {
if (typeof color === 'string' && (isValidHexString(color) || isTransparent(color))) {
return color;
}
if (isRGBColor(color)) {
return rgbaColorToRgbaString(color);
}
if (isHSLColor(color)) {
return hslaColorToHslaString(color);
}
return '';
};
export const hexStringToRgbaColor = (hex: string): RgbaColor => {
hex = hex.replace('#', '');
const r = parseInt(hex.substring(0, 2), 16);
const g = parseInt(hex.substring(2, 4), 16);
const b = parseInt(hex.substring(4, 6), 16);
return { r, g, b };
};
const rgbaColorToRgbaString = (color: RgbaColor): string => {
const { a, b, g, r } = color;
return color.a === 0 ? 'transparent' : color.a != undefined ? `rgba(${r},${g},${b},${a})` : `rgb(${r},${g},${b})`;
};
const hslaColorToHslaString = (color: HslaColor): string => {
const { h, s, l, a } = color;
const sPerc = Math.round(s * 100);
const lPerc = Math.round(l * 100);
return color.a === 0
? 'transparent'
: color.a != undefined
? `hsla(${h},${sPerc}%,${lPerc}%,${a})`
: `hsl(${h},${sPerc}%,${lPerc}%)`;
};
const hexStringToHslaColor = (hex: string): HslaColor => {
const rgbaString = colorToSameTypeString(hexStringToRgbaColor(hex));
return rgbaStringToHslaColor(rgbaString);
};
const rgbaStringToHslaColor = (rgba: string): HslaColor => {
const rgbaColor = parseRgbaString(rgba);
const r = rgbaColor.r / 255;
const g = rgbaColor.g / 255;
const b = rgbaColor.b / 255;
const max = Math.max(r, g, b),
min = Math.min(r, g, b);
let h, s;
const l = (max + min) / 2;
if (max == min) {
h = s = 0;
} else {
const d = max - min;
s = l >= 0.5 ? d / (2 - (max + min)) : d / (max + min);
switch (max) {
case r:
h = ((g - b) / d) * 60;
break;
case g:
h = ((b - r) / d + 2) * 60;
break;
default:
h = ((r - g) / d + 4) * 60;
break;
}
}
const res: HslaColor = { h: Math.round(h), s, l };
const a = rgbaColor.a;
if (a != undefined) {
res.a = a;
}
return res;
};
const parseRgbaString = (str: string): RgbaColor => {
const [r, g, b, a] = str
.replace(CLEAN_RGBA_REGEX, '')
.split(',')
.map(c => Number.parseFloat(c));
return { r, g, b, a };
};
const parseHslaString = (str: string): HslaColor => {
const [h, s, l, a] = str
.replace(CLEAN_HSLA_REGEX, '')
.split(',')
.map(c => Number.parseFloat(c));
return { h, s: s / 100, l: l / 100, a };
};