From b0b27a6c79f3284aa863da0931a718df6d3293f6 Mon Sep 17 00:00:00 2001 From: Fabrice Gangler Date: Sat, 3 Jun 2017 13:53:18 +0200 Subject: [PATCH] #25 Javascript: allowed rgb format in isValidateColor() --- .../src/main/webapp/Js/36-sample.color.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/contrast-finder-webapp/src/main/webapp/Js/36-sample.color.js b/contrast-finder-webapp/src/main/webapp/Js/36-sample.color.js index 0c274093..aaa5a1cb 100644 --- a/contrast-finder-webapp/src/main/webapp/Js/36-sample.color.js +++ b/contrast-finder-webapp/src/main/webapp/Js/36-sample.color.js @@ -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; @@ -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