Skip to content

Commit

Permalink
Add a more familiar hex color entry (bevyengine#7060)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
arewerage authored and ItsDoot committed Feb 1, 2023
1 parent fe47ef9 commit cf04434
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crates/bevy_render/src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: AsRef<str>>(hex: T) -> Result<Color, HexColorError> {
let hex = hex.as_ref();
let hex = hex.strip_prefix('#').unwrap_or(hex);

// RGB
if hex.len() == 3 {
Expand Down
4 changes: 2 additions & 2 deletions examples/3d/pbr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()
Expand Down

0 comments on commit cf04434

Please sign in to comment.