-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
128 lines (111 loc) · 1.81 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
/**
* Module dependencies.
*/
var colors = require('./colors');
/**
* Expose `parse`.
*/
module.exports = parse;
/**
* Parse `str`.
*
* @param {String} str
* @return {Object}
* @api public
*/
function parse(str) {
return named(str)
|| hex3(str)
|| hex6(str)
|| rgb(str)
|| rgba(str);
}
/**
* Parse named css color `str`.
*
* @param {String} str
* @return {Object}
* @api private
*/
function named(str) {
var c = colors[str.toLowerCase()];
if (!c) return;
return {
r: c[0],
g: c[1],
b: c[2],
a: 1
}
}
/**
* Parse rgb(n, n, n)
*
* @param {String} str
* @return {Object}
* @api private
*/
function rgb(str) {
if (0 == str.indexOf('rgb(')) {
str = str.match(/rgb\(([^)]+)\)/)[1];
var parts = str.split(/ *, */).map(Number);
return {
r: parts[0],
g: parts[1],
b: parts[2],
a: 1
}
}
}
/**
* Parse rgba(n, n, n, n)
*
* @param {String} str
* @return {Object}
* @api private
*/
function rgba(str) {
if (0 == str.indexOf('rgba(')) {
str = str.match(/rgba\(([^)]+)\)/)[1];
var parts = str.split(/ *, */).map(Number);
return {
r: parts[0],
g: parts[1],
b: parts[2],
a: parts[3]
}
}
}
/**
* Parse #nnnnnn
*
* @param {String} str
* @return {Object}
* @api private
*/
function hex6(str) {
if ('#' == str[0] && 7 == str.length) {
return {
r: parseInt(str.slice(1, 3), 16),
g: parseInt(str.slice(3, 5), 16),
b: parseInt(str.slice(5, 7), 16),
a: 1
}
}
}
/**
* Parse #nnn
*
* @param {String} str
* @return {Object}
* @api private
*/
function hex3(str) {
if ('#' == str[0] && 4 == str.length) {
return {
r: parseInt(str[1] + str[1], 16),
g: parseInt(str[2] + str[2], 16),
b: parseInt(str[3] + str[3], 16),
a: 1
}
}
}