Skip to content

Commit

Permalink
🐛 Fix color serializer.
Browse files Browse the repository at this point in the history
  • Loading branch information
langyo committed Nov 15, 2024
1 parent 7fc1492 commit 7f1e540
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 17 deletions.
12 changes: 6 additions & 6 deletions packages/theme/src/prelude/element/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use once_cell::sync::Lazy;
use crate::types::{Color, Palette};

pub static COLOR_MAP: Lazy<Palette> = Lazy::new(|| Palette {
primary: Color::from_rgb_str_hex("#409EFF"),
secondary: Color::from_rgb_str_hex("#79bbff"),
success: Color::from_rgb_str_hex("#67C23A"),
warning: Color::from_rgb_str_hex("#E6A23C"),
error: Color::from_rgb_str_hex("#F56C6C"),
info: Color::from_rgb_str_hex("#909399"),
primary: Color::from_rgb_str("#409EFF"),
secondary: Color::from_rgb_str("#79bbff"),
success: Color::from_rgb_str("#67C23A"),
warning: Color::from_rgb_str("#E6A23C"),
error: Color::from_rgb_str("#F56C6C"),
info: Color::from_rgb_str("#909399"),
});
56 changes: 47 additions & 9 deletions packages/theme/src/types/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,20 +53,58 @@ pub struct Color {
pub red: f32,
pub green: f32,
pub blue: f32,
pub alpha: Option<f32>,
}

#[allow(dead_code)]
impl Color {
pub fn new(red: f32, green: f32, blue: f32) -> Self {
Self { red, green, blue }
pub fn new(red: f32, green: f32, blue: f32, alpha: Option<f32>) -> Self {
Self {
red,
green,
blue,
alpha,
}
}

pub fn from_rgb_str_hex(rgb_str_hex: &str) -> Self {
let rgb_str_hex = rgb_str_hex.trim_start_matches('#');
let red = u8::from_str_radix(&rgb_str_hex[0..2], 16).unwrap() as f32 / 256.0;
let green = u8::from_str_radix(&rgb_str_hex[2..4], 16).unwrap() as f32 / 256.0;
let blue = u8::from_str_radix(&rgb_str_hex[4..6], 16).unwrap() as f32 / 256.0;
Self { red, green, blue }
pub fn from_rgb_str(str: &str) -> Self {
if str.starts_with("rgba") {
let str = str.trim_start_matches("rgba(").trim_end_matches(')');
let mut split = str.split(',');
let red = split.next().unwrap().parse::<f32>().unwrap() / 256.;
let green = split.next().unwrap().parse::<f32>().unwrap() / 256.;
let blue = split.next().unwrap().parse::<f32>().unwrap() / 256.;
let alpha = split.next().unwrap().parse::<f32>().unwrap();
Self {
red,
green,
blue,
alpha: Some(alpha),
}
} else if str.starts_with("#") {
let str = str.trim_start_matches('#');
let red = u8::from_str_radix(&str[0..2], 16).unwrap() as f32 / 256.;
let green = u8::from_str_radix(&str[2..4], 16).unwrap() as f32 / 256.;
let blue = u8::from_str_radix(&str[4..6], 16).unwrap() as f32 / 256.;
Self {
red,
green,
blue,
alpha: None,
}
} else {
let str = str.trim_start_matches("rgb(").trim_end_matches(')');
let mut split = str.split(',');
let red = split.next().unwrap().parse::<f32>().unwrap() / 256.;
let green = split.next().unwrap().parse::<f32>().unwrap() / 256.;
let blue = split.next().unwrap().parse::<f32>().unwrap() / 256.;
Self {
red,
green,
blue,
alpha: None,
}
}
}

pub fn to_rgb_str(&self) -> String {
Expand Down Expand Up @@ -115,6 +153,6 @@ impl<'de> Deserialize<'de> for Color {
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Ok(Color::from_rgb_str_hex(&s))
Ok(Color::from_rgb_str(&s))
}
}
2 changes: 0 additions & 2 deletions website/src/web_entry.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use anyhow::anyhow;

use wasm_bindgen::prelude::*;
use web_sys::window;

Expand Down

0 comments on commit 7f1e540

Please sign in to comment.