15 releases

0.16.0 Feb 5, 2024
0.15.1 Nov 29, 2024
0.15.0 Feb 5, 2024
0.15.0-rc.2 Oct 27, 2024
0.14.1 Jul 4, 2024

#2466 in Game dev

Download history 8136/week @ 2024-08-17 8043/week @ 2024-08-24 8470/week @ 2024-08-31 9583/week @ 2024-09-07 9227/week @ 2024-09-14 9818/week @ 2024-09-21 9397/week @ 2024-09-28 9532/week @ 2024-10-05 8268/week @ 2024-10-12 8172/week @ 2024-10-19 8291/week @ 2024-10-26 9208/week @ 2024-11-02 7098/week @ 2024-11-09 7311/week @ 2024-11-16 8972/week @ 2024-11-23 16581/week @ 2024-11-30

41,340 downloads per month
Used in 430 crates (38 directly)

MIT/Apache

2MB
37K 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

~12MB
~259K SLoC