Skip to content

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

@anchpop

Description

@anchpop

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-RenderingDrawing game state to the screenC-UsabilityA targeted quality-of-life change that makes Bevy easier to use

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions