Skip to content

Commit cb56563

Browse files
Azorloghaevyrie
authored andcommitted
Add conversions from Color to u32 (bevyengine#4088)
# Objective - `Mesh::ATTRIBUTE_COLOR` expects colors as `u32`s but there is no function for easy conversion. - See bevyengine#4037 (review) ## Solution - Added `Color::as_rgba_u32` and `Color::as_linear_rgba_u32`
1 parent 026761c commit cb56563

File tree

1 file changed

+93
-1
lines changed
  • crates/bevy_render/src/color

1 file changed

+93
-1
lines changed

crates/bevy_render/src/color/mod.rs

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ impl Color {
414414
}
415415
}
416416

417-
/// Converts a `Color` to a `[f32; 4]` from linear RBG colorspace
417+
/// Converts a `Color` to a `[f32; 4]` from linear RGB colorspace
418418
#[inline]
419419
pub fn as_linear_rgba_f32(self: Color) -> [f32; 4] {
420420
match self {
@@ -487,6 +487,98 @@ impl Color {
487487
} => [hue, saturation, lightness, alpha],
488488
}
489489
}
490+
491+
/// Converts Color to a u32 from sRGB colorspace.
492+
///
493+
/// Maps the RGBA channels in RGBA order to a little-endian byte array (GPUs are little-endian).
494+
/// A will be the most significant byte and R the least significant.
495+
pub fn as_rgba_u32(self: Color) -> u32 {
496+
match self {
497+
Color::Rgba {
498+
red,
499+
green,
500+
blue,
501+
alpha,
502+
} => u32::from_le_bytes([
503+
(red * 255.0) as u8,
504+
(green * 255.0) as u8,
505+
(blue * 255.0) as u8,
506+
(alpha * 255.0) as u8,
507+
]),
508+
Color::RgbaLinear {
509+
red,
510+
green,
511+
blue,
512+
alpha,
513+
} => u32::from_le_bytes([
514+
(red.linear_to_nonlinear_srgb() * 255.0) as u8,
515+
(green.linear_to_nonlinear_srgb() * 255.0) as u8,
516+
(blue.linear_to_nonlinear_srgb() * 255.0) as u8,
517+
(alpha * 255.0) as u8,
518+
]),
519+
Color::Hsla {
520+
hue,
521+
saturation,
522+
lightness,
523+
alpha,
524+
} => {
525+
let [red, green, blue] =
526+
HslRepresentation::hsl_to_nonlinear_srgb(hue, saturation, lightness);
527+
u32::from_le_bytes([
528+
(red * 255.0) as u8,
529+
(green * 255.0) as u8,
530+
(blue * 255.0) as u8,
531+
(alpha * 255.0) as u8,
532+
])
533+
}
534+
}
535+
}
536+
537+
/// Converts Color to a u32 from linear RGB colorspace.
538+
///
539+
/// Maps the RGBA channels in RGBA order to a little-endian byte array (GPUs are little-endian).
540+
/// A will be the most significant byte and R the least significant.
541+
pub fn as_linear_rgba_u32(self: Color) -> u32 {
542+
match self {
543+
Color::Rgba {
544+
red,
545+
green,
546+
blue,
547+
alpha,
548+
} => u32::from_le_bytes([
549+
(red.nonlinear_to_linear_srgb() * 255.0) as u8,
550+
(green.nonlinear_to_linear_srgb() * 255.0) as u8,
551+
(blue.nonlinear_to_linear_srgb() * 255.0) as u8,
552+
(alpha * 255.0) as u8,
553+
]),
554+
Color::RgbaLinear {
555+
red,
556+
green,
557+
blue,
558+
alpha,
559+
} => u32::from_le_bytes([
560+
(red * 255.0) as u8,
561+
(green * 255.0) as u8,
562+
(blue * 255.0) as u8,
563+
(alpha * 255.0) as u8,
564+
]),
565+
Color::Hsla {
566+
hue,
567+
saturation,
568+
lightness,
569+
alpha,
570+
} => {
571+
let [red, green, blue] =
572+
HslRepresentation::hsl_to_nonlinear_srgb(hue, saturation, lightness);
573+
u32::from_le_bytes([
574+
(red.nonlinear_to_linear_srgb() * 255.0) as u8,
575+
(green.nonlinear_to_linear_srgb() * 255.0) as u8,
576+
(blue.nonlinear_to_linear_srgb() * 255.0) as u8,
577+
(alpha * 255.0) as u8,
578+
])
579+
}
580+
}
581+
}
490582
}
491583

492584
impl Default for Color {

0 commit comments

Comments
 (0)