Skip to content

Commit

Permalink
#25 Javascript: allowed rgb format in isValidateColor()
Browse files Browse the repository at this point in the history
  • Loading branch information
dzc34 committed Jun 4, 2017
1 parent 0d6e417 commit b0b27a6
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions contrast-finder-webapp/src/main/webapp/Js/36-sample.color.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ $(document).ready(function() {

function changeColorSample(colorPrefix) {
var input = document.getElementById(colorPrefix + "-input");
var color = input.value;
var color = input.value.toLowerCase();
var sample = document.getElementById(colorPrefix + "-sample");
color = color.toString().trim();
// color = color.toString().replace(/\s/g,""); // replace ' ', \t, \n, ...
color = color.toString().replace(/\s/g,""); // replace ' ', \t, \n, ...
color = isValidateColor(color.toString());
if (color !== "false") {
sample.style.backgroundColor = color;
Expand All @@ -34,9 +33,14 @@ function changeColorSample(colorPrefix) {
}

function isValidateColor(str) {
if (str.match(/^#[a-fA-F0-9]{6}$/i) !== null
|| str.match(/^#[a-fA-F0-9]{3}$/i) !== null)
{
if(str.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/) !== null){
return str;
}
else if (str.match(/^(\d{1,3}),(\d{1,3}),(\d{1,3})$/) !== null){
return "rgb("+ str + ")";
}
else if (str.match(/^#[a-fA-F0-9]{6}$/i) !== null
|| str.match(/^#[a-fA-F0-9]{3}$/i) !== null){
return str;
}
else if (str.match(/^#?([a-fA-F0-9]{6})$/) !== null
Expand Down

0 comments on commit b0b27a6

Please sign in to comment.