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

#2486 in Game dev

Download history 8192/week @ 2024-08-28 9406/week @ 2024-09-04 8729/week @ 2024-09-11 10016/week @ 2024-09-18 9254/week @ 2024-09-25 10296/week @ 2024-10-02 8494/week @ 2024-10-09 7877/week @ 2024-10-16 8425/week @ 2024-10-23 8946/week @ 2024-10-30 7899/week @ 2024-11-06 7450/week @ 2024-11-13 7843/week @ 2024-11-20 14532/week @ 2024-11-27 12791/week @ 2024-12-04 9864/week @ 2024-12-11

46,275 downloads per month
Used in 442 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

~9–11MB
~257K SLoC