use crate::runtime::layout::MAX_SIDE;
const SOLID: &str = "$solid/";
pub fn solid_name(
w: Option<i32>,
h: Option<i32>,
color: Option<&str>,
alpha: Option<i32>,
) -> String {
let c = crate::gui::parse_rgb(color.unwrap_or(""))
.map(|c| c.0)
.unwrap_or(0);
let a = alpha.unwrap_or(255).clamp(0, 255);
format!(
"{SOLID}{:06x}{:02x}/{}x{}.png",
c & 0xffffff,
a,
w.unwrap_or(1).clamp(1, MAX_SIDE),
h.unwrap_or(1).clamp(1, MAX_SIDE)
)
}
pub fn solid_spec(path: &str) -> Option<(i32, i32, u32)> {
let rest = path.strip_prefix(SOLID)?.strip_suffix(".png")?;
let (spec, size) = rest.split_once('/')?;
if size.contains('/') {
return None;
}
let argb = u32::from_str_radix(spec, 16).ok()?;
let (w, h) = size.split_once('x')?;
let w = w.parse::<i32>().unwrap_or(1).clamp(1, MAX_SIDE);
let h = h.parse::<i32>().unwrap_or(1).clamp(1, MAX_SIDE);
let rgb = argb >> 8;
let a = argb & 0xff;
Some((w, h, (a << 24) | rgb))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_solid_name_round_trips() {
let name = solid_name(Some(4), Some(2), Some("255,0,0"), Some(128));
assert_eq!(name, "$solid/ff000080/4x2.png");
assert_eq!(solid_spec(&name), Some((4, 2, 0x80ff_0000)));
}
}