9 releases

0.16.0 Feb 5, 2024
0.15.0 Feb 5, 2024
0.14.1 Jul 4, 2024
0.14.0 Feb 3, 2024
0.12.3 Feb 7, 2024

#2297 in Game dev

Download history 22/week @ 2024-04-04 4/week @ 2024-04-25 9/week @ 2024-05-02 8/week @ 2024-05-09 3/week @ 2024-05-23 3/week @ 2024-05-30 1050/week @ 2024-06-06 954/week @ 2024-06-13 826/week @ 2024-06-20 1515/week @ 2024-06-27 8767/week @ 2024-07-04 6800/week @ 2024-07-11 7192/week @ 2024-07-18

24,426 downloads per month
Used in 278 crates (26 directly)

MIT/Apache

1.5MB
27K SLoC

Bevy Color

License Crates.io Downloads Docs Discord


lib.rs:

Representations of colors in various color spaces.

This crate provides a number of color representations, including:

  • Srgba (standard RGBA, with gamma correction)
  • LinearRgba (linear RGBA, without gamma correction)
  • Hsla (hue, saturation, lightness, alpha)
  • Hsva (hue, saturation, value, alpha)
  • Hwba (hue, whiteness, blackness, alpha)
  • Laba (lightness, a-axis, b-axis, alpha)
  • Lcha (lightness, chroma, hue, alpha)
  • Oklaba (lightness, a-axis, b-axis, alpha)
  • Oklcha (lightness, chroma, hue, alpha)
  • Xyza (x-axis, y-axis, z-axis, alpha)

Each of these color spaces is represented as a distinct Rust type.

Color Space Usage

Rendering engines typically use linear RGBA colors, which allow for physically accurate lighting calculations. However, linear RGBA colors are not perceptually uniform, because both human eyes and computer monitors have non-linear responses to light. "Standard" RGBA represents an industry-wide compromise designed to encode colors in a way that looks good to humans in as few bits as possible, but it is not suitable for lighting calculations.

Most image file formats and scene graph formats use standard RGBA, because graphic design tools are intended to be used by humans. However, 3D lighting calculations operate in linear RGBA, so it is important to convert standard colors to linear before sending them to the GPU. Most Bevy APIs will handle this conversion automatically, but if you are writing a custom shader, you will need to do this conversion yourself.

HSL and LCH are "cylindrical" color spaces, which means they represent colors as a combination of hue, saturation, and lightness (or chroma). These color spaces are useful for working with colors in an artistic way - for example, when creating gradients or color palettes. A gradient in HSL space from red to violet will produce a rainbow. The LCH color space is more perceptually accurate than HSL, but is less intuitive to work with.

HSV and HWB are very closely related to HSL in their derivation, having identical definitions for hue. Where HSL uses saturation and lightness, HSV uses a slightly modified definition of saturation, and an analog of lightness in the form of value. In contrast, HWB instead uses whiteness and blackness parameters, which can be used to lighten and darken a particular hue respectively.

Oklab and Oklch are perceptually uniform color spaces that are designed to be used for tasks such as image processing. They are not as widely used as the other color spaces, but are useful for tasks such as color correction and image analysis, where it is important to be able to do things like change color saturation without causing hue shifts.

XYZ is a foundational space commonly used in the definition of other more modern color spaces. The space is more formally known as CIE 1931, where the x and z axes represent a form of chromaticity, while y defines an illuminance level.

See also the Wikipedia article on color spaces.

Other Utilities

The crate also provides a number of color operations, such as blending, color difference, and color range operations.

In addition, there is a Color enum that can represent any of the color types in this crate. This is useful when you need to store a color in a data structure that can't be generic over the color type.

Color types that are either physically or perceptually linear also implement Add<Self>, Sub<Self>, Mul<f32> and Div<f32> allowing you to use them with splines.

Please note that most often adding or subtracting colors is not what you may want. Please have a look at other operations like blending, lightening or mixing colors using e.g. [Mix] or Luminance instead.

Example

use bevy_color::{Srgba, Hsla};

let srgba = Srgba::new(0.5, 0.2, 0.8, 1.0);
let hsla: Hsla = srgba.into();

println!("Srgba: {:?}", srgba);
println!("Hsla: {:?}", hsla);

Dependencies

~10MB
~223K SLoC