forked from vercel/commerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.ts
50 lines (40 loc) · 1.14 KB
/
colors.ts
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
import random from 'lodash.random'
export function getRandomPairOfColors() {
const colors = ['#37B679', '#DA3C3C', '#3291FF', '#7928CA', '#79FFE1']
const getRandomIdx = () => random(0, colors.length - 1)
let idx = getRandomIdx()
let idx2 = getRandomIdx()
// Has to be a different color
while (idx2 === idx) {
idx2 = getRandomIdx()
}
// Returns a pair of colors
return [colors[idx], colors[idx2]]
}
function hexToRgb(hex: string = '') {
// @ts-ignore
const match = hex.toString(16).match(/[a-f0-9]{6}|[a-f0-9]{3}/i)
if (!match) {
return [0, 0, 0]
}
let colorString = match[0]
if (match[0].length === 3) {
colorString = colorString
.split('')
.map((char: string) => {
return char + char
})
.join('')
}
const integer = parseInt(colorString, 16)
const r = (integer >> 16) & 0xff
const g = (integer >> 8) & 0xff
const b = integer & 0xff
return [r, g, b]
}
export function isDark(color = '') {
// Equation from http://24ways.org/2010/calculating-color-contrast
const rgb = hexToRgb(color)
const res = (rgb[0] * 299 + rgb[1] * 587 + rgb[2] * 114) / 1000
return res < 128
}