use crate::effects::ImageEffect;
use crate::{
ColorRGB, Complex, NEON_PALETTES, NeonColor, get_random_integer, process_rows_parallel_scoped,
};
use image::RgbImage;
pub const STAR_RANGE: [usize; 2] = [60, 120];
pub struct Star {
pub position: Complex,
pub radius: f32,
pub intensity: f32,
pub color_palette: NeonColor,
}
pub struct StarfieldGenerator {
pub stars: Vec<Star>,
}
impl ImageEffect for StarfieldGenerator {
fn apply(&self, rgb_img: &mut RgbImage) {
let contrast_color = ColorRGB::new(0.64, 0.75, 0.85);
process_rows_parallel_scoped(rgb_img, |y, row_data| {
let y_f = y as f32;
let mut active_stars = Vec::with_capacity(16);
for star in &self.stars {
let dy = star.position.im as f32 - y_f;
let limit = star.radius * 2.0;
if dy.abs() < limit {
let dy_sq = dy * dy;
let star_radius_sq = star.radius * star.radius;
active_stars.push((star, dy_sq, star_radius_sq));
}
}
for (x, pixel_slice) in row_data.chunks_exact_mut(3).enumerate() {
let x_f = x as f32;
let mut r_contrib = 0.0;
let mut g_contrib = 0.0;
let mut b_contrib = 0.0;
let mut total_alpha = 0.0;
for &(star, dy_sq, star_radius_sq) in &active_stars {
if let Some((r, g, b, alpha)) = Self::calculate_star_influence(
star,
x_f,
dy_sq,
star_radius_sq,
contrast_color,
) {
r_contrib += r;
g_contrib += g;
b_contrib += b;
total_alpha += alpha;
}
}
if total_alpha > 0.001 {
let mut pixel_color = ColorRGB::from_slice(pixel_slice);
let alpha_clamp = total_alpha.min(0.95);
let fg_color = ColorRGB::new(
r_contrib / total_alpha,
g_contrib / total_alpha,
b_contrib / total_alpha,
);
let bg_linear = pixel_color.squared();
let fg_linear = fg_color.squared();
let blended_linear = bg_linear.lerp(fg_linear, alpha_clamp);
pixel_color = blended_linear.sqrt();
pixel_color.write_to_slice(pixel_slice);
}
}
});
}
fn info(&self) -> String {
format!("overlay ({} stars)", self.stars.len())
}
}
impl StarfieldGenerator {
pub fn random(monitor: &crate::Monitor) -> Self {
let width = monitor.resolution.width as u32;
let height = monitor.resolution.height as u32;
let count = get_random_integer(STAR_RANGE[0], STAR_RANGE[1]);
Self::new(count, width, height)
}
pub fn new(count: usize, width: u32, height: u32) -> Self {
let mut stars = Vec::with_capacity(count);
for _ in 0..count {
let x: f64 = get_random_integer(0, width);
let y: f64 = get_random_integer(0, height);
let radius: f32 = get_random_integer(5, 45);
let intensity = get_random_integer::<_, f32>(30, 95) / 100.0;
let p_idx: usize = get_random_integer(0, NEON_PALETTES.len() - 1);
let color_palette = NEON_PALETTES[p_idx];
stars.push(Star {
position: Complex::new(x, y),
radius,
intensity,
color_palette,
});
}
Self { stars }
}
#[inline(always)]
fn calculate_star_influence(
star: &Star,
x_f: f32,
dy_sq: f32,
star_radius_sq: f32,
contrast_color: ColorRGB,
) -> Option<(f32, f32, f32, f32)> {
let dx = star.position.re as f32 - x_f;
let dist_sq = dx * dx + dy_sq;
if dist_sq < star_radius_sq * 4.0 {
let factor = (-dist_sq / (2.0 * star_radius_sq)).exp();
let alpha = factor * star.intensity;
let star_rgb = star.color_palette.color_rgb;
let blended = star_rgb.lerp(contrast_color, 0.25).scale(alpha);
Some((blended.red, blended.green, blended.blue, alpha))
} else {
None
}
}
}
#[cfg(test)]
mod tests_star {
use super::*;
use crate::core::Monitor;
#[test]
fn test_star_generation() {
let monitor = Monitor::default();
let starfield = StarfieldGenerator::random(&monitor);
assert!(!starfield.stars.is_empty());
}
}