Need help with colormath?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

ajalt
167 Stars 6 Forks MIT License 318 Commits 0 Opened issues

Description

Multiplatform Kotlin color conversion and manipulation

Services available

!
?

Need anything else?

Contributors list

# 25,655
Kotlin
uber
fingerp...
elm
315 commits
# 32,851
C++
C#
control...
Shell
1 commit

Colormath

Colormath is a Kotlin Multiplatform library for color manipulation and conversion.

Colormath can:

  • Convert between color models and spaces
  • Manipulate colors with transformations such as mixing and chromatic adaptation
  • Calculate attributes such as WCAG contrast and perceptual color difference
  • Generate gradients with custom interpolation methods and easing functions
  • Parse and render colors as strings, including all representations from the CSS spec
// Create an sRGB color
val color = RGB("#ff23cc")

// Interpolate with another color val mixed = color.interpolate(RGB(0.1, 0.4, 1), 0.5f) // RGB("#8c45e6")

// Convert to a different color space val lab = mixed.toLAB() // LAB(46.3, 60.9, -70)

// Change the transparency val labA = lab.copy(alpha = 0.25f) // LAB(46.3, 60.9, -70, 0.25)

// Adapt white point val lab50 = labA.convertTo(LAB50) // LAB50(45, 55.1812, 72.5911, 0.25)

// Render as a css color string println(lab50.formatCssString()) // "lab(45% 55.1812 -72.5911 / 0.25)"

Documentation

The full documentation can be found on the website.

There are also some online examples:

Installation

Colormath is distributed through Maven Central.

dependencies {
    implementation("com.github.ajalt.colormath:colormath:3.1.1")

// optional extensions for interop with other platforms
//
// android.graphics.Color
implementation("com.github.ajalt.colormath.extensions:colormath-ext-android-color:3.1.1")
// androidx.annotation.ColorInt
implementation("com.github.ajalt.colormath.extensions:colormath-ext-android-colorint:3.1.1")
// androidx.compose.ui.graphics.Color
implementation("com.github.ajalt.colormath.extensions:colormath-ext-jetpack-compose:3.1.1")

}

If you're using Maven instead of Gradle, use
colormath-jvm

Multiplatform

Colormath supports the following targets:

jvm
,
mingwX64
,
linuxX64
,
macosX64
,
ios
,
watchos
,
tvos
, and
js
(for both NodeJS and Browsers). You'll need to use Gradle 6 or newer. Artifacts for
macosArm64
are also published, but not tested with CI.

Snapshots

Snapshot builds are also available

You'll need to add the Sonatype snapshots repository:

repositories {
    maven {
        url = uri("https://oss.sonatype.org/content/repositories/snapshots/")
    }
}

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.