migrate WebColor

Fri, 23 Oct 2020 18:40:50 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 23 Oct 2020 18:40:50 +0200
changeset 149
30b840ed8c0e
parent 148
87364e7ec333
child 150
822b7e3d064d

migrate WebColor

src/main/java/de/uapcore/lightpit/types/WebColor.java file | annotate | diff | comparison | revisions
src/main/kotlin/de/uapcore/lightpit/types/WebColor.kt file | annotate | diff | comparison | revisions
--- a/src/main/java/de/uapcore/lightpit/types/WebColor.java	Fri Oct 23 18:03:59 2020 +0200
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,30 +0,0 @@
-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;
-    }
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/main/kotlin/de/uapcore/lightpit/types/WebColor.kt	Fri Oct 23 18:40:50 2020 +0200
@@ -0,0 +1,27 @@
+package de.uapcore.lightpit.types
+
+
+/**
+ * Represents a web color in hexadezimal representation.
+ * @param arg the 6 digits hex string optionally preceded by a hash symbol
+ */
+class WebColor(arg: String) {
+
+    /**
+     * The color representation with the leading hash symbol.
+     */
+    val color: String = (if (arg.startsWith("#")) arg else "#$arg").toUpperCase()
+
+    /**
+     * The hex representation without the leading hash symbol.
+     */
+    val hex: String = color.substring(1)
+
+    init {
+        require(this.color.matches(Regex("#[0-9A-F]{6}"))) { "$color is not a color" }
+    }
+
+    override fun toString(): String {
+        return color
+    }
+}
\ No newline at end of file

mercurial