use super::mark;
use super::shape::Shape;
const CANVAS: f32 = 256.0;
fn hex(hue: egui::Color32) -> String {
format!("#{:02x}{:02x}{:02x}", hue.r(), hue.g(), hue.b())
}
fn at((x, y): (f32, f32)) -> String {
format!("{:.2},{:.2}", x * CANVAS, y * CANVAS)
}
impl Shape {
fn element(&self) -> String {
match self {
Self::Trace { fill, .. } | Self::Lune { fill, .. } => {
let ribs = self.ribs();
let mut points: Vec<String> = ribs.iter().map(|rib| at(rib.out)).collect();
points.extend(ribs.iter().rev().map(|rib| at(rib.back)));
format!(
" <polygon points=\"{}\" fill=\"{}\"/>\n",
points.join(" "),
hex(*fill)
)
}
Self::Disc {
cx,
cy,
radius,
fill,
} => format!(
" <circle cx=\"{:.2}\" cy=\"{:.2}\" r=\"{:.2}\" fill=\"{}\"/>\n",
cx * CANVAS,
cy * CANVAS,
radius * CANVAS,
hex(*fill)
),
}
}
}
pub fn svg() -> String {
let body: String = mark().iter().map(Shape::element).collect();
format!(
"<svg xmlns=\"http://www.w3.org/2000/svg\" viewBox=\"0 0 {CANVAS:.0} {CANVAS:.0}\" \
width=\"{CANVAS:.0}\" height=\"{CANVAS:.0}\">\n\
\x20 <title>yog — the key and the gate</title>\n\
\x20 <!-- Generated by yog::theme::icon::svg(). Edit the walk constants, not this file. -->\n\
{body}</svg>\n"
)
}