Skip to content

Instantly share code, notes, and snippets.

@rijkvanzanten
Created March 26, 2017 09:30
Show Gist options
  • Save rijkvanzanten/d3d35db6bb127b95d4db10ea650b8433 to your computer and use it in GitHub Desktop.
Save rijkvanzanten/d3d35db6bb127b95d4db10ea650b8433 to your computer and use it in GitHub Desktop.
/**
* Validates RGB(a) value
* @param {Number} r
* @param {Number} g
* @param {Number} b
* @param {Number} [a=0]
* @return {Boolean}
*/
function isValidRGB(r, g, b, a) {
a = a || 0;
if(typeof r !== 'number' || typeof g !== 'number' || typeof b !== 'number' || typeof a !== 'number') return false;
var regex = /\b(1?[0-9]{1,2}|2[0-4][0-9]|25[0-5])\b/;
if(
regex.test(r) &&
regex.test(g) &&
regex.test(b) &&
a >= 0 && a <= 1
) return true;
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment