wallswitch 0.60.9

randomly selects wallpapers for multiple monitors
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Nova Julia liquid fractal generator overlay.
//!
//! This module implements the Nova Julia fractal, a formula combining
//! Newton-Raphson root-finding loops with a dynamic Julia-like perturbation constant.
//! It produces fluid, organic, plume-like structures resembling liquid metal, glowing
//! plasma currents, and detailed biological lattices.

use crate::effects::{
    ComplexEffectsExt, MAX_ITERATIONS, MIN_ITERATIONS, RelaxedEscape, RelaxedViewportConfig,
    optimize_relaxed_viewport, render_fractal_parallel,
};
use crate::{Complex, ImageEffect, NEON_PALETTES, NeonColor, get_random_integer};
use image::RgbImage;

/// Valid operational range for randomized zoom viewport allocation.
const ZOOM_RANGE: [f64; 2] = [1.2, 3.2];

/// Structural parameters governing the Nova fluid dynamics.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NovaPreset {
    /// The integer exponent p in the polynomial formula z^p - 1.
    pub power: u32,
    /// The relaxation factor R represented as a complex scalar or vector coordinate.
    pub r: Complex,
    /// The perturbation constant c added at each iteration step.
    pub c: Complex,
    /// The user-friendly identifier name of the preset.
    pub name: &'static str,
}

/// A procedural generator for rendering Nova Julia fractals onto desktop backgrounds.
pub struct NovaGenerator {
    /// The active parameters defining the shape and behavior of the fractal.
    pub preset: NovaPreset,
    /// The maximum iteration limit for escape-time calculations.
    pub scan_iterations: u32,
    /// The base color palette selected for the neon glow.
    pub color_palette: NeonColor,
    /// The viewport zoom level.
    pub zoom: f64,
    /// The complex phasor representing the viewport rotation.
    pub rotation: Complex,
}

impl Default for NovaGenerator {
    fn default() -> Self {
        Self {
            preset: NovaPreset {
                power: 3,
                r: Complex::new(1.0, 0.0),
                c: Complex::new(0.1, 0.15),
                name: "Liquid Mercury Flow",
            },
            scan_iterations: get_random_integer::<_, u32>(MIN_ITERATIONS / 12, MAX_ITERATIONS / 12)
                .max(50),
            color_palette: NEON_PALETTES[0],
            zoom: 1.8,
            rotation: Complex::one(),
        }
    }
}

impl ImageEffect for NovaGenerator {
    /// Applies the procedural effect in-place to a mutable image buffer.
    fn apply(&self, rgb_img: &mut RgbImage) {
        let scan_iterations = self.scan_iterations;
        let power = self.preset.power;
        let r = self.preset.r;
        let c = self.preset.c;
        let color_palette = self.color_palette;

        let (width, height) = rgb_img.dimensions();
        let w_f = width as f64;
        let h_f = height as f64;
        let aspect_ratio = w_f.max(h_f) / w_f.min(h_f);

        // Dynamically scale the edge fade radius to cover 98% of the maximum viewport extent
        let max_radius = 0.98 * 0.5 * self.zoom * aspect_ratio;

        render_fractal_parallel(
            rgb_img,
            self.zoom,
            self.rotation,
            Complex::new(0.0, 0.0),
            true,
            move |z_init, _scale| {
                let (i, diff_norm, z_final) =
                    compute_nova_escape(z_init, power, r, c, scan_iterations);

                // Leverage ComplexEffectsExt circular_fade on Complex coordinates directly
                let edge_fade = z_init.circular_fade(max_radius as f32, 0.40);

                let escape_state = RelaxedEscape {
                    iterations: i,
                    max_iterations: scan_iterations,
                    diff_norm,
                    z_final,
                };

                // Leverage RelaxedEscape::color method
                escape_state.color(color_palette, edge_fade, (1e-5_f64).ln() as f32, true)
            },
        );
    }

    /// Returns formatting diagnostic information about the active generator.
    fn info(&self) -> String {
        format!(
            "fractal [{}]\n\
            f(z) = z^{} - 1 = 0, where c = {:6.3} {} {:5.3}i (iter = {:4}, zoom = {:.2}), color: {}",
            self.preset.name,
            self.preset.power,
            self.preset.c.re,
            if self.preset.c.im >= 0.0 { "+" } else { "-" },
            self.preset.c.im.abs(),
            self.scan_iterations,
            self.zoom,
            self.color_palette
        )
    }
}

