dvui icon indicating copy to clipboard operation
dvui copied to clipboard

use proper colorspace for color operations

Open iacore opened this issue 2 years ago • 3 comments

cannot lighten black

0 * (1 + y) == 0

https://github.com/david-vanderson/dvui/blob/811e8d93f9f55c0a16d18d45e61a2db646198b52/src/Color.zig#L28-L35

iacore avatar Nov 20 '23 11:11 iacore

Yep you are right. Do you know what we should be doing here? My guess is that we need some colorspace conversion functions (cielab?) so that lighten/darken use that. Do you know which we should use?

david-vanderson avatar Nov 20 '23 14:11 david-vanderson

CIELUV.

https://www.hsluv.org/ https://www.kuon.ch/post/2020-03-08-hsluv/ https://gist.github.com/denis-bz/ec601823dc52666812a3e27b88772a84

I vaguely remember porting the conversion code between sRGB and HSLuv from d3.js (Javascript) to Rust or C, but I cannot find the code. I can port the code again when I have time.

What rendering API do we even use? OpenGL? We need control over colorspace so the color displayed is accurate.

iacore avatar Nov 20 '23 18:11 iacore

Excellent - thanks for that. As for rendering API, for the SDL backend we use SDL_RenderGeometryRaw and I haven't dug into it to figure out what's going on with colorspaces. For the mach backend (which I will make work again at some point), it uses webgpu with some shaders I wrote, but again I haven't thought about colorspaces yet.

I think a great step would be to have conversion code so that we have nicer darken/lighten functions. Thank you!

david-vanderson avatar Nov 20 '23 19:11 david-vanderson

I have ported ok_color.h to C. -> branch ok_color

This branch can't build because the color space conversion code is used at compile time. Can't call into C code at compile time. Build this branch and you will understand.


I have also attempted to translate it to Zig, but it's difficult so I gave up. -> branch ok_color_zig

I think eventually zig translate-c will get good enough that this becomes automatic.

right now i give up

iacore avatar Jan 23 '24 21:01 iacore

Thanks very much. I really appreciate you taking the time to dig into this. I will pick up where you left off soon and see if I can make progress.

david-vanderson avatar Jan 24 '24 02:01 david-vanderson

One idea is to generate the palette at the start of run-time instead of at compile-time. You can do this in dvui.init().

Edit: I forgot to push the ok_color branch to this repo... I have done it now.

iacore avatar Jan 24 '24 12:01 iacore

I can't #68. Either zig c-translate produced invalid code, or the original algorithm is buggy.

Hopefully dvui won't need Okhsl, ever.

iacore avatar Mar 22 '24 19:03 iacore

I was finally able to hand-convert hsluv.c to zig. The adwaita theme code is specifying colors using it, and the demo window (under styling) has HSLuv sliders.

This is just a start and I'm unsure what the path forward is, but being able to convert to HSLuv and back (HPLuv is also implemented) is a big improvement from where we were.

Thanks for the help here!

david-vanderson avatar Apr 12 '24 01:04 david-vanderson

~~Where did you get hsluv.c? If it's the ok_color branch, that's by me (I ported it from C++ to C).~~

It seems to be from https://github.com/hsluv/hsluv-c/blob/master/src/hsluv.c.

iacore avatar Apr 12 '24 11:04 iacore

That's right. It looked the easiest to port and had test data.

david-vanderson avatar Apr 12 '24 13:04 david-vanderson

Is this issue done now?

iacore avatar May 20 '24 20:05 iacore

I think so. Likely there will be follow-on stuff regarding more colorspace stuff, but this is a good start. Thanks!

david-vanderson avatar May 20 '24 21:05 david-vanderson