use crate::types::Color;
use super::paint::{ ColorStop, GradientSpace };
pub const LUT_SAMPLES: usize = 512;
pub const LUT_DOMAIN: ( f32, f32 ) = ( -2.0, 3.0 );
#[ inline ]
pub fn srgb_to_linear( x: f32 ) -> f32
{
if x <= 0.04045 { x / 12.92 } else { ((x + 0.055) / 1.055).powf( 2.4 ) }
}
#[ inline ]
pub fn linear_to_srgb( x: f32 ) -> f32
{
if x <= 0.0031308 { x * 12.92 } else { 1.055 * x.powf( 1.0 / 2.4 ) - 0.055 }
}
pub fn sample_stops( stops: &[ColorStop], t: f32, space: GradientSpace ) -> Color
{
if stops.is_empty() { return Color::TRANSPARENT; }
if stops.len() == 1 { return stops[0].color; }
let mut sorted: Vec<&ColorStop> = stops.iter().collect();
sorted.sort_by( |a, b|
a.position.partial_cmp( &b.position ).unwrap_or( std::cmp::Ordering::Equal )
);
let n = sorted.len();
let ( a, b ) = if t <= sorted[0].position
{
( sorted[0], sorted[1] )
}
else if t >= sorted[n - 1].position
{
( sorted[n - 2], sorted[n - 1] )
}
else
{
let mut pair = ( sorted[0], sorted[1] );
for win in sorted.windows( 2 )
{
if t >= win[0].position && t <= win[1].position
{
pair = ( win[0], win[1] );
break;
}
}
pair
};
let dt = b.position - a.position;
let u = if dt.abs() < 1e-6 { 0.0 } else { ( t - a.position ) / dt };
mix_colors( a.color, b.color, u, space )
}
fn mix_colors( a: Color, b: Color, u: f32, space: GradientSpace ) -> Color
{
let alpha = a.a + ( b.a - a.a ) * u;
match space
{
GradientSpace::Srgb => Color
{
r: a.r + ( b.r - a.r ) * u,
g: a.g + ( b.g - a.g ) * u,
b: a.b + ( b.b - a.b ) * u,
a: alpha,
},
GradientSpace::LinearRgb => mix_linear( a, b, u, alpha ),
GradientSpace::Oklab => mix_linear( a, b, u, alpha ),
}
}
fn mix_linear( a: Color, b: Color, u: f32, alpha: f32 ) -> Color
{
let ar = srgb_to_linear( a.r );
let ag = srgb_to_linear( a.g );
let ab = srgb_to_linear( a.b );
let br = srgb_to_linear( b.r );
let bg = srgb_to_linear( b.g );
let bb = srgb_to_linear( b.b );
let r = linear_to_srgb( ar + ( br - ar ) * u );
let g = linear_to_srgb( ag + ( bg - ag ) * u );
let b = linear_to_srgb( ab + ( bb - ab ) * u );
Color { r, g, b, a: alpha }
}
pub fn build_lut_bytes( stops: &[ColorStop], space: GradientSpace ) -> Vec<u8>
{
let ( t0, t1 ) = LUT_DOMAIN;
let n = LUT_SAMPLES;
let mut out = Vec::with_capacity( n * 4 );
for i in 0..n
{
let t = t0 + ( t1 - t0 ) * ( i as f32 / ( n - 1 ) as f32 );
let c = sample_stops( stops, t, space );
out.push( ( c.r.clamp( 0.0, 1.0 ) * 255.0 + 0.5 ) as u8 );
out.push( ( c.g.clamp( 0.0, 1.0 ) * 255.0 + 0.5 ) as u8 );
out.push( ( c.b.clamp( 0.0, 1.0 ) * 255.0 + 0.5 ) as u8 );
out.push( ( c.a.clamp( 0.0, 1.0 ) * 255.0 + 0.5 ) as u8 );
}
out
}
#[ cfg( test ) ]
mod tests
{
use super::*;
fn approx( a: f32, b: f32 ) -> bool { ( a - b ).abs() < 2e-2 }
#[ test ]
fn srgb_linear_roundtrip_is_stable()
{
for &v in &[ 0.0, 0.04, 0.1, 0.25, 0.5, 0.75, 1.0 ]
{
let back = linear_to_srgb( srgb_to_linear( v ) );
assert!( ( v - back ).abs() < 1e-5, "roundtrip {} -> {}", v, back );
}
}
#[ test ]
fn sample_at_exact_stop_returns_that_stop()
{
let stops = vec!
[
ColorStop { position: 0.0, color: Color::WHITE },
ColorStop { position: 1.0, color: Color::BLACK },
];
let a = sample_stops( &stops, 0.0, GradientSpace::Srgb );
let b = sample_stops( &stops, 1.0, GradientSpace::Srgb );
assert_eq!( a, Color::WHITE );
assert_eq!( b, Color::BLACK );
}
#[ test ]
fn sample_midpoint_srgb_is_halfway()
{
let stops = vec!
[
ColorStop { position: 0.0, color: Color::rgb( 0.0, 0.0, 0.0 ) },
ColorStop { position: 1.0, color: Color::rgb( 1.0, 1.0, 1.0 ) },
];
let m = sample_stops( &stops, 0.5, GradientSpace::Srgb );
assert!( approx( m.r, 0.5 ) && approx( m.g, 0.5 ) && approx( m.b, 0.5 ) );
}
#[ test ]
fn sample_midpoint_linear_rgb_is_brighter_than_srgb()
{
let stops = vec!
[
ColorStop { position: 0.0, color: Color::rgb( 0.0, 0.0, 0.0 ) },
ColorStop { position: 1.0, color: Color::rgb( 1.0, 1.0, 1.0 ) },
];
let m_srgb = sample_stops( &stops, 0.5, GradientSpace::Srgb );
let m_linear = sample_stops( &stops, 0.5, GradientSpace::LinearRgb );
assert!( m_linear.r > m_srgb.r, "linear {:?} should be brighter than srgb {:?}", m_linear, m_srgb );
}
#[ test ]
fn extrapolation_below_first_stop_continues_slope()
{
let stops = vec!
[
ColorStop { position: 0.0, color: Color::WHITE },
ColorStop { position: 1.0, color: Color::BLACK },
];
let c = sample_stops( &stops, -1.0, GradientSpace::Srgb );
assert!( c.r > 1.0, "extrapolation should exceed 1.0 before clamp: {}", c.r );
}
#[ test ]
fn extrapolation_above_last_stop_continues_slope()
{
let stops = vec!
[
ColorStop { position: 0.0, color: Color::rgb( 0.2, 0.2, 0.2 ) },
ColorStop { position: 1.0, color: Color::rgb( 0.8, 0.8, 0.8 ) },
];
let c = sample_stops( &stops, 2.0, GradientSpace::Srgb );
assert!( c.r > 1.0, "extrapolation should exceed 1.0: {}", c.r );
}
#[ test ]
fn alpha_mixes_linearly_across_spaces()
{
let stops = vec!
[
ColorStop { position: 0.0, color: Color::rgba( 1.0, 0.0, 0.0, 1.0 ) },
ColorStop { position: 1.0, color: Color::rgba( 1.0, 0.0, 0.0, 0.0 ) },
];
for space in [ GradientSpace::Srgb, GradientSpace::LinearRgb, GradientSpace::Oklab ]
{
let m = sample_stops( &stops, 0.5, space );
assert!( approx( m.a, 0.5 ), "alpha should mix linearly in {:?}: got {}", space, m.a );
}
}
#[ test ]
fn unsorted_stops_are_handled()
{
let stops = vec!
[
ColorStop { position: 1.0, color: Color::BLACK },
ColorStop { position: 0.0, color: Color::WHITE },
];
let a = sample_stops( &stops, 0.0, GradientSpace::Srgb );
let b = sample_stops( &stops, 1.0, GradientSpace::Srgb );
assert_eq!( a, Color::WHITE );
assert_eq!( b, Color::BLACK );
}
#[ test ]
fn build_lut_has_expected_length()
{
let stops = vec!
[
ColorStop { position: 0.0, color: Color::WHITE },
ColorStop { position: 1.0, color: Color::BLACK },
];
let bytes = build_lut_bytes( &stops, GradientSpace::LinearRgb );
assert_eq!( bytes.len(), LUT_SAMPLES * 4 );
}
#[ test ]
fn build_lut_clips_extrapolation_to_u8_range()
{
let stops = vec!
[
ColorStop { position: 0.0, color: Color::WHITE },
ColorStop { position: 1.0, color: Color::BLACK },
];
let bytes = build_lut_bytes( &stops, GradientSpace::Srgb );
for b in &bytes { let _ = *b; }
}
}