forked from websanova/rgbHex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrgbHex.js
79 lines (64 loc) · 2.12 KB
/
rgbHex.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
window.rgbHex = (function() {
function isNumeric(arg) {
return !isNaN(parseFloat(arg)) && isFinite(arg);
}
function trim(arg) {
return arg.replace(/^\s+|\s+$/g, "");
}
function isRgb(arg) {
arg = trim(arg);
return isNumeric(arg) && arg >= 0 && arg <= 255;
}
function isHex(arg) {
return /^[0-9a-f]{3}$|^[0-9a-f]{6}$/i.test(trim(arg));
}
function rgbToHex(arg) {
arg = parseInt(arg, 10).toString(16);
return arg.length === 1 ? '0' + arg : arg;
}
function hexToRgb(arg) {
return parseInt(arg, 16).toString();
}
function processRgb(arg) {
arg = arg.split(',');
if ( (arg.length === 3 || arg.length === 4) && isRgb(arg[0]) && isRgb(arg[1]) && isRgb(arg[2]) ) {
if(arg.length === 4 && !isNumeric(arg[3])) { return null; }
return '#' + rgbToHex(arg[0]).toUpperCase() + rgbToHex(arg[1]).toUpperCase() + rgbToHex(arg[2]).toUpperCase();
}
else {
return null;
}
}
function processHex(arg) {
if(isHex(arg)) {
// Error?? If you have this: FEA wouldn't this make FEAFEA and should this be FFEEAA?
if(arg.length === 3) { arg = arg + arg; }
return 'rgb(' + hexToRgb(arg.substr(0,2)) + ',' + hexToRgb(arg.substr(2,2)) + ',' + hexToRgb(arg.substr(4,2)) + ')';
}
}
return function(arg) {
if(!arg) { return null; }
var code = null,
rgbRegex = /^rgba?\((.*)\);?$/,
hexRegex = /^#/;
arg = trim(arg.toString());
if( rgbRegex.test(arg) )
{
return processRgb(arg.match(rgbRegex)[1]);
}
else if (hexRegex.test(arg)) {
return processHex(arg.split('#').reverse()[0]);
}
//try to parse the string without rgb or #
else {
code = arg.split(',');
if (code.length === 1) { return processHex(arg); }
else if (code.length === 3 || code.length === 4) { return processRgb(arg); }
}
};
})();
if(jQuery) {
jQuery.extend({
rgbHex: function(arg) { return window.rgbHex(arg); }
});
}