-
-
Notifications
You must be signed in to change notification settings - Fork 185
/
utilities.js
57 lines (44 loc) · 1.31 KB
/
utilities.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
export const hashCode = (name) => {
var hash = 0;
for (var i = 0; i < name.length; i++) {
var character = name.charCodeAt(i);
hash = ((hash<<5)-hash)+character;
hash = hash & hash; // Convert to 32bit integer
}
return Math.abs(hash);
}
export const getModulus = (num, max) => {
return num % max;
}
export const getDigit = (number, ntn) => {
return Math.floor(( number/ Math.pow(10, ntn)) % 10);
}
export const getBoolean = (number, ntn) => {
return (!((getDigit(number, ntn)) % 2))
}
export const getAngle = (x, y) => {
return Math.atan2(y, x) * 180 / Math.PI;
}
export const getUnit = (number, range, index) => {
let value = number % range
if(index && ((getDigit(number, index) % 2) === 0)) {
return -value
} else return value
}
export const getRandomColor = (number, colors, range) => {
return colors[(number) % range]
}
export const getContrast = (hexcolor) => {
// If a leading # is provided, remove it
if (hexcolor.slice(0, 1) === '#') {
hexcolor = hexcolor.slice(1);
}
// Convert to RGB value
var r = parseInt(hexcolor.substr(0,2),16);
var g = parseInt(hexcolor.substr(2,2),16);
var b = parseInt(hexcolor.substr(4,2),16);
// Get YIQ ratio
var yiq = ((r * 299) + (g * 587) + (b * 114)) / 1000;
// Check contrast
return (yiq >= 128) ? '#000000' : '#FFFFFF';
};