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

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
    }
}

mercurial