-
Notifications
You must be signed in to change notification settings - Fork 5
/
utils.js
83 lines (72 loc) · 1.56 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
var $ = document.querySelectorAll.bind(document);
var $$ = document.querySelector.bind(document);
Element.prototype.on = Element.prototype.addEventListener;
function hasClass(ele,cls) {
if (ele) {
return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
}
function addClass(ele,cls) {
if (ele) {
if (!hasClass(ele,cls)) ele.className += " "+cls;
}
}
function removeClass(ele,cls) {
if (ele) {
if (hasClass(ele,cls)) {
var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
ele.className=ele.className.replace(reg,'');
}
}
}
function hexToRgb(hex) {
var reg,
res,
result;
if (hex.length === 7) {
reg = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i;
result = reg.exec(hex);
}
else if (hex.length === 4) {
reg = /^#?([a-f\d]{1})([a-f\d]{1})([a-f\d]{1})$/i;
res = reg.exec(hex);
result = [];
for (var i = 1; i <= res.length; i++) {
result[i] = res[i] + res[i];
}
}
else {
return false;
}
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : false;
}
function rgb(hex) {
var res = hexToRgb(hex),
color = '';
if (res) {
color = 'rgb(' + res.r + ', ' + res.g + ', ' + res.b + ')';
}
return color;
}
function rgba(hex, a) {
var res = hexToRgb(hex),
color = '';
if (res) {
color = 'rgba(' + res.r + ', ' + res.g + ', ' + res.b + ', ' + a + ')';
}
return color;
}
function isEven(number) {
if (number % 2 === 0) {
return true;
}
return false;
}
function prev(event) {
if (event.preventDefault) { event.preventDefault(); }
else { event.returnValue = false; }
}