14 releases

0.16.0 Feb 5, 2024
0.15.0-rc.3 Nov 5, 2024
0.14.1 Jul 4, 2024
0.14.0 Feb 3, 2024

#2456 in Game dev

Download history 8840/week @ 2024-07-29 8037/week @ 2024-08-05 9300/week @ 2024-08-12 7771/week @ 2024-08-19 8380/week @ 2024-08-26 8922/week @ 2024-09-02 8920/week @ 2024-09-09 9737/week @ 2024-09-16 9764/week @ 2024-09-23 9467/week @ 2024-09-30 8936/week @ 2024-10-07 8382/week @ 2024-10-14 7978/week @ 2024-10-21 8878/week @ 2024-10-28 9070/week @ 2024-11-04 6077/week @ 2024-11-11

32,966 downloads per month
Used in 404 crates (34 directly)

MIT/Apache

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

~11MB
~258K SLoC