[feat] support RGBA values as background color

This commit is contained in:
mrWinston 2024-03-19 21:11:02 +01:00 committed by Mist
parent 6c753177e3
commit 8a05bddadb
2 changed files with 14 additions and 2 deletions

View file

@ -13,9 +13,15 @@ impl Into<RgbaColor> for String {
.chunks(2)
.map(|chunk| i64::from_str_radix(&chunk.iter().collect::<String>(), 16).unwrap())
.collect::<Vec<_>>();
let alpha: i64;
match splits.get(3) {
Some(x) => alpha = *x,
None => alpha = 255,
}
RgbaColor {
color: Color::from_rgba8(splits[0] as u8, splits[1] as u8, splits[2] as u8, 255),
color: Color::from_rgba8(splits[0] as u8, splits[1] as u8, splits[2] as u8, alpha as u8),
}
}
}

View file

@ -11,6 +11,7 @@ use super::interface::{
};
const HEX_COLOR_LENGTH: usize = 7;
const HEX_COLOR_WITH_ALPHA_LENGTH: usize = 9;
pub struct Background {
children: Vec<Box<dyn Component>>,
@ -22,6 +23,11 @@ impl Background {
}
}
fn is_valid_hex_color(color: String) -> bool {
(color.len() == HEX_COLOR_LENGTH || color.len() == HEX_COLOR_WITH_ALPHA_LENGTH)
&& color.starts_with("#")
}
impl Component for Background {
fn children(&self) -> &Vec<Box<dyn Component>> {
&self.children
@ -46,7 +52,7 @@ impl Component for Background {
paint.anti_alias = false;
match params.bg_color.as_ref() {
Some(color) => {
if color.len() != HEX_COLOR_LENGTH || !color.starts_with("#") {
if ! is_valid_hex_color(color.to_string()){
return Err(RenderError::InvalidHexColor(color.to_string()));
}