-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathisRgbColor.js
More file actions
42 lines (37 loc) · 1.52 KB
/
Copy pathisRgbColor.js
File metadata and controls
42 lines (37 loc) · 1.52 KB
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
/* eslint-disable prefer-rest-params */
import assertString from './util/assertString';
const rgbColor = /^rgb\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){2}([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])\)$/;
const rgbaColor = /^rgba\((([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5]),){3}(0(\.\d+)?|1(\.0+)?|\.\d+)\)$/;
const rgbColorPercent = /^rgb\((([0-9]%|[1-9][0-9]%|100%),){2}([0-9]%|[1-9][0-9]%|100%)\)$/;
const rgbaColorPercent = /^rgba\((([0-9]%|[1-9][0-9]%|100%),){3}(0(\.\d+)?|1(\.0+)?|\.\d+)\)$/;
const startsWithRgb = /^rgba?/;
export default function isRgbColor(str, options) {
assertString(str);
// default options to true for percent and false for spaces
let allowSpaces = false;
let includePercentValues = true;
if (typeof options !== 'object') {
if (arguments.length >= 2) {
includePercentValues = arguments[1];
}
} else {
allowSpaces = options.allowSpaces !== undefined ? options.allowSpaces : allowSpaces;
includePercentValues = options.includePercentValues !== undefined ?
options.includePercentValues : includePercentValues;
}
if (allowSpaces) {
// make sure it starts with continuous rgba? without spaces before stripping
if (!startsWithRgb.test(str)) {
return false;
}
// strip all whitespace
str = str.replace(/\s/g, '');
}
if (!includePercentValues) {
return rgbColor.test(str) || rgbaColor.test(str);
}
return rgbColor.test(str) ||
rgbaColor.test(str) ||
rgbColorPercent.test(str) ||
rgbaColorPercent.test(str);
}