1 package org.jmage.util;
2
3 /***
4 * Utility class for color conversions
5 */
6 public class ColorUtil {
7 private static final String HEX = "0x";
8 private static final String RGBSTRING_ERROR = "not a valid RRGGBB string: ";
9 private static final String INVALID_COLOR = "not a proper color value: ";
10
11 /***
12 * Converts an RRGGBB style string into an int array
13 *
14 * @param color string
15 * @return an int array containing the rgb values.
16 */
17 public static int[] decodeRGBString(String color) throws NumberFormatException, AssertionError {
18 assert(6 == color.length()) : RGBSTRING_ERROR + color;
19 int[] rgb = new int[3];
20 rgb[0] = Integer.decode(HEX + color.substring(0, 2)).intValue();
21 assert (rgb[0] >= 0 && rgb[0] <= 255) : INVALID_COLOR + rgb[0];
22 rgb[1] = Integer.decode(HEX + color.substring(2, 4)).intValue();
23 assert (rgb[1] >= 0 && rgb[1] <= 255) : INVALID_COLOR + rgb[1];
24 rgb[2] = Integer.decode(HEX + color.substring(4)).intValue();
25 assert (rgb[2] >= 0 && rgb[2] <= 255) : INVALID_COLOR + rgb[2];
26
27 return rgb;
28 }
29 }