Skip to content
/ HCT Public

HCT (Kyant) color space and dynamic theme library

License

Notifications You must be signed in to change notification settings

Kyant0/HCT

Repository files navigation

HCT (Kyant)

Another yet high performance HCT (Kyant) color space and dynamic color scheme library for Android Jetpack Compose.

Compared to the Google's HCT, the HCT (Kyant) color space has the following advantages:

  • It supports the P3 color space
  • It doesn't have the blue-purple shift issue
  • It's faster to compute one color (about 0.02ms in the worst case)
  • It's more numerically accurate

Usages

Prerequisites

  • Add the dependency

JitPack Release

allprojects {
    repositories {
        maven("https://jitpack.io")
    }
}

implementation("com.github.Kyant0:HCT:<version>")
  • Initialize the library
System.loadLibrary("com.kyant.hct")
Hct.init(useP3 = true)

HCT manipulation

  • Create a raw HCT
val hct = Hct(hue = 240.0, chroma = 20.0, tone = 40.0)

Note: chroma < 28.0 for every color in P3 color space

  • Convert HCT to Color
hct.toColor()

Dynamic schemes

  • Create a DynamicScheme
val isDark = isSystemInDarkTheme()
val scheme = remember(isDark) {
    DynamicScheme(
        sourceColorHct = hct,
        style = SchemeStyle.TonalSpot,
        isDark = isDark
    )
}

Note: Predefined scheme styles may not be the same as the Google's due to the different chroma scale

  • Use color roles
scheme.primary
scheme.primaryContainer
// ... and more

Note: The library doesn't provide the deprecated colors roles, for example background, please use surfaceContainerXXX instead

  • Create a custom SchemeStyle
val SchemeYou = object : SchemeStyle {
    override val primaryPaletteStyle = TonalPaletteStyle(chroma = { 15.0 })
    override val secondaryPaletteStyle = TonalPaletteStyle({ 5.0 })
    override val tertiaryPaletteStyle = TonalPaletteStyle({ 10.0 }, hue = { it.hue   120.0 })
    override val neutralPaletteStyle = TonalPaletteStyle({ 0.0 })
    override val neutralVariantPaletteStyle = TonalPaletteStyle({ 2.0 })
    override val errorPaletteStyle = TonalPaletteStyle({ 28.0 }, { 20.0 })
}

References

About

HCT (Kyant) color space and dynamic theme library

Resources

License

Stars

Watchers

Forks

Releases

No releases published