impl NovaGenerator {
    /// Generates a randomized Nova Julia configuration fitted to monitor proportions.
    pub fn random(monitor: &crate::Monitor) -> Self {
        let width = monitor.resolution.width as u32;
        let height = monitor.resolution.height as u32;

        let presets = [
            NovaPreset {
                power: 3,
                r: Complex::new(1.0, 0.0),
                c: Complex::new(0.10, 0.15),
                name: "Liquid Mercury Flow",
            },
            NovaPreset {
                power: 3,
                r: Complex::new(1.0, 0.0),
                c: Complex::new(-0.20, 0.45),
                name: "Cosmic Plasma Flare",
            },
            NovaPreset {
                power: 4,
                r: Complex::new(1.0, 0.0),
                c: Complex::new(0.22, 0.10),
                name: "Ornate Coral Filigree",
            },
            NovaPreset {
                power: 3,
                r: Complex::new(0.9, 0.0),
                c: Complex::new(-0.35, 0.25),
                name: "Nebulous Dust Whispers",
            },
            NovaPreset {
                power: 4,
                r: Complex::new(1.0, 0.0),
                c: Complex::new(-0.10, 0.35),
                name: "Gilded Lace Tapestry",
            },
            NovaPreset {
                power: 5,
                r: Complex::new(1.0, 0.0),
                c: Complex::new(-0.05, 0.55),
                name: "Glacial Frost Lattice",
            },
            NovaPreset {
                power: 3,
                r: Complex::new(1.15, 0.0),
                c: Complex::new(0.0, 0.12),
                name: "Spiritual Mandala Ripple",
            },
            NovaPreset {
                power: 4,
                r: Complex::new(0.8, 0.0),
                c: Complex::new(0.30, -0.20),
                name: "Bioluminescent Spore Nest",
            },
            NovaPreset {
                power: 3,
                r: Complex::new(1.0, 0.0),
                c: Complex::new(0.18, -0.40),
                name: "Abyssal Trench Vines",
            },
            NovaPreset {
                power: 6,
                r: Complex::new(1.0, 0.0),
                c: Complex::new(-0.15, 0.15),
                name: "Hyperdimensional Loom",
            },
            NovaPreset {
                power: 3,
                r: Complex::new(1.0, 0.15),
                c: Complex::new(-0.15, 0.35),
                name: "Gothic Cathedral Rose",
            },
            NovaPreset {
                power: 5,
                r: Complex::new(0.85, 0.25),
                c: Complex::new(0.25, 0.05),
                name: "Quantum Foam Fluctuation",
            },
            NovaPreset {
                power: 4,
                r: Complex::new(1.2, -0.10),
                c: Complex::new(-0.28, -0.28),
                name: "Stellar Nucleosynthesis",
            },
            NovaPreset {
                power: 6,
                r: Complex::new(0.95, 0.05),
                c: Complex::new(0.05, 0.42),
                name: "Emerald Moss Labyrinth",
            },
            NovaPreset {
                power: 7,
                r: Complex::new(1.0, 0.0),
                c: Complex::new(-0.08, 0.38),
                name: "Bismuth Crystal Citadel",
            },
            NovaPreset {
                power: 3,
                r: Complex::new(0.75, -0.30),
                c: Complex::new(0.32, 0.18),
                name: "Astral Jellyfish Canopy",
            },
            NovaPreset {
                power: 4,
                r: Complex::new(1.1, 0.15),
                c: Complex::new(-0.45, 0.10),
                name: "Solar Prominence Loops",
            },
            NovaPreset {
                power: 5,
                r: Complex::new(0.9, -0.20),
                c: Complex::new(-0.12, -0.32),
                name: "Aetheric Ley Line Matrix",
            },
            NovaPreset {
                power: 8,
                r: Complex::new(1.05, 0.10),
                c: Complex::new(0.15, 0.15),
                name: "Phytoplankton Radiance",
            },
            NovaPreset {
                power: 6,
                r: Complex::new(0.8, 0.40),
                c: Complex::new(-0.22, 0.22),
                name: "Chronos Vortex Gear",
            },
            NovaPreset {
                power: 5,
                r: Complex::new(1.0, 0.3),
                c: Complex::new(-0.18, 0.12),
                name: "Aeon Temple Portico",
            },
            NovaPreset {
                power: 8,
                r: Complex::new(0.9, -0.15),
                c: Complex::new(0.20, 0.35),
                name: "Hyperborean Crown",
            },
            NovaPreset {
                power: 3,
                r: Complex::new(1.1, 0.45),
                c: Complex::new(-0.33, -0.05),
                name: "Abyssal Nautilus Shell",
            },
            NovaPreset {
                power: 4,
                r: Complex::new(0.7, 0.5),
                c: Complex::new(0.15, -0.55),
                name: "Spectral Dragon Spine",
            },
            NovaPreset {
                power: 3,
                r: Complex::new(1.3, -0.2),
                c: Complex::new(0.25, 0.25),
                name: "Opalescent Silk Ribbons",
            },
            NovaPreset {
                power: 5,
                r: Complex::new(1.0, -0.4),
                c: Complex::new(-0.42, 0.18),
                name: "Phoenix Heart Nebula",
            },
            NovaPreset {
                power: 7,
                r: Complex::new(0.85, 0.1),
                c: Complex::new(0.30, -0.30),
                name: "Crystalline Geode Valley",
            },
            NovaPreset {
                power: 6,
                r: Complex::new(1.15, -0.3),
                c: Complex::new(-0.02, 0.48),
                name: "Eldritch Eye Lattice",
            },
            NovaPreset {
                power: 4,
                r: Complex::new(0.9, 0.35),
                c: Complex::new(-0.25, 0.30),
                name: "Prismatic Quantum Lattice",
            },
            NovaPreset {
                power: 9,
                r: Complex::new(1.0, 0.25),
                c: Complex::new(0.08, -0.28),
                name: "Void Weaver Spindle",
            },
        ];

        let p_idx: usize = get_random_integer(0, NEON_PALETTES.len() - 1);
        let color_palette = NEON_PALETTES[p_idx];

        let angle_degrees: f64 = get_random_integer(0, 359);
        let radians = angle_degrees.to_radians();

        let preset_idx: usize = get_random_integer(0, presets.len() - 1);
        let selected_preset = presets[preset_idx];

        let mut nova = Self {
            preset: selected_preset,
            scan_iterations: get_random_integer(40, 80),
            color_palette,
            zoom: 1.8,
            rotation: Complex::from_polar(1.0, radians),
        };

        nova.optimize_fit(width, height);
        nova
    }

