Skip to main content

parse_hex

Function parse_hex 

pub fn parse_hex(s: &str) -> Option<AlphaColor<Srgb>>
Expand description

Parse #rgb, #rrggbb or #rrggbbaa (leading # optional, surrounding whitespace ignored, case-insensitive). The short form doubles each digit, as CSS does. None for anything else, including a sign or a non-ASCII character that u8::from_str_radix alone would let through or trip over.

use tilo_ui::color::parse_hex;
use tilo_ui::vello::peniko::Color;
assert_eq!(parse_hex("#3B82F6"), Some(Color::from_rgb8(0x3B, 0x82, 0xF6)));
assert_eq!(parse_hex("f0a"), Some(Color::from_rgb8(0xFF, 0x00, 0xAA)));
assert_eq!(parse_hex("#00000080"), Some(Color::from_rgba8(0, 0, 0, 0x80)));
assert_eq!(parse_hex("#12345"), None);