forked from kamicane/rgb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
136 lines (117 loc) · 3.68 KB
/
index.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
color
*/"use strict"
var colors = {
maroon : "#800000",
red : "#ff0000",
orange : "#ffA500",
yellow : "#ffff00",
olive : "#808000",
purple : "#800080",
fuchsia : "#ff00ff",
white : "#ffffff",
lime : "#00ff00",
green : "#008000",
navy : "#000080",
blue : "#0000ff",
aqua : "#00ffff",
teal : "#008080",
black : "#000000",
silver : "#c0c0c0",
gray : "#808080",
transparent : "#0000"
}
var RGBtoRGB = function(r, g, b, a){
if (a == null || a === "") a = 1
r = parseFloat(r)
g = parseFloat(g)
b = parseFloat(b)
a = parseFloat(a)
if (!(r <= 255 && r >= 0 && g <= 255 && g >= 0 && b <= 255 && b >= 0 && a <= 1 && a >= 0)) return null
return [Math.round(r), Math.round(g), Math.round(b), a]
}
var HEXtoRGB = function(hex){
if (hex.length === 3) hex += "f"
if (hex.length === 4){
var h0 = hex.charAt(0),
h1 = hex.charAt(1),
h2 = hex.charAt(2),
h3 = hex.charAt(3)
hex = h0 + h0 + h1 + h1 + h2 + h2 + h3 + h3
}
if (hex.length === 6) hex += "ff"
var rgb = []
for (var i = 0, l = hex.length; i < l; i += 2) rgb.push(parseInt(hex.substr(i, 2), 16) / (i === 6 ? 255 : 1))
return rgb
}
// HSL to RGB conversion from:
// http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
// thank you!
var HUEtoRGB = function(p, q, t){
if (t < 0) t += 1
if (t > 1) t -= 1
if (t < 1 / 6) return p + (q - p) * 6 * t
if (t < 1 / 2) return q
if (t < 2 / 3) return p + (q - p) * (2 / 3 - t) * 6
return p
}
var HSLtoRGB = function(h, s, l, a){
var r, b, g
if (a == null || a === "") a = 1
h = parseFloat(h) / 360
s = parseFloat(s) / 100
l = parseFloat(l) / 100
a = parseFloat(a) / 1
if (h > 1 || h < 0 || s > 1 || s < 0 || l > 1 || l < 0 || a > 1 || a < 0) return null
if (s === 0){
r = b = g = l
} else {
var q = l < 0.5 ? l * (1 + s) : l + s - l * s
var p = 2 * l - q
r = HUEtoRGB(p, q, h + 1 / 3)
g = HUEtoRGB(p, q, h)
b = HUEtoRGB(p, q, h - 1 / 3)
}
return [r * 255, g * 255, b * 255, a]
}
var keys = []
for (var c in colors) keys.push(c)
var shex = "(?:#([a-f0-9]{3,8}))",
sval = "\\s*([.\\d%]+)\\s*",
sop = "(?:,\\s*([.\\d]+)\\s*)?",
slist = "\\(" + [sval, sval, sval] + sop + "\\)",
srgb = "(?:rgb)a?",
shsl = "(?:hsl)a?",
skeys = "(" + keys.join("|") + ")"
var xhex = RegExp(shex, "i"),
xrgb = RegExp(srgb + slist, "i"),
xhsl = RegExp(shsl + slist, "i")
var color = function(input, array){
if (input == null) return null
input = (input + "").replace(/\s+/, "")
var match = colors[input]
if (match){
return color(match, array)
} else if (match = input.match(xhex)){
input = HEXtoRGB(match[1])
} else if (match = input.match(xrgb)){
input = match.slice(1)
} else if (match = input.match(xhsl)){
input = HSLtoRGB.apply(null, match.slice(1))
} else return null
if (!(input && (input = RGBtoRGB.apply(null, input)))) return null
if (array) return input
if (input[3] === 1) input.splice(3, 1)
return "rgb" + (input.length === 4 ? "a" : "") + "(" + input + ")"
}
var regexp = RegExp([skeys, shex, srgb + slist, shsl + slist].join("|"), "gi")
color.replace = function(string, method){
if (!method) method = function(match){
return color(match)
}
return (string + "").replace(regexp, method)
}
color.matches = function(string){
return !!(string + "").match(regexp)
}
module.exports = color