Color.kt

Color.kt is a modern color science library for Kotlin Multiplatform and Java. It includes modern perceptually-uniform color spaces and color appearance models, such as Oklab and ZCAM.

API documentation

Features

  • Perceptually-uniform color spaces, each with LCh (lightness, chroma, hue) representations
  • Color appearance models
  • Hue-preserving gamut mapping
    • Preserve lightness, reduce chroma (default)
    • Project towards neutral 50% gray
    • Adaptive lightness and chroma preservation
  • CIE 1931 XYZ interchange color space
    • Relative luminance
    • Absolute luminance in nits (cd/m²) for HDR color spaces
  • Automatic conversion graph
  • Gamma-correct sRGB encoding and decoding
  • Support for custom color spaces
  • Idiomatic Java API

Usage

Latest version on Maven Central

Add this library as a dependency and replace VERSION with the latest version above:

repositories {
    mavenCentral()
}

dependencies {
    implementation 'dev.kdrag0n:colorkt:VERSION'
}

If you’re using the Kotlin Gradle DSL:

repositories {
    mavenCentral()
}

dependencies {
    implementation("dev.kdrag0n:colorkt:VERSION")
}

Examples

Increase colorfulness with CIELAB

Convert a hex sRGB color code to CIELAB, increase the chroma (colorfulness), and convert it back to sRGB as a hex color code:

<div class="highlight highlight-source-kotlin position-relative" data-snippet-clipboard-copy-content="val cielab = Srgb(" #9b392f").convert()
val lch = cielab.convert()
val boosted1 = lch.copy(C = lch.C * 2).convert().toHex()
“>

val cielab = Srgb("#9b392f").convert<CieLab>()
val lch = cielab.convert<CieLch>()
val boosted1 = lch.copy(C = lch.C * 2).convert<Srgb>().toHex()