Skip to content

Commit

Permalink
[Feat] convert rgb hex color to rgba hex color
Browse files Browse the repository at this point in the history
  • Loading branch information
mistricky committed Mar 31, 2024
1 parent 8a05bdd commit 14d2445
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
33 changes: 25 additions & 8 deletions generator/src/color.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
use std::i64;
use tiny_skia::Color;

const HEX_COLOR_LENGTH: usize = 7;
const HEX_COLOR_WITH_ALPHA_LENGTH: usize = 9;

pub struct RgbaColor {
pub color: Color,
}

pub fn is_valid_hex_color(color: &str) -> bool {
(color.len() == HEX_COLOR_LENGTH || color.len() == HEX_COLOR_WITH_ALPHA_LENGTH)
&& color.starts_with("#")
}

fn parse_color_to_rgba_hex(hex: &str) -> String {
if !is_valid_hex_color(&hex) || hex.len() == HEX_COLOR_WITH_ALPHA_LENGTH {
hex.to_string()
} else {
format!("{}ff", hex)
}
}

impl Into<RgbaColor> for String {
fn into(self) -> RgbaColor {
let hex_color = &self.to_lowercase()[1..self.len()];
let rgba_hex_color = parse_color_to_rgba_hex(&self);
// Remove the '#' symbol
let hex_color = &rgba_hex_color.to_lowercase()[1..rgba_hex_color.len()];
let chars = hex_color.chars().collect::<Vec<char>>();
let splits = &chars
.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, alpha as u8),
color: Color::from_rgba8(
splits[0] as u8,
splits[1] as u8,
splits[2] as u8,
splits[3] as u8,
),
}
}
}
12 changes: 2 additions & 10 deletions generator/src/components/background.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@ use tiny_skia::{
Color, GradientStop, LinearGradient, Paint, Pixmap, Point, Rect, SpreadMode, Transform,
};

use crate::color::RgbaColor;
use crate::color::{is_valid_hex_color, RgbaColor};

use super::interface::{
component::{Component, ComponentContext, RenderParams},
render_error::{self, RenderError},
style::{ComponentAlign, ComponentStyle, RawComponentStyle},
};

const HEX_COLOR_LENGTH: usize = 7;
const HEX_COLOR_WITH_ALPHA_LENGTH: usize = 9;

pub struct Background {
children: Vec<Box<dyn Component>>,
}
Expand All @@ -23,11 +20,6 @@ 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
Expand All @@ -52,7 +44,7 @@ impl Component for Background {
paint.anti_alias = false;
match params.bg_color.as_ref() {
Some(color) => {
if ! is_valid_hex_color(color.to_string()){
if !is_valid_hex_color(color) {
return Err(RenderError::InvalidHexColor(color.to_string()));
}

Expand Down

0 comments on commit 14d2445

Please sign in to comment.