pub fn stem_darkening_amount(ppem: f32) -> f32 {
(0.4375 - 0.0625 * ppem).clamp(0.0, 0.5)
}
pub fn apply_stem_darkening(coverage: &mut [f32], amount: f32) {
for v in coverage.iter_mut() {
*v = (*v + amount * (1.0 - *v)).clamp(0.0, 1.0);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn decreasing_with_ppem() {
assert!(stem_darkening_amount(6.0) > stem_darkening_amount(7.0));
assert_eq!(stem_darkening_amount(7.0), 0.0);
assert_eq!(stem_darkening_amount(32.0), 0.0);
}
#[test]
fn boosts_coverage() {
let mut cov = vec![0.5f32];
let before = cov[0];
apply_stem_darkening(&mut cov, 0.1);
assert!(cov[0] > before);
}
#[test]
fn clamps_coverage_to_one() {
let mut cov = vec![1.0f32];
apply_stem_darkening(&mut cov, 0.5);
assert_eq!(cov[0], 1.0);
}
#[test]
fn zero_amount_is_noop() {
let mut cov = vec![0.5f32, 0.3f32];
let before = cov.clone();
apply_stem_darkening(&mut cov, 0.0);
assert_eq!(cov, before);
}
}