From d64882ffbf73522afe16cb8c9b65f279598beff5 Mon Sep 17 00:00:00 2001 From: Guillaume DALLENNE Date: Sat, 29 Aug 2020 01:50:49 +0200 Subject: [PATCH] add Color::rgb_u8 and Color::rgba_u8 (#381) --- crates/bevy_render/src/color.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/crates/bevy_render/src/color.rs b/crates/bevy_render/src/color.rs index c747a5838fd5b..25e7ff299d5fc 100644 --- a/crates/bevy_render/src/color.rs +++ b/crates/bevy_render/src/color.rs @@ -37,6 +37,21 @@ impl Color { pub const fn rgba(r: f32, g: f32, b: f32, a: f32) -> Color { Color { r, g, b, a } } + + pub fn rgb_u8(r: u8, g: u8, b: u8) -> Color { + Color::rgba_u8(r, g, b, u8::MAX) + } + + // Float operations in const fn are not stable yet + // see https://github.com/rust-lang/rust/issues/57241 + pub fn rgba_u8(r: u8, g: u8, b: u8, a: u8) -> Color { + Color::rgba( + r as f32 / u8::MAX as f32, + g as f32 / u8::MAX as f32, + b as f32 / u8::MAX as f32, + a as f32 / u8::MAX as f32, + ) + } } impl Default for Color {