raea/color

Bevy's colour types: Srgba, LinearRgba, Hsla, Oklab, Oklch and the rest, with their conversions.

View as Markdown

These are Bevy's colour types with Bevy's maths, so a colour mixed, lightened or converted here comes out as it would in Bevy. Anywhere a prop takes a colour, it also takes a CSS colour string.

TS
import { LinearRgba, Srgba, kelvin } from "raea/color";

const brand = Srgba.hex("#e8b04a");
const hover = brand.lighter(0.1);
const glow = new LinearRgba(4, 2.5, 1, 1); // brighter than white, for bloom
const warm = kelvin(3200);
TS
import { Hsla, kelvin, LinearRgba, Okhsla } from "raea/color"

raea/color exports 2 functions, 7 classes. Bevy's counterpart: bevy_color. Source: sdk/raea/src/color.ts.

Functions

kelvin

A colour temperature as a light's colour (Tanner Helland's fit to the Planckian locus), sRGB hex.

TS
function kelvin(k: number): string

toHex

A colour as the 3D renderer takes it (#rrggbb): hex, rgb()/rgba() or a common name; CSS variables throw.

TS
function toHex(owner: string, k: string, v: unknown): string

Classes

Hsla

Bevy's Hsla: hue (degrees), saturation and lightness, each 0..1; converted through Hsva and Hwba, as Bevy's is.

Bevy: bevy_color::hsla::Hsla.

MemberWhat it does
new Hsla(hue: number, saturation: number, lightness: number, alpha?: number)Hue in degrees (0 to 360), saturation, lightness and alpha 0 to 1.
Hsla.from(c: Rgb): HslaThe same colour from an Srgba or LinearRgba.
.hue: numberBevy: The hue channel. [0.0, 360.0]
.saturation: numberBevy: The saturation channel. [0.0, 1.0]
.lightness: numberBevy: The lightness channel. [0.0, 1.0]
.alpha: numberBevy: The alpha channel. [0.0, 1.0]
.to_srgba(): SrgbaThe same colour as an Srgba.
.to_linear(): LinearRgbaThe same colour as a LinearRgba.
.mix(o: Hsla, t: number): HslaThe colour t (0 to 1) of the way to o, mixed in this space.

Used in: ui/scroll_and_overflow/drag_to_scroll, ui/ui_material, ui/widgets/feathers_gallery, animation/color_animation, 2d/2d_shapes, shader_advanced/custom_shader_instancing.

LinearRgba

Bevy's LinearRgba: linear light, which a StandardMaterial's emissive is; a channel past 1 is brighter than white.

Bevy: bevy_color::linear_rgba::LinearRgba.

MemberWhat it does
new LinearRgba(red: number, green: number, blue: number, alpha?: number)Linear red, green, blue (above 1 for brighter than white) and alpha.
LinearRgba.rgb(red: number, green: number, blue: number): LinearRgbaAn opaque linear colour.
LinearRgba.from(c: Srgba): LinearRgbaBevy's LinearRgba::from(Srgba)
.red: numberBevy: The red channel. [0.0, 1.0] for SDR colors.
.green: numberBevy: The green channel. [0.0, 1.0] for SDR colors.
.blue: numberBevy: The blue channel. [0.0, 1.0] for SDR colors.
.alpha: numberBevy: The alpha channel. [0.0, 1.0]
.to_srgba(): SrgbaBevy's Srgba::from(LinearRgba): gamma-encoded (a channel at or below 0 kept as it is)
.luminance(): numberBevy's Luminance::luminance: Rec. 709's weights over the linear channels
.darker(amount: number): LinearRgbaBevy's Luminance::darker: mixed toward black until its luminance is amount lower (at least 0)
.lighter(amount: number): LinearRgbaBevy's Luminance::lighter: mixed toward white until its luminance is amount higher (at most 1)
.mix(o: LinearRgba, t: number): LinearRgbaBevy's Mix: each channel lerped, in f32
.mul(k: number): LinearRgbaBevy's * k: each colour channel scaled (alpha kept)

Used in: 3d/spotlight, 3d/bloom_3d, 3d/mesh_ray_cast, animation/color_animation, 2d/mesh2d_vertex_color_texture, shader_advanced/custom_shader_instancing.

Okhsla

Bevy's Okhsla: Oklab's hue (degrees), a saturation relative to the sRGB gamut, and a lightness with a toe, each 0..1.

Bevy: bevy_color::Okhsla.

MemberWhat it does
new Okhsla(hue: number, saturation: number, lightness: number, alpha?: number)Hue in degrees, saturation, lightness and alpha 0 to 1, in Okhsl.
Okhsla.from_oklaba(lab: Oklaba): OkhslaFrom Oklab (bevy_color oklab_to_okhsl).
Okhsla.from(c: Srgba | LinearRgba | Hsla): OkhslaFrom any sRGB colour, through Oklab.
.hue: numberBevy: The hue channel. [0.0, 360.0]
.saturation: numberBevy: The saturation channel. [0.0, 1.0]
.lightness: numberBevy: The lightness channel. [0.0, 1.0]
.alpha: numberBevy: The alpha channel. [0.0, 1.0]
.to_oklaba(): OklabaTo Oklab (bevy_color okhsl_to_oklab).
.to_linear(): LinearRgbaThe same colour as a LinearRgba.
.to_srgba(): SrgbaThe same colour as an Srgba.
.with_alpha(alpha: number): OkhslaThe same colour with this alpha.

Used in: ui/widgets/feathers_gallery.

Oklaba

Bevy's Oklaba: perceptual lightness and two opponent axes.

Bevy: bevy_color::oklaba::Oklaba.

MemberWhat it does
new Oklaba(lightness: number, a: number, b: number, alpha?: number)Perceptual lightness, a (green to red), b (blue to yellow) and alpha.
Oklaba.from(c: Oklcha | Rgb): OklabaThe same colour from an Srgba, LinearRgba or Oklcha.
.lightness: numberBevy: The 'lightness' channel. [0.0, 1.0]
.a: numberBevy: The 'a' channel. [-1.0, 1.0]
.b: numberBevy: The 'b' channel. [-1.0, 1.0]
.alpha: numberBevy: The alpha channel. [0.0, 1.0]
.to_linear(): LinearRgbaThe same colour as a LinearRgba.
.to_srgba(): SrgbaThe same colour as an Srgba.
.mix(o: Oklaba, t: number): OklabaThe colour t (0 to 1) of the way to o, mixed in this space.

Used in: animation/color_animation.

Oklcha

Bevy's Oklcha: Oklab's lightness, chroma and hue (degrees).

Bevy: bevy_color::oklcha::Oklcha.

MemberWhat it does
new Oklcha(lightness: number, chroma: number, hue: number, alpha?: number)Perceptual lightness, chroma, hue in degrees and alpha.
Oklcha.from(c: Rgb | Oklaba): OklchaThe same colour from an Srgba, LinearRgba or Oklaba.
.lightness: numberBevy: The 'lightness' channel. [0.0, 1.0]
.chroma: numberBevy: The 'chroma' channel. [0.0, 1.0]
.hue: numberBevy: The 'hue' channel. [0.0, 360.0]
.alpha: numberBevy: The alpha channel. [0.0, 1.0]
.to_linear(): LinearRgbaThe same colour as a LinearRgba.
.to_srgba(): SrgbaThe same colour as an Srgba.
.mix(o: Oklcha, t: number): OklchaThe colour t (0 to 1) of the way to o, mixed in this space.
.lighter(amount: number): OklchaBevy's Luminance::lighter: the lightness raised by amount, at most 1
.darker(amount: number): OklchaBevy's Luminance::darker: the lightness lowered by amount, at least 0
.with_alpha(alpha: number): OklchaBevy's Alpha::with_alpha

Used in: animation/color_animation.

Srgba

Bevy's Srgba: gamma-encoded colour, each channel 0..1.

Bevy: bevy_color::srgba::Srgba.

MemberWhat it does
new Srgba(red: number, green: number, blue: number, alpha?: number)sRGB red, green, blue and alpha, each 0 to 1.
Srgba.hex(hex: string): SrgbaBevy's Srgba::hex: rgb, rgba, rrggbb or rrggbbaa (the # optional), and also rgb() or a common name
.red: numberBevy: The red channel. [0.0, 1.0] for SDR colors.
.green: numberBevy: The green channel. [0.0, 1.0] for SDR colors.
.blue: numberBevy: The blue channel. [0.0, 1.0] for SDR colors.
.alpha: numberBevy: The alpha channel. [0.0, 1.0]
.darker(amount: number): SrgbaBevy's Luminance::darker on an Srgba: its linear colour's, encoded again
.lighter(amount: number): SrgbaBevy's Luminance::lighter on an Srgba: its linear colour's, encoded again
.with_alpha(alpha: number): SrgbaThe same colour with this alpha.
.to_hex(): stringBevy's Srgba::to_hex: #RRGGBB (and AA when not opaque), each channel clamped and rounded
.mix(o: Srgba, t: number): SrgbaBevy's Mix: each channel lerped, in f32

Used in: ui/images/image_node, ui/images/image_node_resizing, ui/images/ui_texture_atlas_slice, ui/images/ui_texture_slice, ui/images/ui_texture_atlas, ui/layout/grid, ui/layout/ghost_nodes, ui/layout/flex_layout, and 62 more.

Xyza

Bevy's Xyza: CIE 1931 XYZ (D65, as linear sRGB's).

Bevy: bevy_color::xyza::Xyza.

MemberWhat it does
new Xyza(x: number, y: number, z: number, alpha?: number)CIE XYZ and alpha.
Xyza.from(c: Rgb): XyzaThe same colour from an Srgba or LinearRgba.
.x: numberBevy: The x-axis. [0.0, 0.9505] for SDR colors.
.y: numberBevy: The y-axis, intended to represent luminance. [0.0, 1.0] for SDR colors.
.z: numberBevy: The z-axis. [0.0, 1.0889] for SDR colors.
.alpha: numberBevy: The alpha channel. [0.0, 1.0]
.to_linear(): LinearRgbaThe same colour as a LinearRgba.
.to_srgba(): SrgbaThe same colour as an Srgba.
.mix(o: Xyza, t: number): XyzaThe colour t (0 to 1) of the way to o, mixed in this space.

Used in: animation/color_animation.