feat: lossless share links + auto iteration count

This commit is contained in:
2026-09-15 15:50:57 +02:00
parent c92c685ade
commit acbf25d64e
2 changed files with 53 additions and 8 deletions
+14 -3
View File
@@ -14,6 +14,8 @@ use crate::fractal::FractalKind;
pub struct ShareState {
pub julia: bool,
pub kind: FractalKind,
/// Exponent for the Multibrot kind (ignored by others).
pub power: u32,
pub center_re: String,
pub center_im: String,
pub half_height: f64,
@@ -21,6 +23,8 @@ pub struct ShareState {
pub julia_c: (f64, f64),
pub color_scale: f32,
pub color_offset: f32,
/// Palette index (`palette_id` in the shader).
pub palette: u32,
}
impl ShareState {
@@ -33,14 +37,15 @@ impl ShareState {
FractalKind::Multibrot => "multi",
FractalKind::Tricorn => "tricorn",
}));
s.push_str(&format!("&pw={}", self.power));
s.push_str(&format!(
"&re={}&im={}&hh={}&it={}",
self.center_re, self.center_im, self.half_height, self.iterations
));
s.push_str(&format!("&jr={}&ji={}", self.julia_c.0, self.julia_c.1));
s.push_str(&format!(
"&cs={}&co={}",
self.color_scale, self.color_offset
"&cs={}&co={}&pal={}",
self.color_scale, self.color_offset, self.palette
));
s
}
@@ -66,6 +71,7 @@ impl ShareState {
_ => FractalKind::Mandelbrot
})
.unwrap_or(FractalKind::Mandelbrot),
power: map.get("pw").and_then(|s| s.parse().ok()).unwrap_or(2),
center_re: (*map.get("re")?).to_string(),
center_im: (*map.get("im")?).to_string(),
half_height: map.get("hh")?.parse().ok()?,
@@ -76,6 +82,7 @@ impl ShareState {
),
color_scale: map.get("cs").and_then(|s| s.parse().ok()).unwrap_or(0.02),
color_offset: map.get("co").and_then(|s| s.parse().ok()).unwrap_or(0.0),
palette: map.get("pal").and_then(|s| s.parse().ok()).unwrap_or(0),
})
}
}
@@ -88,7 +95,8 @@ mod tests {
fn round_trip() {
let s = ShareState {
julia: true,
kind: FractalKind::Tricorn,
kind: FractalKind::Multibrot,
power: 5,
center_re: "-0.743643887037158704752191506114774".into(),
center_im: "0.131825904205311970493132056385139".into(),
half_height: 1.5e-20,
@@ -96,15 +104,18 @@ mod tests {
julia_c: (-0.123, 0.745),
color_scale: 0.02,
color_offset: 0.25,
palette: 3,
};
let d = ShareState::decode(&s.encode()).unwrap();
assert_eq!(d.julia, s.julia);
assert_eq!(d.kind, s.kind);
assert_eq!(d.power, s.power);
assert_eq!(d.center_re, s.center_re);
assert_eq!(d.center_im, s.center_im);
assert_eq!(d.half_height, s.half_height);
assert_eq!(d.iterations, s.iterations);
assert_eq!(d.julia_c, s.julia_c);
assert_eq!(d.palette, s.palette);
}
#[test]