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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Mandelbrot Set fractal generator overlay.
//!
//! This module provides the implementation of the Mandelbrot Set fractal renderer,
//! using pre-tuned coordinate presets. It generates mathematical overlays
//! dynamically fitted to display proportions and blends them using high-definition
//! neon curves, chromatic dual-tone borders, and 3D parallax shadow depths.

use std::cmp::Ordering;

use crate::{
    Complex, FractalPreset, ImageEffect, MAX_ITERATIONS, MIN_ITERATIONS, NEON_PALETTES, NeonColor,
    ProceduralEffect, ROTATION_STEPS, Viewport, ViewportSpecs, color_distance_estimator,
    compute_escape_iterations, get_random_integer, get_rotation_phasors, render_fractal_parallel,
};
use image::RgbImage;
use rayon::prelude::*;

/// A procedural generator for rendering Mandelbrot Set fractals onto desktop backgrounds.
pub struct MandelbrotGenerator {
    /// The active coordinate preset, enclosing the center points and metadata.
    pub preset: FractalPreset,
    /// 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 MandelbrotGenerator {
    /// Returns the default fallback instance of the Mandelbrot Set generator.
    fn default() -> Self {
        Self {
            preset: FractalPreset {
                center: Complex::new(-0.56226, 0.64273),
                fractal_name: "Feathered Filament Cascades",
                effect_name: ProceduralEffect::Mandelbrot,
            },
            scan_iterations: get_random_integer(MIN_ITERATIONS, MAX_ITERATIONS),
            color_palette: NEON_PALETTES[5],
            zoom: 0.0025,
            rotation: Complex::one(),
        }
    }
}

impl ImageEffect for MandelbrotGenerator {
    /// Applies the Mandelbrot Set procedural overlay in-place directly to a mutable image buffer.
    fn apply(&self, rgb_img: &mut RgbImage) {
        let center = self.preset.center;
        let scan_iterations = self.scan_iterations;
        let color_palette = self.color_palette;

        render_fractal_parallel(
            rgb_img,
            self.zoom,
            self.rotation,
            center,
            false, // is_julia = false
            |z_init, scale| {
                let (i, z, dz) = compute_escape_iterations(
                    ProceduralEffect::Mandelbrot,
                    z_init,
                    center,
                    scan_iterations,
                );

                color_distance_estimator(i, scan_iterations, z, dz, scale, color_palette)
            },
        );
    }

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

impl MandelbrotGenerator {
    /// Evaluates the branch direction phasor `u` of the local fractal branch
    /// around the initial center.
    ///
    /// Uses circular complex coordinate rings to detect boundary gradients.
    fn find_branch_phasor(center: Complex, search_radius: f64, scan_iterations: u32) -> Complex {
        let mut best_phasor = Complex::one();
        let mut max_boundary_score = -1.0;

        // Sample 16 angular directions using rotation phasors to respect DRY
        for phasor in get_rotation_phasors(ROTATION_STEPS) {
            // Sample points along this direction to evaluate details
            let mut total_variation = 0.0;
            let mut prev_i = 0;

            for k in 1..=4 {
                // Using 4 steps distributes the samples slightly more densely
                let sample_point = center + phasor * (search_radius * (k as f64) * 0.25);
                let (i, _, _) = compute_escape_iterations(
                    ProceduralEffect::Mandelbrot,
                    sample_point,
                    center,
                    scan_iterations,
                );

                if k > 1 {
                    total_variation += (i as f32 - prev_i as f32).abs();
                }
                prev_i = i;
            }

            if total_variation > max_boundary_score {
                max_boundary_score = total_variation;
                best_phasor = phasor;
            }
        }

        best_phasor
    }

