#[cfg(not(feature = "std"))]
use alloc::format;
#[cfg(feature = "std")]
use std::format;
use super::CoreError;
pub fn parse_bgr_color(color_str: &str) -> Result<[u8; 4], CoreError> {
let trimmed = color_str.trim();
let hex_part =
if (trimmed.starts_with("&H") || trimmed.starts_with("&h")) && trimmed.ends_with('&') {
&trimmed[2..trimmed.len() - 1]
} else if let Some(stripped) = trimmed.strip_prefix("&H") {
stripped
} else if let Some(stripped) = trimmed.strip_prefix("&h") {
stripped
} else if let Some(stripped) = trimmed.strip_prefix("0x") {
stripped
} else if trimmed.chars().all(|c| c.is_ascii_hexdigit()) {
trimmed
} else {
return Err(CoreError::InvalidColor(format!(
"Invalid color format: {color_str}"
)));
};
let hex_value = u32::from_str_radix(hex_part, 16)
.map_err(|_| CoreError::InvalidColor(format!("Invalid hex value: {hex_part}")))?;
let color_array = match hex_part.len() {
6 => {
let red = (hex_value & 0xFF) as u8;
let green = ((hex_value >> 8) & 0xFF) as u8;
let blue = ((hex_value >> 16) & 0xFF) as u8;
[red, green, blue, 0] }
8 => {
let alpha = ((hex_value >> 24) & 0xFF) as u8; let red = (hex_value & 0xFF) as u8;
let green = ((hex_value >> 8) & 0xFF) as u8;
let blue = ((hex_value >> 16) & 0xFF) as u8;
[red, green, blue, alpha]
}
_ => {
return Err(CoreError::InvalidColor(format!(
"Invalid color length: {}",
hex_part.len()
)))
}
};
Ok(color_array)
}