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

changeset 127
6105ee2cceaf
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/java/de/uapcore/lightpit/types/WebColor.java	Thu Oct 15 12:27:05 2020 +0200
@@ -0,0 +1,30 @@
+package de.uapcore.lightpit.types;
+
+public final class WebColor {
+
+    private final String hex;
+
+    /**
+     * Constructs a color object from a hex string.
+     * @param hex the 6 digits hex string optionally preceded by a hash symbol
+     * @throws IllegalArgumentException if the given string does not specify a color
+     */
+    public WebColor(String hex) throws IllegalArgumentException {
+        this.hex = (hex.startsWith("#") ? hex : ("#"+hex)).toUpperCase();
+        if (!this.hex.matches("#[0-9A-F]{6}"))
+            throw new IllegalArgumentException(hex+" is not a color");
+    }
+
+    /**
+     * Returns the hex representation without th leading hash symbol.
+     * @return the hex representation of this color (e.g. FF0000 for red)
+     */
+    public String getHex() {
+        return hex.substring(1);
+    }
+
+    @Override
+    public String toString() {
+        return hex;
+    }
+}

mercurial