zed-industries / zed

Code at the speed of thought – Zed is a high-performance, multiplayer code editor from the creators of Atom and Tree-sitter.
https://zed.dev
Other
39.32k stars 2.04k forks source link

Some colors from themes render slightly differently than defined #9057

Open izuzak opened 3 months ago

izuzak commented 3 months ago

Check for existing issues

Describe the bug / provide steps to reproduce it

I'm working on a theme and setting the syntax.property.color value to #2C78BF. And I see the color of those UI elements change, but the color is slightly different as reported by the color picker -- it says the color is #2D78BF. See first screenshot.

I don't notice this for every color, though -- the color for syntax.string.color is #98BC37 and the color picker reports exactly that value. See second screenshot.

The behavior doesn't seem to be related to which UI/syntax property the color is associated with, but with the color itself. E.g. if I set syntax.string.color to #2C78BF, then it still comes out "wrong" as #2D78BF.

I mentioned this in the Zed discord channel and @maxdeviant said:

I've noticed something similar before. There's a loss of precision somewhere in the mix as colors get converted back and forth between representations that results in some variance.

This was when I noticed the problem originally, during the GPUI 2 rewrite: https://github.com/zed-industries/zed/pull/3547. At the time we were experimenting with importing VS Code themes into Zed, and I noticed that the hex colors weren't round-tripping properly. The fix added in that PR made it so the colors (at least, all the ones I manually checked) were able to round-trip through the theme pipeline and come out the other end in the same form.

We're still using that same approach when parsing hex colors from JSON themes: https://github.com/zed-industries/zed/blob/85e6bc94e980bd91a69ef2ed5cb0372cc4e35667/crates/theme/src/schema.rs#L14-L27. But I'm not sure if there's some other spot where we might be losing precision.

It's a bit of a rabbit hole, because when I was trying to debug it initially I realized that sometimes the colors don't even match up with what the macOS color picker says they are if you're in a different color space 😅

Environment

Zed: v0.126.0 (Zed Preview) OS: macOS 14.3.1 Memory: 64 GiB Architecture: aarch64

If applicable, add mockups / screenshots to help explain present your vision of the feature

Screenshot 1:

Image

Screenshot 2:

Image

If applicable, attach your ~/Library/Logs/Zed/Zed.log file to this issue.

No response

SkyeX commented 3 months ago

This is likely an issue with the color space/management set by the individual system rather than a loss of precision. The ideal solution would be to encode everything in a Linear color space and convert it to the system color space on render.

Not to mention that most color spaces are based on numerical rather than perceptual properties, so there is a lot of variation between devices. Adopting a perceptual color space like OK Lab would help maintain perceived color across implementations.