1 package tim.prune.config;
6 * Class to hold static methods for handling colours
7 * including converting to and from hex code Strings
9 public abstract class ColourUtils
12 * Convert a string into a Color object
13 * @param inValue 6-character hex code
14 * @return corresponding colour
16 public static Color colourFromHex(String inValue)
19 if (inValue != null && inValue.length() == 6)
23 final int redness = convertToInt(inValue.substring(0, 2));
24 final int greenness = convertToInt(inValue.substring(2, 4));
25 final int blueness = convertToInt(inValue.substring(4, 6));
26 retVal = new Color(redness, greenness, blueness);
28 catch (NumberFormatException nfe) {} // colour stays null
34 * @param inPair two-digit String representing hex code
35 * @return corresponding integer (0 to 255)
37 private static int convertToInt(String inPair)
39 int val = Integer.parseInt(inPair, 16);
45 * Make a hex code string for the given colour
46 * @param inColour colour
47 * @return 6-character hex code
49 public static String makeHexCode(Color inColour)
51 return convertToHex(inColour.getRed()) + convertToHex(inColour.getGreen()) + convertToHex(inColour.getBlue());
55 * @param inValue integer value from 0 to 255
56 * @return two-character hex code
58 private static String convertToHex(int inValue)
60 // Uses lower case a-f
61 String code = Integer.toHexString(inValue);
62 // Pad with leading 0 if necessary
63 return (inValue < 16 ? "0" + code : code);