X-Git-Url: http://gitweb.fperrin.net/?p=GpsPrune.git;a=blobdiff_plain;f=src%2Ftim%2Fprune%2Fconfig%2FColourUtils.java;fp=src%2Ftim%2Fprune%2Fconfig%2FColourUtils.java;h=a3c11880dfe69b53857b536345eb5cbf44c72f88;hp=0000000000000000000000000000000000000000;hb=ce6f2161b8596f7018d6a76bff79bc9e571f35fd;hpb=2d8cb72e84d5cc1089ce77baf1e34ea3ea2f8465 diff --git a/src/tim/prune/config/ColourUtils.java b/src/tim/prune/config/ColourUtils.java new file mode 100644 index 0000000..a3c1188 --- /dev/null +++ b/src/tim/prune/config/ColourUtils.java @@ -0,0 +1,65 @@ +package tim.prune.config; + +import java.awt.Color; + +/** + * Class to hold static methods for handling colours + * including converting to and from hex code Strings + */ +public abstract class ColourUtils +{ + /** + * Convert a string into a Color object + * @param inValue 6-character hex code + * @return corresponding colour + */ + public static Color colourFromHex(String inValue) + { + Color retVal = null; + if (inValue != null && inValue.length() == 6) + { + try + { + final int redness = convertToInt(inValue.substring(0, 2)); + final int greenness = convertToInt(inValue.substring(2, 4)); + final int blueness = convertToInt(inValue.substring(4, 6)); + retVal = new Color(redness, greenness, blueness); + } + catch (NumberFormatException nfe) {} // colour stays null + } + return retVal; + } + + /** + * @param inPair two-digit String representing hex code + * @return corresponding integer (0 to 255) + */ + private static int convertToInt(String inPair) + { + int val = Integer.parseInt(inPair, 16); + if (val < 0) val = 0; + return val; + } + + /** + * Make a hex code string for the given colour + * @param inColour colour + * @return 6-character hex code + */ + public static String makeHexCode(Color inColour) + { + return convertToHex(inColour.getRed()) + convertToHex(inColour.getGreen()) + convertToHex(inColour.getBlue()); + } + + /** + * @param inValue integer value from 0 to 255 + * @return two-character hex code + */ + private static String convertToHex(int inValue) + { + // Uses lower case a-f + String code = Integer.toHexString(inValue); + // Pad with leading 0 if necessary + return (inValue < 16 ? "0" + code : code); + } +}