Fix palette blend modes, add unit tests
CI / cargo fmt (push) Successful in 27s
CI / cargo test (push) Successful in 13m16s
CI / cargo test (GPU) (push) Successful in 12m53s

This commit is contained in:
2026-07-03 16:18:01 -04:00
committed by bspeice
parent 5bd325d0ea
commit 0d9d2b693e
+54 -14
View File
@@ -3,7 +3,7 @@
//! Map points from the IFS coordinate system to pixel coordinates. This is a lossy transformation. //! Map points from the IFS coordinate system to pixel coordinates. This is a lossy transformation.
use bytemuck::{Pod, Zeroable}; use bytemuck::{Pod, Zeroable};
use glam::{Affine2, IVec2, UVec2, Vec2, Vec4, Vec4Swizzles, vec2}; use glam::{Affine2, IVec2, UVec2, Vec2, Vec4, Vec4Swizzles, vec2};
use libm::{ceilf, floorf, powf}; use libm::{floorf, powf};
/// Blending modes for mapping IFS color values (which are on a scale `[0, 1]`) /// Blending modes for mapping IFS color values (which are on a scale `[0, 1]`)
/// to RGBA colors. /// to RGBA colors.
@@ -26,15 +26,15 @@ impl Default for BlendMode {
impl BlendMode { impl BlendMode {
/// Map an IFS color value to RGBA color from the provided palette. /// Map an IFS color value to RGBA color from the provided palette.
pub fn ifs_to_rgb(&self, color: f32, palette: &[Vec4]) -> Vec4 { pub fn ifs_to_rgb(&self, color: f32, palette: &[Vec4]) -> Vec4 {
let palette_index = color * palette.len() as f32; let colors_m_one = palette.len() - 1;
let palette_index_lower = floorf(palette_index) as usize; let period = 1.0 / colors_m_one as f32;
let palette_index_upper = ceilf(palette_index) as usize; let index_lower = floorf(color / period) as usize;
let index_upper = (index_lower + 1).clamp(0, colors_m_one);
let rem = color % period / period;
match self { match self {
BlendMode::Linear => { BlendMode::Linear => palette[index_lower].lerp(palette[index_upper], rem),
(palette[palette_index_lower] + palette[palette_index_upper]) / 2.0 BlendMode::Step => palette[index_lower],
}
BlendMode::Step => palette[palette_index_lower],
} }
} }
} }
@@ -161,12 +161,52 @@ pub mod entry {
#[cfg(test)] #[cfg(test)]
mod test { mod test {
use crate::camera::Camera; use crate::camera::{BlendMode, Camera};
use glam::{Affine2, Vec2, ivec2, uvec2, vec2}; use glam::{Affine2, Vec2, Vec4, ivec2, uvec2, vec2};
use libm::powf; use libm::powf;
fn vec4s(value: f32) -> Vec4 {
Vec4::splat(value)
}
#[test] #[test]
fn manual_camera() { fn blend_linear() {
let ifs_to_rgb = |color, palette| BlendMode::Linear.ifs_to_rgb(color, palette);
let palette = &[vec4s(0.0), vec4s(1.0)];
assert_eq!(ifs_to_rgb(0.0, palette), vec4s(0.0));
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(0.5));
assert_eq!(ifs_to_rgb(1.0, palette), vec4s(1.0));
let palette = &[vec4s(1.0), vec4s(2.0), vec4s(3.0)];
assert_eq!(ifs_to_rgb(0.0, palette), vec4s(1.0));
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(2.0));
assert_eq!(ifs_to_rgb(1.0, palette), vec4s(3.0));
let palette = &[vec4s(1.0), vec4s(2.0), vec4s(3.0), vec4s(4.0)];
assert_eq!(ifs_to_rgb(0.0, palette), vec4s(1.0));
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(2.5));
assert_eq!(ifs_to_rgb(1.0, palette), vec4s(4.0));
}
#[test]
fn blend_step() {
let ifs_to_rgb = |color, palette| BlendMode::Step.ifs_to_rgb(color, palette);
let palette = &[vec4s(0.0), vec4s(1.0)];
assert_eq!(ifs_to_rgb(0.5, palette), vec4s(0.0));
let palette = &[vec4s(1.0), vec4s(2.0), vec4s(3.0)];
assert_eq!(ifs_to_rgb(0.0, palette), palette[0]);
assert_eq!(ifs_to_rgb(0.25, palette), palette[0]);
assert_eq!(ifs_to_rgb(0.4, palette), palette[0]);
assert_eq!(ifs_to_rgb(0.5, palette), palette[1]);
assert_eq!(ifs_to_rgb(0.7, palette), palette[1]);
assert_eq!(ifs_to_rgb(1.0, palette), palette[2]);
}
#[test]
fn camera_manual() {
let starting_point = vec2(1.0, 1.0); let starting_point = vec2(1.0, 1.0);
// Move the origin; points move right and up by one unit, giving us (2.0, 2.0) // Move the origin; points move right and up by one unit, giving us (2.0, 2.0)
@@ -204,7 +244,7 @@ mod test {
} }
#[test] #[test]
fn point_outside_camera() { fn camera_point_outside_image() {
// Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2] // Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2]
let camera = Camera::new( let camera = Camera::new(
uvec2(1000, 1000), uvec2(1000, 1000),
@@ -219,7 +259,7 @@ mod test {
} }
#[test] #[test]
fn point_outside_camera_negative() { fn camera_point_outside_image_negative() {
// Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2] // Scale 250 for an image 1000 x 1000 gives an effective range of [-2, 2]
let camera = Camera::new( let camera = Camera::new(
uvec2(1000, 1000), uvec2(1000, 1000),
@@ -234,7 +274,7 @@ mod test {
} }
#[test] #[test]
fn aspect_ratio() { fn camera_aspect_ratio() {
// Scale 100 for an image 1600 x 900 gives an effective X range of [-8, 8], // Scale 100 for an image 1600 x 900 gives an effective X range of [-8, 8],
// and effective Y range of [-4.5, 4.5] // and effective Y range of [-4.5, 4.5]
let camera = Camera::new( let camera = Camera::new(