    /// Performs Locked Interior Grid Alignment (LIGA) to find and focus on the
    /// fourth-level nested bulb (circulo_4) along the branch direction phasor.
    fn locked_interior_grid_alignment(
        center: Complex,
        phasor: Complex,
        search_radius: f64,
        scan_iterations: u32,
    ) -> Complex {
        let steps = 64;
        let mut interior_segments = Vec::new();
        let mut in_interior = false;
        let mut segment_start = 0;

        // Scan along the 1D path c(t) = center + t * phasor
        for step in 0..steps {
            let t = -search_radius + (step as f64 / (steps - 1) as f64) * (2.0 * search_radius);
            let test_point = center + phasor * t;

            let (i, _, _) = compute_escape_iterations(
                ProceduralEffect::Mandelbrot,
                test_point,
                center,
                scan_iterations,
            );

            // An interior point does not escape
            let is_interior = i >= scan_iterations;

            if is_interior && !in_interior {
                in_interior = true;
                segment_start = step;
            } else if !is_interior && in_interior {
                in_interior = false;
                interior_segments.push((segment_start, step - 1));
            }
        }
        if in_interior {
            interior_segments.push((segment_start, steps - 1));
        }

        // Target the fourth nested stable segment (circulo_4).
        // Fall back to the last available segment if there are fewer than 4.
        let target_segment = if interior_segments.len() >= 4 {
            Some(interior_segments[3])
        } else {
            interior_segments.last().cloned()
        };

        if let Some((start_idx, end_idx)) = target_segment {
            let mid_step = (start_idx + end_idx) as f64 / 2.0;
            let t_mid = -search_radius + (mid_step / (steps - 1) as f64) * (2.0 * search_radius);
            center + phasor * t_mid
        } else {
            center
        }
    }

    /// Helper method to compute the Shannon Entropy of a given viewport configuration.
    fn calculate_entropy(
        center: Complex,
        zoom: f64,
        rotation: Complex,
        scan_iterations: u32,
        width: u32,
        height: u32,
    ) -> f64 {
        let grid_size = 64; // High-density grid dimension for complexity sampling
        let mut histogram = vec![0; scan_iterations as usize + 1];

        let specs = ViewportSpecs {
            center,
            zoom,
            rotation,
            is_julia: false,
        };
        let viewport = Viewport::new(width as f64, height as f64, &specs);

        let step_x = (width as f64) / (grid_size as f64);
        let step_y = (height as f64) / (grid_size as f64);

        // Sample escape iterations across the 2D evaluation grid
        for gy in 0..grid_size {
            let y_f = (gy as f64) * step_y;
            for gx in 0..grid_size {
                let x_f = (gx as f64) * step_x;
                let z_init = viewport.map(x_f, y_f);

                let (i, _, _) = compute_escape_iterations(
                    ProceduralEffect::Mandelbrot,
                    z_init,
                    center,
                    scan_iterations,
                );

                if (i as usize) < histogram.len() {
                    histogram[i as usize] += 1;
                }
            }
        }

        // Calculate Shannon Entropy
        let total_samples = (grid_size * grid_size) as f64;
        let mut entropy: f64 = 0.0;

        for &count in &histogram {
            if count > 0 {
                let p = (count as f64) / total_samples;
                entropy -= p * p.ln();
            }
        }

        entropy
    }

