use crate::models::{Kind, Node};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
pub const GROUP_PALETTE: &[&str] = &[
"#2563eb", "#d97706", "#7c3aed", "#db2777", "#0891b2", "#ea580c", "#4f46e5", "#c026d3", "#0284c7", "#ca8a04", ];
pub struct Style {
pub fill: String,
pub border: String,
pub text: String,
pub shape: Shape,
pub latex_fill: String,
pub latex_border: String,
pub stroke_width: u32,
}
pub enum Shape {
Box,
Rounded,
Diamond,
}
pub struct Theme;
impl Theme {
pub fn get_group_color(name: &str) -> (String, String) {
let idx = get_palette_index(name);
(GROUP_PALETTE[idx].to_string(), format!("TectGroup{}", idx))
}
pub fn get_node_style(node: &Node) -> Style {
if node.is_artificial_error_termination {
return Style {
fill: "#dc2626".into(), border: "#b91c1c".into(),
text: "#ffffff".into(),
shape: Shape::Diamond,
latex_fill: "TectRed".into(),
latex_border: "TectRedDark".into(),
stroke_width: 1,
};
}
if node.is_artificial_graph_start || node.is_artificial_graph_end {
return Style {
fill: "#059669".into(), border: "#047857".into(),
text: "#ffffff".into(),
shape: Shape::Rounded,
latex_fill: "TectGreen".into(),
latex_border: "TectGreenDark".into(),
stroke_width: 1,
};
}
if let Some(group) = &node.function.group {
let (group_hex, group_latex) = Self::get_group_color(&group.name);
Style {
fill: "#1e293b".into(), border: group_hex, text: "#ffffff".into(),
shape: Shape::Box,
latex_fill: "TectBlue".into(),
latex_border: group_latex,
stroke_width: 3, }
} else {
Style {
fill: "#1e293b".into(),
border: "#475569".into(), text: "#ffffff".into(),
shape: Shape::Box,
latex_fill: "TectBlue".into(),
latex_border: "TectBlueDark".into(),
stroke_width: 1,
}
}
}
pub fn get_token_color(kind: &Kind) -> (&'static str, &'static str) {
match kind {
Kind::Constant(_) => ("#a855f7", "TectPurple"), Kind::Variable(_) => ("#94a3b8", "TectGray"), Kind::Error(_) => ("#ef4444", "TectRed"), }
}
}
fn get_palette_index(s: &str) -> usize {
let mut hasher = DefaultHasher::new();
s.hash(&mut hasher);
(hasher.finish() as usize) % GROUP_PALETTE.len()
}