treeform / chroma

Everything you want to do with colors, in Nim.
MIT License
106 stars 12 forks source link

Mapping to OKLab and back significantly changes the color. #41

Open gcr opened 2 months ago

gcr commented 2 months ago

hii!

i'm struggling to use this library for some image processing and palette mapping tasks. I've found that transforming to oklab and back significantly brightens and desaturates colors.

import chroma
echo parseHex("F8DB57").to(ColorOklab).to(ColorRGB).color.toHex()
# output:      FCF4BA

Color #F8DB57 turns into #FCF4BA.

Here's some assorted roundtrips:

import chroma
import random
import strformat

proc chip(c: ColorRGB): string =
    ## nice-looking color chip for github-flavored markdown
    let hex = c.color.toHex()
    fmt"`#{hex}` <img valign='middle' alt='blue' src='https://readme-swatches.vercel.app/{hex}'/>"

randomize()
for _ in 1..10:
    let color = ColorRGB(
        r: random.rand(255).uint8,
        g: random.rand(255).uint8,
        b: random.rand(255).uint8,
    )
    let color2 = color.to(ColorOklab).to(ColorRGB)
    echo fmt"{color.chip} -> {color2.chip}"

Output:

#FF1580 blue -> #1A89CC blue #B543B5 blue -> #E9A8E6 blue #DBCA21 blue -> #F1EF99 blue #28B837 blue -> #9CE9A0 blue #E254E2 blue -> #FBB5F8 blue #5103BC blue -> #B06BF8 blue #3F51B2 blue -> #A0B0E7 blue #DB26C5 blue -> #0196EF blue #B44D3E blue -> #EAAEA1 blue #803A22 blue -> #D09E86 blue

Each pair of colors should be identical (before converting to OkLab and after). But for many of these pairs, even the hue is wildly off.

gcr commented 2 months ago

Nevermind - this is fixed at HEAD. I resolved this in my project temporarily by cloning this repository and running nimble install from the root folder.

Once #42 is fixed, this will be too.

Roundtrips at HEAD look good:

#42B5DE blue -> #42B5DE blue #F85C71 blue -> #F85C71 blue #249550 blue -> #249550 blue #B75EE3 blue -> #B75EE3 blue #FBE114 blue -> #FBE114 blue #1D31AC blue -> #1D31AC blue #707F97 blue -> #707F97 blue #B05B48 blue -> #B05B48 blue #97BD88 blue -> #97BD88 blue #6C5BD9 blue -> #6C5BD9 blue