Add a more familiar hex color entry (#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.  ## Solution - Allow you to enter colors like `#ff33f2` and use the `.strip_prefix` method to delete the `#` character.
This commit is contained in:
parent
8ca3d0462c
commit
4fff0ce837
@ -250,10 +250,14 @@ impl Color {
|
|||||||
/// # use bevy_render::color::Color;
|
/// # use bevy_render::color::Color;
|
||||||
/// let color = Color::hex("FF00FF").unwrap(); // fuchsia
|
/// let color = Color::hex("FF00FF").unwrap(); // fuchsia
|
||||||
/// let color = Color::hex("FF00FF7F").unwrap(); // partially transparent 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> {
|
pub fn hex<T: AsRef<str>>(hex: T) -> Result<Color, HexColorError> {
|
||||||
let hex = hex.as_ref();
|
let hex = hex.as_ref();
|
||||||
|
let hex = hex.strip_prefix('#').unwrap_or(hex);
|
||||||
|
|
||||||
// RGB
|
// RGB
|
||||||
if hex.len() == 3 {
|
if hex.len() == 3 {
|
||||||
|
@ -30,7 +30,7 @@ fn setup(
|
|||||||
.unwrap(),
|
.unwrap(),
|
||||||
),
|
),
|
||||||
material: materials.add(StandardMaterial {
|
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
|
// vary key PBR parameters on a grid of spheres to show the effect
|
||||||
metallic: y01,
|
metallic: y01,
|
||||||
perceptual_roughness: x01,
|
perceptual_roughness: x01,
|
||||||
@ -51,7 +51,7 @@ fn setup(
|
|||||||
.unwrap(),
|
.unwrap(),
|
||||||
),
|
),
|
||||||
material: materials.add(StandardMaterial {
|
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
|
// vary key PBR parameters on a grid of spheres to show the effect
|
||||||
unlit: true,
|
unlit: true,
|
||||||
..default()
|
..default()
|
||||||
|
Loading…
Reference in New Issue
Block a user