-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.js
40 lines (36 loc) · 1.32 KB
/
parser.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
/*
* ES-TinyColor : RGB String Parsing
* ────────────────────────────────────────────────────────────────────────────
*/
import {PERMISSIVE_MATCH3, PERMISSIVE_MATCH4} from './utilities.js'
const matchers = (function () {
return {
rgb: new RegExp(`rgb${PERMISSIVE_MATCH3}`),
rgba: new RegExp(`rgba${PERMISSIVE_MATCH4}`),
}
})()
/**
* Permissive string parsing. Take in a number of formats, and output an object
* based on detected format.
*
* Try to match string input using regular expressions. Keep most of the number
* bounding out of this function - don't worry about [0,1] or [0,100] or [0,360]
* Just return an object and let the conversion functions handle that.
* This way the result will be the same whether the tinycolor is initialized
* with string or object.
*
* @param {string} color The color
* @return {object} Returns `{ r, g, b }` or `{ r, g, b, a }`
*/
export default function rgbStringToObject(color) {
let r, g, b, a, match
if ((match = matchers.rgb.exec(color))) {
[r, g, b] = match.splice(1, 3)
return {r, g, b}
}
if ((match = matchers.rgba.exec(color))) {
[r, g, b, a] = match.splice(1, 4)
return {r, g, b, a}
}
return false
}