commit | 5c505533ad6a5638518c7dc020729844444cf6f2 | [log] [tgz] |
---|---|---|
author | Danny Lin <danny@kdrag0n.dev> | Sat Aug 28 04:13:14 2021 -0700 |
committer | Danny Lin <danny@kdrag0n.dev> | Sat Aug 28 04:13:14 2021 -0700 |
tree | fa215eb0652655ee768eacc9fc16c1e9a3e3e7e4 | |
parent | b32cee6dd9e74bc0e4f48f15f8353d5f47e4a002 [diff] |
readme: Add full readme for release
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.
Add the dependency:
repositories { mavenCentral() } dependencies { implementation 'dev.kdrag0n:colorkt:1.0.0' }
If you're using the Kotlin Gradle DSL:
repositories { mavenCentral() } dependencies { implementation("dev.kdrag0n:colorkt:1.0.0") }
Convert a hex sRGB color code to CIELAB, increase the chroma (colorfulness), and convert it back to sRGB as a hex color code:
val cielab = Srgb("#9b392f").convert<CieLab>() val lch = cielab.convert<CieLch>() val boosted1 = lch.copy(C = lch.C * 2).convert<Srgb>().toHex()
Do the same thing in Oklab:
val oklab = Srgb("#9b392f").convert<Oklab>() val lch = oklab.convert<Oklch>() val boosted2 = lch.copy(C = lch.C * 2).convert<Srgb>().toHex()
Model a color using ZCAM:
// Brightness of the display val luminance = 200.0 // nits (cd/m²) // Conditions under which the color will be viewed val cond = Zcam.ViewingConditions( surroundFactor = Zcam.ViewingConditions.SURROUND_AVERAGE, adaptingLuminance = 0.4 * luminance, // Mid-gray background at 50% luminance backgroundLuminance = CieLab(50.0, 0.0, 0.0).toXyz().y * luminance, // D65 is the only supported white point referenceWhite = Illuminants.D65.toAbs(luminance), ) // Color to convert val src = Srgb("#533b69") // Use ZCAM to get perceptual color appearance attributes val zcam = src.toLinear().toXyz().toAbs(luminance).toZcam(cond)
Increase the chroma (colorfulness):
val colorful = zcam.copy(chroma = zcam.chroma * 2)
Convert the color back to sRGB, while preserving hue and avoiding ugly results caused by hard clipping:
val srgb = colorful.clipToLinearSrgb()
Finally, print the new hex color code:
println(srgb.toHex())
Color.kt makes it easy to convert between any two color spaces by automatically finding the shortest path in the color conversion graph. This simplifies long conversions, which can occur frequently when working with different color spaces. For example, converting from CIELCh to Oklab LCh is usually done like this:
val oklab = cielch.toCieLab().toXyz().toLinearSrgb().toOklab().toOklch()
With automatic conversion:
val oklab = cielch.convert<Oklch>()
However, keep in mind that there is a performance cost associated with automatic conversion because it requires searching the graph. Conversion paths are cached after the first use, but it is still less efficient than manual conversion; prefer manual, explicit conversions in performance-critical code.
Color.kt includes several color spaces that should cover most use cases, but you can also add your own if necessary. Simply create a class that implements the Color interface, and implement some conversions to make the color space useful:
data class GrayColor(val brightness: Double) : Color { fun toLinearSrgb() = LinearSrgb(brightness, brightness, brightness) companion object { // sRGB luminosity function from https://en.wikipedia.org/wiki/Relative_luminance fun LinearSrgb.toGray() = GrayColor(0.2126 * r + 0.7251 * g + 0.0722 * b) } }
Optionally, add your new color space to the automatic conversion graph for convenient usage:
ConversionGraph.add<LinearSrgb, GrayColor> { it.toGray() } ConversionGraph.add<GrayColor, LinearSrgb> { it.toLinearSrgb() }
If you implement a new color space this way, please consider contributing it with a pull request so that everyone can benefit from it!