    /// Generates an independent, highly optimized Mandelbrot Set configuration for a single monitor.
    pub fn random(monitor: &crate::Monitor) -> Self {
        let width = monitor.resolution.width as u32;
        let height = monitor.resolution.height as u32;

        let presets = [
            FractalPreset {
                center: Complex::new(-0.8115, 0.2014),
                fractal_name: "Tendril Valley Filaments",
                effect_name: ProceduralEffect::Mandelbrot,
            },
            FractalPreset {
                center: Complex::new(-0.156, 1.033),
                fractal_name: "Dreadlock Valley Basin",
                effect_name: ProceduralEffect::Mandelbrot,
            },
            FractalPreset {
                center: Complex::new(-0.38, 0.66),
                fractal_name: "Starburst Star Valley",
                effect_name: ProceduralEffect::Mandelbrot,
            },
            FractalPreset {
                center: Complex::new(-0.56226, 0.64273),
                fractal_name: "Feathered Filament Cascades",
                effect_name: ProceduralEffect::Mandelbrot,
            },
            FractalPreset {
                center: Complex::new(-0.77568377, 0.13646737),
                fractal_name: "Deep Seahorse Tail Spiral",
                effect_name: ProceduralEffect::Mandelbrot,
            },
            FractalPreset {
                center: Complex::new(-1.45, 0.0),
                fractal_name: "West Needle Crown Filaments",
                effect_name: ProceduralEffect::Mandelbrot,
            },
            FractalPreset {
                center: Complex::new(-1.25, 0.05),
                fractal_name: "Gothic Archway Scepters",
                effect_name: ProceduralEffect::Mandelbrot,
            },
            FractalPreset {
                center: Complex::new(-0.55, 0.62),
                fractal_name: "Pentagonal Star Valley",
                effect_name: ProceduralEffect::Mandelbrot,
            },
            FractalPreset {
                center: Complex::new(-1.625, 0.0),
                fractal_name: "Bi-Directional Filament",
                effect_name: ProceduralEffect::Mandelbrot,
            },
        ];

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

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

        let rotations_count = ROTATION_STEPS;
        let zooms_count = get_random_integer(30, 80);
        let precision: usize = get_random_integer(2, 8);

        let rotation_phasors: Vec<Complex> = get_rotation_phasors(rotations_count).collect();
        let mut candidates = Vec::with_capacity(zooms_count * rotations_count);

        for z_idx in 0..zooms_count {
            let group = z_idx / 10;
            let offset = z_idx % 10;

            let exponent = precision.saturating_sub(group).max(1) as i32;
            let multiplier = (offset + 1 + (group != 0) as usize) as f64;
            let base_zoom = multiplier * 10.0_f64.powi(-exponent) + 1e-8;

            candidates.extend((0..rotations_count).map(|r_idx| (base_zoom, r_idx)));
        }

        let (best_base_zoom, best_rotation, _best_entropy) = candidates
            .par_iter()
            .map(|&(base_zoom, r_idx)| {
                let aspect_ratio = (width as f64) / (height as f64);
                let adjusted_zoom = if aspect_ratio > 1.0 {
                    base_zoom * aspect_ratio.sqrt()
                } else {
                    base_zoom
                };

                let rotation = rotation_phasors[r_idx];
                let entropy = Self::calculate_entropy(
                    selected_preset.center,
                    adjusted_zoom,
                    rotation,
                    scan_iterations,
                    width,
                    height,
                );
                (base_zoom, rotation, entropy)
            })
            .max_by(|a, b| a.2.partial_cmp(&b.2).unwrap_or(Ordering::Equal))
            .unwrap_or((0.0002, Complex::one(), 0.0));

        let mut mandelbrot = Self {
            preset: selected_preset,
            scan_iterations,
            color_palette,
            zoom: best_base_zoom,
            rotation: best_rotation,
        };

        mandelbrot.optimize_fit(width, height);
        mandelbrot.dynamic_autofocus(width, height);
        mandelbrot
    }

    /// Dynamically adjusts the camera center and zoom to focus on the local region of maximum
    /// visual complexity near the selected mathematical preset.
    pub fn dynamic_autofocus(&mut self, width: u32, height: u32) {
        let search_radius = self.zoom * 0.25;
        let branch_phasor =
            Self::find_branch_phasor(self.preset.center, search_radius, self.scan_iterations);

        let aligned_center = Self::locked_interior_grid_alignment(
            self.preset.center,
            branch_phasor,
            search_radius,
            self.scan_iterations,
        );

        self.preset.center = aligned_center;

        let best_entropy = Self::calculate_entropy(
            self.preset.center,
            self.zoom,
            self.rotation,
            self.scan_iterations,
            width,
            height,
        );

        let climb_radius = self.zoom * 0.05;
        let rotations = ROTATION_STEPS;
        let search_directions: Vec<Complex> = std::iter::once(Complex::zero())
            .chain(get_rotation_phasors(rotations).map(|phasor| phasor * climb_radius))
            .collect();

        let (best_center, _max_entropy) = search_directions
            .par_iter()
            .map(|&offset| {
                let candidate_center = self.preset.center + offset;
                let entropy = Self::calculate_entropy(
                    candidate_center,
                    self.zoom,
                    self.rotation,
                    self.scan_iterations,
                    width,
                    height,
                );
                (candidate_center, entropy)
            })
            .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap_or(Ordering::Equal))
            .unwrap_or((self.preset.center, best_entropy));

        self.preset.center = best_center;

        let scale = self.zoom / (width.min(height) as f64);
        let lod_iterations = (150.0 + 45.0 * (1.0 / scale).ln()) as u32;
        self.scan_iterations = lod_iterations.clamp(MIN_ITERATIONS, MAX_ITERATIONS);
    }

    /// Automatically scales and reframes the viewport based on the screen's aspect ratio.
    pub fn optimize_fit(&mut self, width: u32, height: u32) {
        let aspect_ratio = (width as f64) / (height as f64);
        if aspect_ratio > 1.0 {
            self.zoom *= aspect_ratio.sqrt();
        }
    }
}

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

    #[test]
    fn test_mandelbrot_generation_random() {
        let monitor = Monitor::default();
        let mandelbrot = MandelbrotGenerator::random(&monitor);
        assert!(mandelbrot.zoom > 0.0);
        assert_eq!(mandelbrot.preset.effect_name, ProceduralEffect::Mandelbrot);
    }
}