Parse web colors

This commit is contained in:
M66B 2021-07-05 16:35:31 +02:00
parent e4f896fdf9
commit e004228529
1 changed files with 14 additions and 7 deletions

View File

@ -1537,7 +1537,7 @@ public class HtmlHelper {
if (x11ColorMap.containsKey(code)) // workaround
color = x11ColorMap.get(code);
else
color = Long.decode(c).intValue();
color = parseWebColor(code);
}
} else if (c.startsWith("rgb") || c.startsWith("hsl")) {
int s = c.indexOf("(");
@ -1567,12 +1567,7 @@ public class HtmlHelper {
} else if (x11ColorMap.containsKey(c))
color = x11ColorMap.get(c);
else
try {
color = Color.parseColor(c);
} catch (IllegalArgumentException ex) {
// Workaround
color = Long.decode("#" + c).intValue();
}
color = parseWebColor(c);
if (BuildConfig.DEBUG)
Log.i("Color " + c + "=" + (color == null ? null : Long.toHexString(color)));
@ -1584,6 +1579,18 @@ public class HtmlHelper {
return color;
}
private static int parseWebColor(String value) {
if (value.length() == 3 || value.length() == 6 || value.length() == 8) {
if (value.length() == 3)
value = "" +
value.charAt(0) + value.charAt(0) +
value.charAt(1) + value.charAt(1) +
value.charAt(2) + value.charAt(2);
return (int) Long.parseLong(value, 16);
} else
throw new IllegalArgumentException("Unknown color=" + value);
}
private static Integer adjustColor(boolean dark, int textColorPrimary, Integer color) {
int r = Color.red(color);
int g = Color.green(color);