feat: Add CLI args to animate color scale

This commit is contained in:
2026-09-30 11:24:21 +02:00
parent aae786a3e9
commit 2e35ba551d
4 changed files with 78 additions and 8 deletions
+6 -3
View File
@@ -45,7 +45,7 @@ Native CLI flags (`src/cli.rs`, applied in `FractalApp::apply_cli`): `--kind`,
`--palette`, `--share <fragment>`,
`--view re,im,half_height[,iterations]`, `--rendering-kind`,
`--yaw`/`--pitch` (3D camera, degrees), `--de`, `--antialias` (2×2),
`--auto-color-scale`, `--buddhabrot`,
`--auto-color-scale`, `--color-scale`, `--buddhabrot`,
`--buddha-palette`. `--headless` (`src/headless.rs`) skips the window
entirely: it builds the same view from the other flags, creates its own
offscreen wgpu device, and renders straight to a PNG (`--width`/`--height`,
@@ -64,8 +64,11 @@ end-state flag alongside the start flags (`--view`/`--share`/`--kind`/
position/zoom/iterations are pulled out of the link), `--to-iterations`,
`--to-julia`, `--to-phoenix-p`, `--to-lambda-l`, `--to-complex-power`
(or `--to-complex-power-re`/`--to-complex-power-im` to move one component),
and `--to-kind` (per-step formula blend via `KindMorph`, camera untouched).
Anything without a target stays at its start value; colors stay fixed.
`--to-kind` (per-step formula blend via `KindMorph`, camera untouched), and
`--to-color-scale` (log-linear from `--color-scale`; refused with
`--auto-color-scale`; `--to-color-scale-at HALF_HEIGHT` pins its completion
to a zoom depth instead of the end). Anything without a target stays at its start value;
other colors (palette, offset) stay fixed.
`--export-path` then names an output *directory* of `frame-00001.png`,
`frame-00002.png`, ... instead of a single file. `--export-path -` writes
to stdout instead (refused on a terminal): the PNG for a still, or for an
+3
View File
@@ -877,6 +877,9 @@ impl FractalApp {
if cli.auto_color_scale {
self.auto_color_scale = true;
}
if let Some(scale) = cli.color_scale {
self.color_scale = scale.clamp(1e-4, 1.0);
}
self.export_path = cli.export_path;
}
+16
View File
@@ -88,6 +88,11 @@ pub struct Cli {
#[arg(long)]
pub auto_color_scale: bool,
/// Colour scale: palette cycles per escape-time unit, clamped to
/// [0.0001, 1] (overrides a --share link's value).
#[arg(long, value_name = "SCALE")]
pub color_scale: Option<f32>,
/// Output path for --headless (default: fractal-<timestamp>.png). When
/// animating (--to-view/--to-share), this is a directory of
/// frame-00001.png, frame-00002.png, ... instead (default:
@@ -114,6 +119,17 @@ pub struct Cli {
#[arg(long)]
pub to_iterations: Option<u32>,
/// End colour scale for an animation (from --color-scale), interpolated
/// geometrically. Can't be combined with --auto-color-scale.
#[arg(long, value_name = "SCALE")]
pub to_color_scale: Option<f32>,
/// Zoom depth (half-height, e.g. 1e-60) at which --to-color-scale is
/// reached: the scale then follows the zoom depth from the start view to
/// there, and holds. Requires --to-color-scale.
#[arg(long, value_name = "HALF_HEIGHT")]
pub to_color_scale_at: Option<String>,
/// End Julia constant for an animation: c is interpolated from --julia
/// to this over the frames.
#[arg(long, value_name = "RE,IM", allow_hyphen_values = true)]
+53 -5
View File
@@ -19,7 +19,7 @@ use crate::fractal::{
export_to_png_blocking, render_readback_blocking, unpad_rgba,
};
use crate::view::{
ViewState, big_from_decimal_str, interpolate_f64, interpolate_view, parse_view_spec,
Scale, ViewState, big_from_decimal_str, interpolate_f64, interpolate_view, parse_view_spec,
precision_for,
};
@@ -133,6 +133,9 @@ struct AnimTargets {
/// 3D camera, degrees.
to_yaw: Option<f32>,
to_pitch: Option<f32>,
to_color_scale: Option<f32>,
/// Zoom depth where `to_color_scale` is reached (else: at the end).
to_color_scale_at: Option<Scale>,
frames: Option<u32>,
fps: f64,
duration: Option<f64>,
@@ -159,6 +162,29 @@ impl AnimTargets {
}
_ => return Err("--shard and --shards must be given together".into()),
};
if let Some(s) = cli.to_color_scale {
if !(s.is_finite() && s > 0.0) {
return Err(format!("invalid --to-color-scale: {s}"));
}
if cli.auto_color_scale {
return Err(
"--to-color-scale can't be combined with --auto-color-scale \
(the auto fit sets the scale on every frame)"
.into(),
);
}
}
let to_color_scale_at = cli
.to_color_scale_at
.as_deref()
.map(|s| {
s.parse::<Scale>()
.map_err(|_| format!("invalid --to-color-scale-at: {s}"))
})
.transpose()?;
if to_color_scale_at.is_some() && cli.to_color_scale.is_none() {
return Err("--to-color-scale-at needs --to-color-scale".into());
}
Ok(Self {
to_view: cli.to_view.clone(),
to_share: cli.to_share.clone(),
@@ -171,6 +197,8 @@ impl AnimTargets {
to_kind: cli.to_kind.map(Into::into),
to_yaw: cli.to_yaw,
to_pitch: cli.to_pitch,
to_color_scale: cli.to_color_scale,
to_color_scale_at,
frames: cli.frames,
fps: cli.fps,
duration: cli.duration,
@@ -192,15 +220,17 @@ impl AnimTargets {
|| self.to_kind.is_some()
|| self.to_yaw.is_some()
|| self.to_pitch.is_some()
|| self.to_color_scale.is_some()
}
}
/// Render a sequence of frames interpolating from the app's current (start)
/// state to `targets`, for feeding into ffmpeg: the camera, iteration count,
/// per-kind constants (c, p, λ, complex power) and, through a kind morph,
/// the iteration formula, and the 3D camera angles. Everything else (colors, ...) stays fixed at
/// whatever `apply_cli` set up for the start. With `--export-path -`, frames
/// are streamed in order to stdout as raw RGBA8 (for ffmpeg's `rawvideo`
/// the iteration formula, the 3D camera angles and the colour scale.
/// Everything else (palette, offset, ...) stays fixed at whatever
/// `apply_cli` set up for the start. With `--export-path -`, frames are
/// streamed in order to stdout as raw RGBA8 (for ffmpeg's `rawvideo`
/// demuxer) instead of being written as PNGs.
fn run_animation(
mut app: FractalApp,
@@ -261,6 +291,15 @@ fn run_animation(
let (yaw0, pitch0) = app.camera_angles();
let yaw1 = targets.to_yaw.map_or(yaw0, f32::to_radians);
let pitch1 = targets.to_pitch.map_or(pitch0, f32::to_radians);
let (scale0, ci_lo0) = app.color_fit();
let scale1 = targets.to_color_scale.map(|s| s.clamp(1e-4, 1.0) as f64);
// With `--to-color-scale-at`, the scale follows the zoom depth (log2 of
// the half-height) from the start view's to that one, instead of `t`.
let depth0 = from.half_height.log2();
let depth_at = targets.to_color_scale_at.map(Scale::log2);
if depth_at == Some(depth0) {
return Err("--to-color-scale-at is the start view's depth".into());
}
let stream = export_path.as_deref() == Some(STDOUT_PATH);
let out_dir = export_path.unwrap_or_else(|| format!("frames-{}", unix_timestamp()));
@@ -288,7 +327,9 @@ fn run_animation(
interpolate_f64(from_iterations as f64, to as f64, t).round() as u32,
);
}
app.set_view(interpolate_view(&from, &to, t));
let view = interpolate_view(&from, &to, t);
let depth = view.half_height.log2();
app.set_view(view);
app.set_constants(std::array::from_fn(|k| {
(
interpolate_f64(from_consts[k].0, to_consts[k].0, t),
@@ -300,6 +341,13 @@ fn run_animation(
interpolate_f64(yaw0 as f64, yaw1 as f64, t) as f32,
interpolate_f64(pitch0 as f64, pitch1 as f64, t) as f32,
);
// Geometrically: the useful range spans decades.
if let Some(scale1) = scale1 {
// The zoom is already eased; don't ease its depth again.
let u = depth_at.map_or(t, |at| ((depth - depth0) / (at - depth0)).clamp(0.0, 1.0));
let scale = interpolate_f64((scale0 as f64).ln(), scale1.ln(), u).exp();
app.set_color_fit((scale as f32, ci_lo0));
}
};
// Snapshot every frame's reference-orbit job up front (cheap: just the