diff --git a/README.md b/README.md
index 4cf147635..96bd8c600 100644
--- a/README.md
+++ b/README.md
@@ -152,7 +152,7 @@ Validator | Description
**isPort(str)** | check if the string is a valid port number.
**isPostalCode(str, locale)** | check if the string is a postal code.
`locale` is one of `['AD', 'AT', 'AU', 'AZ', 'BA', 'BE', 'BG', 'BR', 'BY', 'CA', 'CH', 'CN', 'CZ', 'DE', 'DK', 'DO', 'DZ', 'EE', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HT', 'HU', 'ID', 'IE', 'IL', 'IN', 'IR', 'IS', 'IT', 'JP', 'KE', 'KR', 'LI', 'LK', 'LT', 'LU', 'LV', 'MG', 'MT', 'MX', 'MY', 'NL', 'NO', 'NP', 'NZ', 'PL', 'PR', 'PT', 'RO', 'RU', 'SA', 'SE', 'SG', 'SI', 'SK', 'TH', 'TN', 'TW', 'UA', 'US', 'ZA', 'ZM']` OR `'any'`. If 'any' is used, function will check if any of the locales match. Locale list is `validator.isPostalCodeLocales`.
**isRFC3339(str)** | check if the string is a valid [RFC 3339][RFC 3339] date.
-**isRgbColor(str [, includePercentValues])** | check if the string is a rgb or rgba color.
`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false.
+**isRgbColor(str [,options])** | check if the string is a rgb or rgba color.
`options` is an object with the following properties
`includePercentValues` defaults to `true`. If you don't want to allow to set `rgb` or `rgba` values with percents, like `rgb(5%,5%,5%)`, or `rgba(90%,90%,90%,.3)`, then set it to false.
`allowSpaces` defaults to `true`, which prohibits whitespace. If set to false, whitespace between color values is allowed, such as `rgb(255, 255, 255)` or even `rgba(255, 128, 0, 0.7)`.
**isSemVer(str)** | check if the string is a Semantic Versioning Specification (SemVer).
**isSurrogatePair(str)** | check if the string contains any surrogate pairs chars.
**isUppercase(str)** | check if the string is uppercase.
diff --git a/src/lib/isRgbColor.js b/src/lib/isRgbColor.js
index 9458522ab..5267d563d 100644
--- a/src/lib/isRgbColor.js
+++ b/src/lib/isRgbColor.js
@@ -1,12 +1,35 @@
+/* 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)?|0(\.0)?)\)$/;
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)?|0(\.0)?)\)$/;
+const startsWithRgb = /^rgba?/;
-export default function isRgbColor(str, includePercentValues = true) {
+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 continous 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);
diff --git a/test/validators.test.js b/test/validators.test.js
index 6b603ff1b..252fae1e0 100644
--- a/test/validators.test.js
+++ b/test/validators.test.js
@@ -4580,9 +4580,53 @@ describe('Validators', () => {
'rgba(3%,3%,101%,0.3)',
'rgb(101%,101%,101%) additional invalid string part',
'rgba(3%,3%,101%,0.3) additional invalid string part',
+ 'r g b( 0, 251, 222 )',
+ 'r g ba( 0, 251, 222 )',
+ 'rg ba(0, 251, 22, 0.5)',
+ 'rgb( 255,255 ,255)',
+ 'rgba(255, 255, 255, 0.5)',
+ 'rgba(255, 255, 255, 0.5)',
+ 'rgb(5%, 5%, 5%)',
],
});
+ // test empty options object
+ test({
+ validator: 'isRgbColor',
+ args: [{}],
+ valid: [
+ 'rgb(0,0,0)',
+ 'rgb(255,255,255)',
+ 'rgba(0,0,0,0)',
+ 'rgba(255,255,255,1)',
+ 'rgba(255,255,255,.1)',
+ 'rgba(255,255,255,0.1)',
+ 'rgb(5%,5%,5%)',
+ 'rgba(5%,5%,5%,.3)',
+ ],
+ invalid: [
+ 'rgb(0,0,0,)',
+ 'rgb(0,0,)',
+ 'rgb(0,0,256)',
+ 'rgb()',
+ 'rgba(0,0,0)',
+ 'rgba(255,255,255,2)',
+ 'rgba(255,255,255,.12)',
+ 'rgba(255,255,256,0.1)',
+ 'rgb(4,4,5%)',
+ 'rgba(5%,5%,5%)',
+ 'rgba(3,3,3%,.3)',
+ 'rgb(101%,101%,101%)',
+ 'rgba(3%,3%,101%,0.3)',
+ 'r g b( 0, 251, 222 )',
+ 'r g ba( 0, 251, 222 )',
+ 'rg ba(0, 251, 22, 0.5)',
+ 'rgb( 255,255 ,255)',
+ 'rgba(255, 255, 255, 0.5)',
+ 'rgba(255, 255, 255, 0.5)',
+ 'rgb(5%, 5%, 5%)',
+ ],
+ });
// test where includePercentValues is given as false
test({
validator: 'isRgbColor',
@@ -4594,6 +4638,159 @@ describe('Validators', () => {
invalid: [
'rgb(4,4,5%)',
'rgba(5%,5%,5%)',
+ 'r g b( 0, 251, 222 )',
+ 'r g ba( 0, 251, 222 )',
+ ],
+ });
+
+ // test where includePercentValues is given as false as part of options object
+ test({
+ validator: 'isRgbColor',
+ args: [{ includePercentValues: false }],
+ valid: [
+ 'rgb(5,5,5)',
+ 'rgba(5,5,5,.3)',
+ ],
+ invalid: [
+ 'rgb(4,4,5%)',
+ 'rgba(5%,5%,5%)',
+ 'r g b( 0, 251, 222 )',
+ 'rgba(255, 255, 255 ,0.2)',
+ 'r g ba( 0, 251, 222 )',
+ ],
+ });
+
+ // test where include percent is true explciitly
+ test({
+ validator: 'isRgbColor',
+ args: [true],
+ valid: [
+ 'rgb(5,5,5)',
+ 'rgba(5,5,5,.3)',
+ 'rgb(0,0,0)',
+ 'rgb(255,255,255)',
+ 'rgba(0,0,0,0)',
+ 'rgba(255,255,255,1)',
+ 'rgba(255,255,255,.1)',
+ 'rgba(255,255,255,0.1)',
+ 'rgb(5%,5%,5%)',
+ 'rgba(5%,5%,5%,.3)',
+ 'rgb(5%,5%,5%)',
+ 'rgba(255,255,255,0.5)',
+ ],
+ invalid: [
+ 'rgba(255, 255, 255, 0.5)',
+ 'rgb(5%, 5%, 5%)',
+ 'rgb(4,4,5%)',
+ 'rgba(5%,5%,5%)',
+ 'r g b( 0, 251, 222 )',
+ 'r g ba( 0, 251, 222 )',
+ 'rgb(0,0,0,)',
+ 'rgb(0,0,)',
+ 'rgb(0,0,256)',
+ 'rgb()',
+ 'rgba(0,0,0)',
+ 'rgba(255,255,255,2)',
+ 'rgba(255,255,255,.12)',
+ 'rgba(255,255,256,0.1)',
+ 'rgb(4,4,5%)',
+ 'rgba(5%,5%,5%)',
+ 'rgba(3,3,3%,.3)',
+ 'rgb(101%,101%,101%)',
+ 'rgba(3%,3%,101%,0.3)',
+ ],
+ });
+
+ // test where percent value is false and allowSpaces is true as part of options object
+ test({
+ validator: 'isRgbColor',
+ args: [{ includePercentValues: false, allowSpaces: true }],
+ valid: [
+ 'rgb(5,5,5)',
+ 'rgba(5,5,5,.3)',
+ 'rgba(255,255,255,0.2)',
+ 'rgba(255, 255, 255 ,0.2)',
+ ],
+ invalid: [
+ 'rgb(4,4,5%)',
+ 'rgba(5%,5%,5%)',
+ 'rgba(5% ,5%, 5%)',
+ 'r g b( 0, 251, 222 )',
+ 'r g ba( 0, 251, 222 )',
+ 'rgb(0,0,)',
+ 'rgb()',
+ 'rgb(4,4,5%)',
+ 'rgb(5%,5%,5%)',
+ 'rgba(3,3,3%,.3)',
+ 'rgb(101%, 101%, 101%)',
+ 'rgba(3%,3%,101%,0.3)',
+ ],
+
+ });
+
+ // test where both are true as part of options object
+ test({
+ validator: 'isRgbColor',
+ args: [{ includePercentValues: true, allowSpaces: true }],
+ valid: [
+ 'rgb( 5, 5, 5)',
+ 'rgba(5, 5, 5, .3)',
+ 'rgb(0, 0, 0)',
+ 'rgb(255, 255, 255)',
+ 'rgba(0, 0, 0, 0)',
+ 'rgba(255, 255, 255, 1)',
+ 'rgba(255, 255, 255, .1)',
+ 'rgba(255, 255, 255, 0.1)',
+ 'rgb(5% ,5% ,5%)',
+ 'rgba(5%,5%,5%, .3)',
+ ],
+ invalid: [
+ 'r g b( 0, 251, 222 )',
+ 'rgb(4,4,5%)',
+ 'rgb(101%,101%,101%)',
+
+ ],
+ });
+
+ // test where allowSpaces is false as part of options object
+ test({
+ validator: 'isRgbColor',
+ args: [{ includePercentValues: true, allowSpaces: false }],
+ valid: [
+ 'rgb(5,5,5)',
+ 'rgba(5,5,5,.3)',
+ 'rgb(0,0,0)',
+ 'rgb(255,255,255)',
+ 'rgba(0,0,0,0)',
+ 'rgba(255,255,255,1)',
+ 'rgba(255,255,255,.1)',
+ 'rgba(255,255,255,0.1)',
+ 'rgb(5%,5%,5%)',
+ 'rgba(5%,5%,5%,.3)',
+
+ ],
+ invalid: [
+ 'rgb( 255,255 ,255)',
+ 'rgba(255, 255, 255, 0.5)',
+ 'rgb(5%, 5%, 5%)',
+ 'rgba(255, 255, 255, 0.5)',
+ 'rgb(4,4,5%)',
+ 'rgba(5%,5%,5%)',
+ 'r g b( 0, 251, 222 )',
+ 'r g ba( 0, 251, 222 )',
+ 'rgb(0,0,0,)',
+ 'rgb(0,0,)',
+ 'rgb(0,0,256)',
+ 'rgb()',
+ 'rgba(0,0,0)',
+ 'rgba(255,255,255,2)',
+ 'rgba(255,255,255,.12)',
+ 'rgba(255,255,256,0.1)',
+ 'rgb(4,4,5%)',
+ 'rgba(5%,5%,5%)',
+ 'rgba(3,3,3%,.3)',
+ 'rgb(101%,101%,101%)',
+ 'rgba(3%,3%,101%,0.3)',
],
});
});