Color

sealed interface Color

A resolved color value.

Variants preserve the source form an author wrote so re-serialization round-trips back to the same JSON shape (e.g. "red" parsed via the named-color table becomes Hex, whose canonical encoding is #rrggbb; an explicit "rgba(...)" stays an Rgba). All variants expose 0..255 channel ints so consumers (renderers, converters) never need to re-parse.

Supported source forms at decode time: #rgb, #rgba, #rrggbb, #rrggbbaa, rgb(r, g, b), rgba(r, g, b, a), and the 140 CSS Color Module 3 names + transparent. Anything else (e.g. hsl(...)) throws a SerializationException.

Inheritors

Types

Link copied to clipboard
data class ColorFunction(val space: Color.ColorFunction.Space, val c1: Double, val c2: Double, val c3: Double, val alphaFraction: Double = 1.0) : Color

color(<space> c1 c2 c3 / α) for the sRGB-family color spaces. Channels are 0..1 floats; out-of-gamut values are clipped after any required gamma encoding.

Link copied to clipboard
object Companion
Link copied to clipboard
data class Hex(val red: Int, val green: Int, val blue: Int, val alpha: Int = 255) : Color

A hex color (#rgb, #rgba, #rrggbb, #rrggbbaa); also the resolved form of a named color. Canonical re-encoding is #rrggbb (or #rrggbbaa when alpha< 255).

Link copied to clipboard
data class Hsl(val hue: Double, val saturation: Double, val lightness: Double, val alphaFraction: Double = 1.0) : Color

hsl(...) / hsla(...). Saturation and lightness are CSS percentages (0..100), hue is degrees (any double; normalized modulo 360 during conversion).

Link copied to clipboard
data class Lab(val lightness: Double, val a: Double, val b: Double, val alphaFraction: Double = 1.0) : Color

lab(L a b) / lab(L a b / α) in the CIE Lab* color space (D50 reference white), converted to sRGB via the CSS Color 4 reference algorithm. Out-of-gamut channels are clipped to 0..255.

Link copied to clipboard
data class Oklch(val lightness: Double, val chroma: Double, val hue: Double, val alphaFraction: Double = 1.0) : Color

oklch(L C H) / oklch(L C H / α). Lightness is 0..1, chroma is unbounded non-negative (typical max ~0.4), hue is degrees. Out-of-gamut channels are clipped.

Link copied to clipboard
data class Rgb(val red: Int, val green: Int, val blue: Int) : Color

An opaque rgb(r, g, b) color. alpha is always 255.

Link copied to clipboard
data class Rgba(val red: Int, val green: Int, val blue: Int, val alphaFraction: Double) : Color

An rgba(r, g, b, a) color. The fractional alpha is preserved in alphaFraction; alpha derives it as 0..255.

Properties

Link copied to clipboard
abstract val alpha: Int

Alpha channel, 0..255 (255 = opaque).

Link copied to clipboard
abstract val blue: Int

Blue channel, 0..255.

Link copied to clipboard
abstract val green: Int

Green channel, 0..255.

Link copied to clipboard
abstract val red: Int

Red channel, 0..255.

Functions

Link copied to clipboard
open fun toCssString(): String

Canonical CSS string form for this color (e.g. #ff8800, rgba(255, 0, 0, 0.5)).