src/main/kotlin/de/uapcore/lightpit/types/WebColor.kt

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
child 150
822b7e3d064d
permissions
-rw-r--r--

migrate WebColor

universe@149 1 package de.uapcore.lightpit.types
universe@149 2
universe@149 3
universe@149 4 /**
universe@149 5 * Represents a web color in hexadezimal representation.
universe@149 6 * @param arg the 6 digits hex string optionally preceded by a hash symbol
universe@149 7 */
universe@149 8 class WebColor(arg: String) {
universe@149 9
universe@149 10 /**
universe@149 11 * The color representation with the leading hash symbol.
universe@149 12 */
universe@149 13 val color: String = (if (arg.startsWith("#")) arg else "#$arg").toUpperCase()
universe@149 14
universe@149 15 /**
universe@149 16 * The hex representation without the leading hash symbol.
universe@149 17 */
universe@149 18 val hex: String = color.substring(1)
universe@149 19
universe@149 20 init {
universe@149 21 require(this.color.matches(Regex("#[0-9A-F]{6}"))) { "$color is not a color" }
universe@149 22 }
universe@149 23
universe@149 24 override fun toString(): String {
universe@149 25 return color
universe@149 26 }
universe@149 27 }

mercurial