Skip to content

Commit 2c3a83f

Browse files
areweragealradish
authored andcommitted
Add a more familiar hex color entry (bevyengine#7060)
# 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.
1 parent 8e65bcd commit 2c3a83f

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

crates/bevy_render/src/color/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,10 +250,14 @@ impl Color {
250250
/// # use bevy_render::color::Color;
251251
/// let color = Color::hex("FF00FF").unwrap(); // fuchsia
252252
/// let color = Color::hex("FF00FF7F").unwrap(); // partially transparent fuchsia
253+
///
254+
/// // A standard hex color notation is also available
255+
/// assert_eq!(Color::hex("#FFFFFF").unwrap(), Color::rgb(1.0, 1.0, 1.0));
253256
/// ```
254257
///
255258
pub fn hex<T: AsRef<str>>(hex: T) -> Result<Color, HexColorError> {
256259
let hex = hex.as_ref();
260+
let hex = hex.strip_prefix('#').unwrap_or(hex);
257261

258262
// RGB
259263
if hex.len() == 3 {

examples/3d/pbr.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ fn setup(
3030
.unwrap(),
3131
),
3232
material: materials.add(StandardMaterial {
33-
base_color: Color::hex("ffd891").unwrap(),
33+
base_color: Color::hex("#ffd891").unwrap(),
3434
// vary key PBR parameters on a grid of spheres to show the effect
3535
metallic: y01,
3636
perceptual_roughness: x01,
@@ -51,7 +51,7 @@ fn setup(
5151
.unwrap(),
5252
),
5353
material: materials.add(StandardMaterial {
54-
base_color: Color::hex("ffd891").unwrap(),
54+
base_color: Color::hex("#ffd891").unwrap(),
5555
// vary key PBR parameters on a grid of spheres to show the effect
5656
unlit: true,
5757
..default()

0 commit comments

Comments
 (0)