-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathutils.rs
More file actions
16 lines (13 loc) · 617 Bytes
/
utils.rs
File metadata and controls
16 lines (13 loc) · 617 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
pub fn hexa_to_rgb(hex_code: String) -> Result<(u8, u8, u8), String> {
let hex = hex_code.trim_start_matches('#');
if hex.len() != 6 {
return Err(format!("Invalid hex color length: expected 6, got {}", hex.len()));
}
let r = u8::from_str_radix(&hex[0..2], 16)
.map_err(|e| format!("Invalid red component in hex: {}", e))?;
let g = u8::from_str_radix(&hex[2..4], 16)
.map_err(|e| format!("Invalid green component in hex: {}", e))?;
let b = u8::from_str_radix(&hex[4..6], 16)
.map_err(|e| format!("Invalid blue component in hex: {}", e))?;
Ok((r, g, b))
}