linebender / color

Color in Rust.
Apache License 2.0
39 stars 5 forks source link

Add Adobe RGB (1998) color space #35

Closed tomcur closed 1 week ago

tomcur commented 2 weeks ago

The Linear A98 RGB <-> Linear sRGB matrices are based on the exact rational conversion matrices calculated from the XYZ chromaticities. This crate doesn't need the exact rationals as they're truncated to f32 at compile-time anyway, but it might be worth having to serve as a reference for other people. It would be nice to have agreement across implementations at any given precision.

The same can be done for other basis change matrices, though it becomes somewhat unwieldy as the number of steps combined into the matrix increases; by my calculation element (0,0) of the Linear sRGB to XYZ-D50 matrix with linear Bradford adaptation is exactly 7206783369626736730083375524119595656 / 16526827482518743502029642682152162875.

This is all based on the assumption that the color spaces' specified xy chromaticities are normative, and the specified matrices in the specs are computed off those.

raphlinus commented 1 week ago

The exact rationals work well here (because the magnitudes are reasonable), but overall I think they're gilding the lily. Doing the calculations in f64 and then truncating to f32 as the absolute last step is what I've generally been doing, and I think is fine.

I'm also slightly curious, what tool are you using? For my matrix multiplications I happen to be using the reference code from the CSS spec, but pretty much anything should work and will give the same results.

tomcur commented 1 week ago

I'm also slightly curious, what tool are you using? For my matrix multiplications I happen to be using the reference code from the CSS spec, but pretty much anything should work and will give the same results.

I've been using these conversions, and some work on Kurbo, as an excuse to learn Sage. It's nice for these conversions as you can specify them algebraically, and if you plug in rationals, the calculations are done over the rationals.