PluginProbe
The Innovative Form Builder – IvyForms / 1.0
The Innovative Form Builder – IvyForms v1.0
1.5.1 1.5 1.4.1 1.4 trunk 0.1.2 0.2 0.2.1 0.3 0.3.1 0.4 0.5 0.6 0.6.1 0.6.1-backup 0.6.1.1 0.7 0.8 0.8.1 0.8.2 0.9 0.9.1 1.0 1.1 1.1.1 All 27 releases
ivyforms / frontend / src / utils / utilColor.ts

utilColor.ts in The Innovative Form Builder – IvyForms 1.0, at frontend/src/utils/utilColor.ts

59 lines 1.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 const getBrightness = (color: string) => {
2 if (!isHex(color)) {
3 color = rgbaToHex(color)
4 }
5
6 const r = parseInt(color.slice(1, 3), 16)
7 const g = parseInt(color.slice(3, 5), 16)
8 const b = parseInt(color.slice(5, 7), 16)
9
10 return (r * 299 + g * 587 + b * 114) / 1000
11 }
12
13 function rgbStyleToHex(color: string | null): string | null {
14 if (!color || color.indexOf('rgb') < 0) {
15 return color
16 }
17
18 if (color.charAt(0) == '#') {
19 return color
20 }
21
22 const nums = /(.*?)rgb\((\d+),\s*(\d+),\s*(\d+)\)/i.exec(color),
23 r = parseInt(nums![2], 10).toString(16),
24 g = parseInt(nums![3], 10).toString(16),
25 b = parseInt(nums![4], 10).toString(16)
26
27 return (
28 '#' +
29 ((r.length == 1 ? '0' + r : r) + (g.length == 1 ? '0' + g : g) + (b.length == 1 ? '0' + b : b))
30 ).toLowerCase()
31 }
32
33 const rgbaToHex = (color: string) => {
34 return `#${color
35 .match(/^rgba?\((\d+),\s*(\d+),\s*(\d+)(?:,\s*(\d+\.{0,1}\d*))?\)$/)
36 .slice(1)
37 .map((n, i) =>
38 (i === 3 ? Math.round(parseFloat(n) * 255) : parseFloat(n))
39 .toString(16)
40 .padStart(2, '0')
41 .replace('NaN', ''),
42 )
43 .join('')}`
44 }
45
46 const isHex = (color: string) => {
47 return /^#([0-9a-f]{3}){1,2}$/i.test(color)
48 }
49
50 const isDark = (color: string) => {
51 return getBrightness(color) < 128
52 }
53
54 const isLight = (color: string) => {
55 return getBrightness(color) > 128
56 }
57
58 export { isLight, isDark, rgbStyleToHex }
59