commit | df2dc2d2acb56dcdd6e162782752e79d81ed5c0b | [log] [tgz] |
---|---|---|
author | Danny Lin <danny@kdrag0n.dev> | Mon Aug 30 01:00:08 2021 -0700 |
committer | Danny Lin <danny@kdrag0n.dev> | Mon Aug 30 01:00:08 2021 -0700 |
tree | 85af515610d9a3e5274ec341acb5804acb740edb | |
parent | ba01bd2097eeab474e76cc6c766411780f512a88 [diff] |
build: Initial Maven publishing setup
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.
Please note that this library has not been released yet. The instructions below will not work until a stable version has been tagged and uploaded to Maven Central.
Add this library as a 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!