Skip to content

Commit 4c32259

Browse files
Taym95marc2332
andauthored
feat: Support Hex colors (#579)
* Support-Hex-colors Signed-off-by: Taym <[email protected]> * docs --------- Signed-off-by: Taym <[email protected]> Co-authored-by: marc2332 <[email protected]>
1 parent 8422ddb commit 4c32259

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

crates/elements/src/_docs/color_syntax.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
//! - `orange`
1414
//! - `transparent`
1515
//!
16-
//! #### rgb() / hsl()
16+
//! #### rgb() / hsl() / Hex
1717
//!
1818
//! - With RGB: `rgb(150, 60, 20)`
1919
//! - With RGB and alpha: `rgb(150, 60, 20, 0.7)`
2020
//! - You can also use 0-255 for the alpha: `rgb(150, 60, 20, 70)`
2121
//! - With HSL: `hsl(28deg, 80%, 50%)`
2222
//! - With HSL and alpha: `hsl(28deg, 80%, 50%, 25%)`
23+
//! - With Hex: `#E93323`

crates/state/src/values/color.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ impl Parse for Color {
2929
parse_hsl(value)
3030
} else if value.starts_with("rgb(") {
3131
parse_rgb(value)
32+
} else if value.starts_with("#") {
33+
parse_hex_color(value)
3234
} else {
3335
Err(ParseColorError)
3436
}
@@ -183,3 +185,14 @@ fn parse_hsl(color: &str) -> Result<Color, ParseColorError> {
183185
Ok(hsv.to_color(255))
184186
}
185187
}
188+
189+
fn parse_hex_color(color: &str) -> Result<Color, ParseColorError> {
190+
if color.len() == 7 {
191+
let r = u8::from_str_radix(&color[1..3], 16).map_err(|_| ParseColorError)?;
192+
let g = u8::from_str_radix(&color[3..5], 16).map_err(|_| ParseColorError)?;
193+
let b = u8::from_str_radix(&color[5..7], 16).map_err(|_| ParseColorError)?;
194+
Ok(Color::from_rgb(r, g, b))
195+
} else {
196+
return Err(ParseColorError);
197+
}
198+
}

crates/state/tests/parse_color.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ fn parse_argb_color_f32() {
3030
assert_eq!(color, Ok(Color::from_argb(128, 91, 123, 57)));
3131
}
3232

33+
#[test]
34+
fn parse_hex_color() {
35+
let color = Color::parse("#FFA500");
36+
assert_eq!(color, Ok(Color::from_rgb(255, 165, 0)));
37+
}
38+
3339
#[test]
3440
fn invalid_colors() {
3541
let incorrect_name = Color::parse("wow(0, 0, 0)");
@@ -42,6 +48,8 @@ fn invalid_colors() {
4248
let extra_component = Color::parse("rgb(0, 0, 0, 0, 0)");
4349
let extra_ending_commas = Color::parse("rgb(0, 0, 0, 0,)");
4450
let bad_unit = Color::parse("hsl(28in, 0.4, 0.25, 50%)");
51+
let missing_number_sign = Color::parse("FFA500");
52+
let incorrect_hex_length = Color::parse("#FFA0");
4553

4654
assert_eq!(incorrect_name.is_err(), true);
4755
assert_eq!(extra_lparen.is_err(), true);
@@ -53,4 +61,6 @@ fn invalid_colors() {
5361
assert_eq!(extra_component.is_err(), true);
5462
assert_eq!(extra_ending_commas.is_err(), true);
5563
assert_eq!(bad_unit.is_err(), true);
64+
assert_eq!(missing_number_sign.is_err(), true);
65+
assert_eq!(incorrect_hex_length.is_err(), true);
5666
}

0 commit comments

Comments
 (0)