diff --git a/contrast-finder-utils/src/main/java/org/asqatasun/utils/colorconvertor/ColorConverter.java b/contrast-finder-utils/src/main/java/org/asqatasun/utils/colorconvertor/ColorConverter.java index c3af4b33..ef207b2f 100644 --- a/contrast-finder-utils/src/main/java/org/asqatasun/utils/colorconvertor/ColorConverter.java +++ b/contrast-finder-utils/src/main/java/org/asqatasun/utils/colorconvertor/ColorConverter.java @@ -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 @@ -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; } diff --git a/contrast-finder-utils/src/test/java/org/asqatasun/utils/colorconvertor/ColorConverterTest.java b/contrast-finder-utils/src/test/java/org/asqatasun/utils/colorconvertor/ColorConverterTest.java index 2fa13ae3..79f5e20a 100644 --- a/contrast-finder-utils/src/test/java/org/asqatasun/utils/colorconvertor/ColorConverterTest.java +++ b/contrast-finder-utils/src/test/java/org/asqatasun/utils/colorconvertor/ColorConverterTest.java @@ -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