Skip to content

Commit

Permalink
Moved formatColor() into ColorConverter class + added unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dzc34 committed Jun 3, 2017
1 parent 3815fbb commit 13f3771
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,19 @@ public static Color offsetRgbColor(Color bgColor, int offsetRed, int offsetGreen
return new Color(bgColor.getRed() + offsetRed, bgColor.getGreen() + offsetGreen, bgColor.getBlue() + offsetBlue);
}

/**
* @param colorStr ex: fff, FFF, #fff, #FFF, ffffff, (...)
* @return color in hexadecimal, with '#' appended if necessary, in upper case
*/
public static String formatColorStr(String colorStr) {
colorStr = colorStr.replaceAll("\\s", ""); // replace ' ', \t, \n, ...
if (colorStr.charAt(0) != '#') {
colorStr = "#" + colorStr;
}
colorStr = colorStr.toUpperCase();
return colorStr;
}

/**
* @param colorStr color in short hexadecimal format, example: #FFF or #FFFFFF
* @return the RGB Color from hex Color
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,53 @@ public void testGetHue() {
// // TODO review the generated test code and remove the default call to fail.
// fail("The test case is a prototype.");
// }



// formatColor()
/////////////////////////////////////////////////////////////////////
public void testFormatColorHexAddHash() {
System.out.println("formatColorHexAddHash [000000]");
String colorStr = "000000"; // # must be added
String expResult = "#000000";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
}

public void testFormatColorHexNotAddHash() {
System.out.println("formatColorHexNotAddHash [#000000]");
String colorStr = "#000000"; // # must not be added
String expResult = "#000000";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
}

public void testFormatColorHexTrim() {
System.out.println("formatColorHexTrim [ #000000 ]");
String colorStr = " #000000 ";
String expResult = "#000000";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
}

public void testFormatColorHexToUpperCase() {
System.out.println("formatColorHexToUpperCase [#ffffff]");
String colorStr = "#ffffff";
String expResult = "#FFFFFF";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
}


public void testFormatColorHex() {
System.out.println("formatColorHex [ ffffff ]");
String colorStr = " ffffff ";
String expResult = "#FFFFFF";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
}

// String rgb2Hex()
/////////////////////////////////////////////////////////////////////
public void testRgb2hexBlack() {
System.out.println("Rgb2hexBlack");
Color color = Color.BLACK;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.asqatasun.contrastFinder.webapp.model;

import org.apache.commons.lang3.StringUtils;
import org.asqatasun.utils.colorconvertor.ColorConverter;

/**
* @author alingua
Expand Down Expand Up @@ -63,7 +64,7 @@ public String getForeground() {
* @param foreground Set foregound color (expected as string) in hexadecimal format, beginning with '#'
*/
public void setForeground(String foreground) {
this.foreground = formatColor(foreground);
this.foreground = ColorConverter.formatColorStr(foreground);
}

/**
Expand All @@ -77,25 +78,9 @@ public String getBackground() {
}

public void setBackground(String background) {
this.background = formatColor(background);
this.background = ColorConverter.formatColorStr(background);
}

/**
* @param colorValue
* @return color in hexadecimal with '#' appended if necessary
*/
private String formatColor(String colorValue) {
colorValue = colorValue.trim();
// colorValue = colorValue.replaceAll("\\s", ""); // replace ' ', \t, \n, ...
if (colorValue.charAt(0) != '#') {
colorValue = "#" + colorValue;
}
colorValue = colorValue.toUpperCase();
return colorValue;
}



public boolean getIsBackgroundTested() {
return isBackgroundTested;
}
Expand Down

0 comments on commit 13f3771

Please sign in to comment.