#12 NumberFormatException when trying to parse colors with alpha

closed
nobody
None
5
2009-05-01
2009-04-24
Andrea Aime
No

Setting, for example, "&chf=bg,s,FFFFFF00" in the servlet call is supposed, by Google API, to return a chart with a transparent background.
Unfortunately the result is a NumberFormatException, because Color.decode() won't work with "0xFFFFFF00".

Changing ChartEngine.parseColor() into:

private static Color parseColor(String text) {
if (text == null) {
throw new IllegalArgumentException(
"Null 'text' argument (in parseColor(String)).");
} else if(text.length() != 6 && text.length() != 8) {
throw new IllegalArgumentException(
"Invalid color representation " + text + ", it should be RRGGBB or RRGGBBAA");
}
// parse RRGGBB format
if(text.length() == 6)
return Color.decode("0x" + text);

// parse RRGGBBAA format
int r = Integer.parseInt(text.substring(0, 2), 16);
int g = Integer.parseInt(text.substring(2, 4), 16);
int b = Integer.parseInt(text.substring(4, 6), 16);
int a = Integer.parseInt(text.substring(6, 8), 16);
return new Color(r,g,b,a);
}

Fixes the issue

Discussion

  • Andrea Aime
    Andrea Aime
    2009-05-01

    • status: open --> closed
     
  • Andrea Aime
    Andrea Aime
    2009-05-01

    Fixed at r73