Fri, 23 Oct 2020 18:40:50 +0200
migrate WebColor
1.1 --- a/src/main/java/de/uapcore/lightpit/types/WebColor.java Fri Oct 23 18:03:59 2020 +0200 1.2 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 1.3 @@ -1,30 +0,0 @@ 1.4 -package de.uapcore.lightpit.types; 1.5 - 1.6 -public final class WebColor { 1.7 - 1.8 - private final String hex; 1.9 - 1.10 - /** 1.11 - * Constructs a color object from a hex string. 1.12 - * @param hex the 6 digits hex string optionally preceded by a hash symbol 1.13 - * @throws IllegalArgumentException if the given string does not specify a color 1.14 - */ 1.15 - public WebColor(String hex) throws IllegalArgumentException { 1.16 - this.hex = (hex.startsWith("#") ? hex : ("#"+hex)).toUpperCase(); 1.17 - if (!this.hex.matches("#[0-9A-F]{6}")) 1.18 - throw new IllegalArgumentException(hex+" is not a color"); 1.19 - } 1.20 - 1.21 - /** 1.22 - * Returns the hex representation without th leading hash symbol. 1.23 - * @return the hex representation of this color (e.g. FF0000 for red) 1.24 - */ 1.25 - public String getHex() { 1.26 - return hex.substring(1); 1.27 - } 1.28 - 1.29 - @Override 1.30 - public String toString() { 1.31 - return hex; 1.32 - } 1.33 -}
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/src/main/kotlin/de/uapcore/lightpit/types/WebColor.kt Fri Oct 23 18:40:50 2020 +0200 2.3 @@ -0,0 +1,27 @@ 2.4 +package de.uapcore.lightpit.types 2.5 + 2.6 + 2.7 +/** 2.8 + * Represents a web color in hexadezimal representation. 2.9 + * @param arg the 6 digits hex string optionally preceded by a hash symbol 2.10 + */ 2.11 +class WebColor(arg: String) { 2.12 + 2.13 + /** 2.14 + * The color representation with the leading hash symbol. 2.15 + */ 2.16 + val color: String = (if (arg.startsWith("#")) arg else "#$arg").toUpperCase() 2.17 + 2.18 + /** 2.19 + * The hex representation without the leading hash symbol. 2.20 + */ 2.21 + val hex: String = color.substring(1) 2.22 + 2.23 + init { 2.24 + require(this.color.matches(Regex("#[0-9A-F]{6}"))) { "$color is not a color" } 2.25 + } 2.26 + 2.27 + override fun toString(): String { 2.28 + return color 2.29 + } 2.30 +} 2.31 \ No newline at end of file