-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathutils.js
64 lines (57 loc) · 1.66 KB
/
utils.js
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
export function colorLuminance(hex, lum) {
// validate hex string
hex = String(hex).replace(/[^0-9a-f]/gi, '')
if (hex.length < 6) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2]
}
lum = lum || 0
// convert to decimal and change luminosity
let rgb = '#',
c,
i
for (i = 0; i < 3; i++) {
c = parseInt(hex.substr(i * 2, 2), 16)
c = Math.round(Math.min(Math.max(0, c + c * lum), 255)).toString(16)
rgb += ('00' + c).substr(c.length)
}
return rgb
}
export function getContrast(hex) {
const r = parseInt(hex.substr(1, 2), 16),
g = parseInt(hex.substr(3, 2), 16),
b = parseInt(hex.substr(5, 2), 16),
yiq = (r * 299 + g * 587 + b * 114) / 1000
return yiq >= 128 ? '#001f3f' : '#F6F5F7'
}
export const isValidColor = hex => /^#[0-9A-F]{6}$/i.test(hex)
export const getColorFromRoute = () => {
if (window.location.hash) {
if (/^#[0-9A-F]{6}$/i.test(window.location.hash)) {
return window.location.hash
}
}
}
export const getSizes = () => {
const windowWidth = window.innerWidth
const windowHeight = window.innerHeight
if ((windowWidth < 1000 || windowHeight < 860) && window.navigator.userAgent !== 'ReactSnap') {
if (windowWidth < 800) {
if (windowWidth < 680) {
return { maxSize: 180, size: 150 }
} else {
return { maxSize: 250, size: 200 }
}
} else {
return { maxSize: 350, size: 250 }
}
} else {
return { maxSize: 410, size: 300 }
}
}
export const camelize = str => {
return str
.replace(/(?:^\w|[A-Z]|\b\w)/g, function (word, index) {
return index === 0 ? word.toLowerCase() : word.toUpperCase()
})
.replace(/\s+/g, '')
}