From 2c3a83fc3de65a2f863c680abc7d273b32f5be05 Mon Sep 17 00:00:00 2001 From: Anton Pushkarev Date: Wed, 4 Jan 2023 23:40:42 +0000 Subject: [PATCH] Add a more familiar hex color entry (#7060) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Objective - When using `Color::hex` for the first time, I was confused by the fact that I can't specify colors using #, which is much more familiar. - In the code editor (if there is support) there is a preview of the color, which is very convenient. ![Снимок экрана от 2022-12-30 02-54-00](https://user-images.githubusercontent.com/69102503/209990973-f6fc3bc6-08f6-4e51-a9a9-1de8a675c82d.png) ## Solution - Allow you to enter colors like `#ff33f2` and use the `.strip_prefix` method to delete the `#` character. --- crates/bevy_render/src/color/mod.rs | 4 ++++ examples/3d/pbr.rs | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/crates/bevy_render/src/color/mod.rs b/crates/bevy_render/src/color/mod.rs index 4fe72e7c3b348..4b9574dc22a75 100644 --- a/crates/bevy_render/src/color/mod.rs +++ b/crates/bevy_render/src/color/mod.rs @@ -250,10 +250,14 @@ impl Color { /// # use bevy_render::color::Color; /// let color = Color::hex("FF00FF").unwrap(); // fuchsia /// let color = Color::hex("FF00FF7F").unwrap(); // partially transparent fuchsia + /// + /// // A standard hex color notation is also available + /// assert_eq!(Color::hex("#FFFFFF").unwrap(), Color::rgb(1.0, 1.0, 1.0)); /// ``` /// pub fn hex>(hex: T) -> Result { let hex = hex.as_ref(); + let hex = hex.strip_prefix('#').unwrap_or(hex); // RGB if hex.len() == 3 { diff --git a/examples/3d/pbr.rs b/examples/3d/pbr.rs index 926a7eadfb577..c3159cff3fcea 100644 --- a/examples/3d/pbr.rs +++ b/examples/3d/pbr.rs @@ -30,7 +30,7 @@ fn setup( .unwrap(), ), material: materials.add(StandardMaterial { - base_color: Color::hex("ffd891").unwrap(), + base_color: Color::hex("#ffd891").unwrap(), // vary key PBR parameters on a grid of spheres to show the effect metallic: y01, perceptual_roughness: x01, @@ -51,7 +51,7 @@ fn setup( .unwrap(), ), material: materials.add(StandardMaterial { - base_color: Color::hex("ffd891").unwrap(), + base_color: Color::hex("#ffd891").unwrap(), // vary key PBR parameters on a grid of spheres to show the effect unlit: true, ..default()