forked from quasarframework/quasar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolors.js
More file actions
41 lines (33 loc) · 854 Bytes
/
colors.js
File metadata and controls
41 lines (33 loc) · 854 Bytes
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
/*
* Credits go to sindresorhus
*/
export function rgbToHex (red, green, blue) {
if (typeof red === 'string') {
const res = red.match(/\b\d{1,3}\b/g).map(Number)
red = res[0]
green = res[1]
blue = res[2]
}
if (
typeof red !== 'number' ||
typeof green !== 'number' ||
typeof blue !== 'number' ||
red > 255 ||
green > 255 ||
blue > 255
) {
throw new TypeError('Expected three numbers below 256')
}
return ((blue | green << 8 | red << 16) | 1 << 24).toString(16).slice(1)
}
export function hexToRgb (hex) {
if (typeof hex !== 'string') {
throw new TypeError('Expected a string')
}
hex = hex.replace(/^#/, '')
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]
}
let num = parseInt(hex, 16)
return [num >> 16, num >> 8 & 255, num & 255]
}