Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature request: Use a fancy color library like Palette. #1797

Closed
anchpop opened this issue Apr 1, 2021 · 3 comments
Closed

Feature request: Use a fancy color library like Palette. #1797

anchpop opened this issue Apr 1, 2021 · 3 comments
Labels
A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use

Comments

@anchpop
Copy link
Contributor

anchpop commented Apr 1, 2021

What problem does this solve or what need does it fill?

I often find myself doing adding materials like materials.add(Color::rgb(0.2, 0.3, 0.3).into()). This seems simple enough... or is it? Color::rgb(0.2, 0.3, 0.3) is actually not an unambiguous description of a color, because it doesn't describe the color space. (I suspect Bevy assumes sRGB for the color space, but I haven't looked into the code to check.) One alternative to sRGB is Linear sRG, and the same "color values" can look substantially different in each:

image

The apparent difference is caused by how our eyes work. Increasing the intensity of a light by 10% doesn't always make it look 10% brighter. sRGB has something called "gamma correction", where they stretch out the darks (where our eyes are more sensitive) and compress the lights.

Many people are familiar with this because they've played with the "linear color" setting in their game engine. Doing lighting calculations on gamma-corrected values leads to physically inaccurate results:

image

So when I write Color::rgb(0.2, 0.3, 0.3), it's genuinely unclear to me what that represents (the docs don't say either). And ambiguities like that lead to extremely common bugs by people who should know better (MS Paint, Photoshop, Firefox) when people do calculations like blending gamma-corrected values.

What solution would you like?

There are some libraries designed to make it easy to work with colors, and a good one is palette. Using the structs provided by palette, you would write Srgb::new(0.2, 0.3, 0.3). It also comes with many useful impls on those structs. From the docs:

use palette::{FromColor, Saturate, Shade, Srgb, Lch};

let color = Srgb::new(0.8, 0.2, 0.1).into_linear();
let lighter = color.lighten(0.1);
let desaturated = Lch::from_color(color).desaturate(0.5);
@alice-i-cecile alice-i-cecile added A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Apr 1, 2021
@alice-i-cecile
Copy link
Member

See also #1146, #1402.

@rparrett
Copy link
Contributor

rparrett commented Apr 1, 2021

Also, bevy 0.5 (via #1572) will have

Color::Rgba
Color::RgbaLinear
Color::Hsla

@mockersf
Copy link
Member

closing for #1146

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Rendering Drawing game state to the screen C-Usability A targeted quality-of-life change that makes Bevy easier to use
Projects
None yet
Development

No branches or pull requests

4 participants