Skip to content

Commit

Permalink
Fixed Asqatasun#63 - Allowed "rgb(255,255,255" for "rgb(255,255,255)"…
Browse files Browse the repository at this point in the history
… in the form
  • Loading branch information
dzc34 committed Jun 11, 2017
1 parent 4c3d61f commit 1d07a4f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ public final class ColorConverter {
private static final int CONSTANT_S_COMPONENTS_FIFTY = 50;
private static final String HEXADECIMAL_DICTIONNARY = "[0-9A-Fa-f]+"; // FFF, FFFFFF
private static final String HEXADECIMAL_DICTIONNARY_V2 = "^#?([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$"; // #FFF, #FFFFFF, FFF, FFFFFF (but not FF or FFFF)
private static final String SHORT_RGB_DICTIONNARY = "^[0-9]{1,3},[0-9]{1,3},[0-9]{1,3}$"; // ex: 255,255,255
private static final String RGB_DICTIONNARY = "^rgb\\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\\)$"; // ex: rgb(255,255,255)
private static final String SHORT_RGB_DICTIONNARY = "^[0-9]{1,3},[0-9]{1,3},[0-9]{1,3}$"; // ex: "255,255,255"
private static final String RGB_DICTIONNARY = "^rgb\\([0-9]{1,3},[0-9]{1,3},[0-9]{1,3}\\)?$"; // ex: "rgb(255,255,255)" and "rgb(255,255,255"

/**
* Private constructor, utility class
Expand Down Expand Up @@ -131,10 +131,13 @@ public static Color offsetRgbColor(Color bgColor, int offsetRed, int offsetGreen
*/
public static String formatColorStr(String colorStr) {
String str = colorStr.replaceAll("\\s", ""); // replace ' ', \t, \n, ...
if (str.toLowerCase().matches(RGB_DICTIONNARY)){ // ex: rgb(255,255,255)
if (str.toLowerCase().matches(RGB_DICTIONNARY)){ // ex: "rgb(255,255,255)" and "rgb(255,255,255"
str = str.toLowerCase();
if(!str.endsWith(")")){
str = str + ")";
}
}
else if(str.matches(SHORT_RGB_DICTIONNARY)){ // ex: 255,255,255
else if(str.matches(SHORT_RGB_DICTIONNARY)){ // ex: "255,255,255"
str = "rgb(" + str + ")";
}
else if(str.matches(HEXADECIMAL_DICTIONNARY_V2)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,17 +142,30 @@ public void testGetHue() {

// formatColor()
/////////////////////////////////////////////////////////////////////
public void testFormatColorRgbLastParenthesisIsMissing() {
String expResult = "rgb(255,255,255)";
String colorStr = "rgb(255,255,255"; // # must be added
System.out.println("formatColorRgblastParenthesisIsMissing ["+ colorStr +"]");
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);

colorStr = " rgb( 255, 255, 255 "; // # must be added
System.out.println("formatColorRgblastParenthesisIsMissing ["+ colorStr +"]");
result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
}

public void testFormatColorRgbToLowerCase() {
System.out.println("formatColorRgbToLowerCase [RGB(255,255,255)]");
String colorStr = "RGB(255,255,255)"; // # must be added
String colorStr = "RGB(255,255,255)";
String expResult = "rgb(255,255,255)";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
}

public void testFormatColorRgb() {
System.out.println("formatColorRgb [ rgb( 255, 255, 255 ) ]");
String colorStr = " rgb( 255, 255, 255 ) "; // # must be added
String colorStr = " rgb( 255, 255, 255 ) ";
String expResult = "rgb(255,255,255)";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
Expand All @@ -162,15 +175,15 @@ public void testFormatColorRgb() {

public void testFormatColorRgbShortWidthSpace() {
System.out.println("formatColorRgbShortWidthSpace [ 255, 255, 255 ]");
String colorStr = " 255, 255, 255 "; // # must be added
String colorStr = " 255, 255, 255 ";
String expResult = "rgb(255,255,255)";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
}

public void testFormatColorRgbShort() {
System.out.println("formatColorRgbShort [255,255,255]");
String colorStr = "255,255,255"; // # must be added
String colorStr = "255,255,255";
String expResult = "rgb(255,255,255)";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
Expand Down
37 changes: 32 additions & 5 deletions contrast-finder-webapp/src/main/webapp/Js/36-sample.color.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,42 @@
function setValidColor(str) {
var result = str.splice(0, 0, "#");
return result;
}


String.prototype.splice = function(idx, rem, s) {
return (this.slice(0, idx) + s + this.slice(idx + Math.abs(rem)));
};

// only run when the substr() function is broken
// Microsoft's JScript does not support negative values for the start index.
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr
if ("ab".substr(-1) !== 'b') {
/**
* Get the substring of a string
* @param {integer} start where to start the substring
* @param {integer} length how many characters to return
* @return {string}
*/
String.prototype.substr = function(substr) {
return function(start, length) {
// call the original method
return substr.call(this,
// did we get a negative start, calculate how much it is from the beginning of the string
// adjust the start parameter for negative value
start < 0 ? this.length + start : start,
length);
};
}(String.prototype.substr);
}

function setValidColor(str) {
var result = str.splice(0, 0, "#");
return result;
}

function isValidateColor(str) {
str = str.trim();
if(str.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)$/) !== null){
if(str.match(/^rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)?$/) !== null){
if(str.substr(-1) !== ")"){
str = str + ")";
}
return str;
}
else if (str.match(/^(\d{1,3}),(\d{1,3}),(\d{1,3})$/) !== null){
Expand Down

0 comments on commit 1d07a4f

Please sign in to comment.