Skip to content

Commit

Permalink
minor fix for ColorConverter.formatColorStr()
Browse files Browse the repository at this point in the history
  • Loading branch information
dzc34 committed Jun 4, 2017
1 parent 13f3771 commit 0d6e417
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ public final class ColorConverter {
private static final int RGB_SHORT_HEXA_LENGTH = 3;
private static final int CONVERT_TO_BASE_16 = 16;
private static final int MAX_ANGLE = 360;
private static final String HEXADECIMAL_DICTIONNARY = "[0-9A-Fa-f]+";
private static final String HEXADECIMAL_DICTIONNARY = "[0-9A-Fa-f]+"; // FFF, FFFFFF
private static final String HEXADECIMAL_DICTIONNARY_V2 = "#?[0-9A-Fa-f]+"; // #FFF, #FFFFFF

/**
* Private constructor, utility class
Expand Down Expand Up @@ -120,10 +121,15 @@ public static Color offsetRgbColor(Color bgColor, int offsetRed, int offsetGreen
*/
public static String formatColorStr(String colorStr) {
colorStr = colorStr.replaceAll("\\s", ""); // replace ' ', \t, \n, ...
if (colorStr.charAt(0) != '#') {
colorStr = "#" + colorStr;
if(colorStr.matches(HEXADECIMAL_DICTIONNARY_V2)) {
if (colorStr.charAt(0) != '#') {
colorStr = "#" + colorStr;
}
colorStr = colorStr.toUpperCase();
}
else {
colorStr = colorStr.toLowerCase();
}
colorStr = colorStr.toUpperCase();
return colorStr;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,23 @@ public void testGetHue() {

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

public void testFormatColorNotValidHex() {
System.out.println("formatColorNotValidHex [ZZZ]");
String colorStr = "ZZZ"; // # must be added
String expResult = "zzz";
String result = ColorConverter.formatColorStr(colorStr);
assertEquals(expResult, result);
}


public void testFormatColorHexAddHash() {
System.out.println("formatColorHexAddHash [000000]");
String colorStr = "000000"; // # must be added
Expand Down

0 comments on commit 0d6e417

Please sign in to comment.