src/main/java/de/uapcore/lightpit/types/WebColor.java

changeset 127
6105ee2cceaf
equal deleted inserted replaced
126:4148e6de0f21 127:6105ee2cceaf
1 package de.uapcore.lightpit.types;
2
3 public final class WebColor {
4
5 private final String hex;
6
7 /**
8 * Constructs a color object from a hex string.
9 * @param hex the 6 digits hex string optionally preceded by a hash symbol
10 * @throws IllegalArgumentException if the given string does not specify a color
11 */
12 public WebColor(String hex) throws IllegalArgumentException {
13 this.hex = (hex.startsWith("#") ? hex : ("#"+hex)).toUpperCase();
14 if (!this.hex.matches("#[0-9A-F]{6}"))
15 throw new IllegalArgumentException(hex+" is not a color");
16 }
17
18 /**
19 * Returns the hex representation without th leading hash symbol.
20 * @return the hex representation of this color (e.g. FF0000 for red)
21 */
22 public String getHex() {
23 return hex.substring(1);
24 }
25
26 @Override
27 public String toString() {
28 return hex;
29 }
30 }

mercurial