File: ColourUtils.java

package info (click to toggle)
gpsprune 17-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,984 kB
  • ctags: 5,218
  • sloc: java: 39,403; sh: 25; makefile: 17; python: 15
file content (65 lines) | stat: -rw-r--r-- 1,640 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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);
	}
}