    /// Automatically aligns viewport dimensions to frame the central fluid core.
    pub fn optimize_fit(&mut self, width: u32, height: u32) {
        let scan_iterations = self.scan_iterations;
        let power = self.preset.power;
        let r = self.preset.r;
        let c = self.preset.c;

        let config = RelaxedViewportConfig {
            width,
            height,
            search_limit: 1.6,
            steps: 64,
            zoom_range: ZOOM_RANGE,
            rand_range: [0.90, 1.35],
            fallback_range: [1.30, 2.80],
        };

        let (zoom, rotation) = optimize_relaxed_viewport(config, self.rotation, |z| {
            let (i, _, _) = compute_nova_escape(z, power, r, c, scan_iterations);
            i > 6 && i < scan_iterations - 2
        });

        self.zoom = zoom;
        self.rotation = rotation;
    }
}

/// Evaluates fluid convergence under the Nova-Julia recurrence relation.
#[inline(always)]
fn compute_nova_escape(
    z_init: Complex,
    power: u32,
    r: Complex,
    c: Complex,
    scan_iterations: u32,
) -> (u32, f64, Complex) {
    let mut z = z_init;
    let mut i = 0;
    let mut diff_norm = 1.0;

    while i < scan_iterations {
        let z_norm_sq = z.abs_sq();
        if z_norm_sq < 1e-6 {
            break;
        }
        if z_norm_sq > 100.0 {
            break;
        }

        let step = r * z.newton_step_term(power);
        let z_next = z - step + c;

        let diff = z_next - z;
        diff_norm = diff.abs_sq();

        if diff_norm < 1e-5 {
            z = z_next;
            break;
        }

        z = z_next;
        i += 1;
    }

    (i, diff_norm, z)
}

#[cfg(test)]
mod tests_nova {
    use super::*;
    use crate::core::Monitor;

    #[test]
    fn test_nova_generation_random() {
        let monitor = Monitor::default();
        let nova = NovaGenerator::random(&monitor);
        assert!(nova.zoom > 0.0);
        assert!(nova.preset.power > 0);
    }
}