Skip to content

Commit 296693a

Browse files
author
Denys Zaiats
committed
[responsive-validator] - added converter of HEX to rgb(a)
1 parent 9166303 commit 296693a

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

src/main/java/util/general/SystemHelper.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public static boolean isRetinaDisplay(Graphics2D g) {
1313
if (field != null) {
1414
field.setAccessible(true);
1515
Object scale = field.get(graphicsDevice);
16-
if(scale instanceof Integer && ((Integer) scale).intValue() == 2) {
16+
if (scale instanceof Integer && ((Integer) scale).intValue() == 2) {
1717
isRetina = true;
1818
}
1919
}
@@ -22,4 +22,31 @@ public static boolean isRetinaDisplay(Graphics2D g) {
2222
}
2323
return isRetina;
2424
}
25+
26+
public static String hexStringToARGB(String hexARGB) throws IllegalArgumentException {
27+
28+
if (!hexARGB.startsWith("#") || !(hexARGB.length() == 7 || hexARGB.length() == 9)) {
29+
30+
throw new IllegalArgumentException("Hex color string is incorrect!");
31+
}
32+
33+
int[] intARGB;
34+
35+
if (hexARGB.length() == 9) {
36+
intARGB = new int[4];
37+
intARGB[0] = Integer.valueOf(hexARGB.substring(1, 3), 16); // alpha
38+
intARGB[1] = Integer.valueOf(hexARGB.substring(3, 5), 16); // red
39+
intARGB[2] = Integer.valueOf(hexARGB.substring(5, 7), 16); // green
40+
intARGB[3] = Integer.valueOf(hexARGB.substring(7), 16); // blue
41+
42+
return String.format("rgba(%d,%d,%d,%.1f)", intARGB[1], intARGB[2], intARGB[3], (float)Math.round((((float)intARGB[0]/255) * 100)) / 100.f);
43+
} else {
44+
intARGB = new int[3];
45+
intARGB[0] = Integer.valueOf(hexARGB.substring(1, 3), 16); // red
46+
intARGB[1] = Integer.valueOf(hexARGB.substring(3, 5), 16); // green
47+
intARGB[2] = Integer.valueOf(hexARGB.substring(5), 16); // blue
48+
49+
return String.format("rgb(%d,%d,%d)", intARGB[0], intARGB[1], intARGB[2]);
50+
}
51+
}
2552
}

src/main/java/util/validator/ResponsiveUIValidator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ public ResponsiveUIValidator withCssValue(String cssProperty, String... args) {
319319

320320
if (!cssValue.equals("")) {
321321
for (String val : args) {
322+
val = !val.startsWith("#") ? val : SystemHelper.hexStringToARGB(val);
322323
if (!TextFinder.textIsFound(val, cssValue)) {
323324
putJsonDetailsWithoutElement(String.format("Expected value of '%s' is '%s'. Actual is '%s'", cssProperty, val, cssValue));
324325
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import org.junit.Assert;
2+
import org.junit.Test;
3+
import util.general.SystemHelper;
4+
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
8+
public class SystemHelperTest {
9+
10+
@Test
11+
public void testThatColorCanBeConvertedFromHexToRgb() {
12+
Map<String, String> colors = new HashMap<>();
13+
colors.put("rgb(0,0,0)", "#000000");
14+
colors.put("rgb(255,255,255)", "#FFFFFF");
15+
colors.put("rgba(255,255,255,1.0)", "#FFFFFFFF");
16+
colors.put("rgba(255,255,255,0.0)", "#00FFFFFF");
17+
colors.put("rgba(255,255,255,0.2)", "#33FFFFFF");
18+
colors.put("rgba(255,255,255,0.5)", "#80FFFFFF");
19+
colors.put("rgb(251,220,220)", "#fbdcdc");
20+
21+
for (Map.Entry<String, String> entry : colors.entrySet()) {
22+
Assert.assertEquals(entry.getKey(), SystemHelper.hexStringToARGB(entry.getValue()));
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)