use super::CaptionStyle;
use crate::contrast::contrast_ratio;
#[derive(Debug, Clone, PartialEq)]
pub enum LegibilityIssue {
NoBackgroundOrEdge,
LowContrastWithBox {
ratio: f32,
},
LowContrastWithEdge {
ratio: f32,
},
}
fn rgb(c: [u8; 4]) -> [f32; 3] {
[c[0] as f32 / 255.0, c[1] as f32 / 255.0, c[2] as f32 / 255.0]
}
fn over(top: [u8; 4], under: [f32; 3]) -> [f32; 3] {
let a = top[3] as f32 / 255.0;
let t = rgb(top);
[0, 1, 2].map(|k| t[k] * a + under[k] * (1.0 - a))
}
impl CaptionStyle {
pub fn worst_box_contrast(&self) -> f32 {
[[1.0f32; 3], [0.0; 3]]
.into_iter()
.map(|frame| {
let behind = over(self.background, frame);
contrast_ratio(over(self.color, behind), behind)
})
.fold(f32::MAX, f32::min)
}
pub fn legibility_issues(&self) -> Vec<LegibilityIssue> {
const MIN: f32 = 4.5;
let boxed = self.background[3] > 0;
let outline = self.outline_width >= 1.0 && self.outline_color[3] > 0;
let shadow = self.shadow_color[3] > 0 && (self.shadow_offset[0].abs() + self.shadow_offset[1].abs()) >= 1.0;
let box_ratio = self.worst_box_contrast();
if boxed && box_ratio >= MIN {
return Vec::new();
}
let edge = |c: [u8; 4]| {
[[1.0f32; 3], [0.0; 3]]
.into_iter()
.map(|frame| {
let behind = over(self.background, frame);
contrast_ratio(over(self.color, behind), over(c, behind))
})
.fold(f32::MAX, f32::min)
};
let edge_ratio = [outline.then(|| edge(self.outline_color)), shadow.then(|| edge(self.shadow_color))]
.into_iter()
.flatten()
.fold(0.0f32, f32::max);
if (outline || shadow) && edge_ratio >= MIN {
return Vec::new();
}
let mut issues = Vec::new();
match (boxed, outline || shadow) {
(false, false) => issues.push(LegibilityIssue::NoBackgroundOrEdge),
(true, false) => issues.push(LegibilityIssue::LowContrastWithBox { ratio: box_ratio }),
(false, true) => issues.push(LegibilityIssue::LowContrastWithEdge { ratio: edge_ratio }),
(true, true) => {
issues.push(LegibilityIssue::LowContrastWithBox { ratio: box_ratio });
issues.push(LegibilityIssue::LowContrastWithEdge { ratio: edge_ratio });
}
}
issues
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_default_and_the_classics_are_legible() {
assert_eq!(CaptionStyle::default().legibility_issues(), vec![]);
let boxed = CaptionStyle { outline_width: 0.0, background: [0, 0, 0, 255], ..Default::default() };
assert_eq!(boxed.legibility_issues(), vec![]);
assert!((boxed.worst_box_contrast() - 21.0).abs() < 1e-3);
let edged = CaptionStyle { background: [0; 4], ..Default::default() };
assert_eq!(edged.legibility_issues(), vec![]);
}
#[test]
fn styles_that_vanish_on_some_frames_are_flagged() {
let bare = CaptionStyle { outline_width: 0.0, background: [0; 4], ..Default::default() };
assert_eq!(bare.legibility_issues(), vec![LegibilityIssue::NoBackgroundOrEdge]);
let faint = CaptionStyle { outline_width: 0.0, background: [0, 0, 0, 40], ..Default::default() };
match faint.legibility_issues().as_slice() {
[LegibilityIssue::LowContrastWithBox { ratio }] => assert!(*ratio < 1.5, "{ratio}"),
other => panic!("{other:?}"),
}
let grey = CaptionStyle { background: [0; 4], outline_color: [190, 190, 190, 255], ..Default::default() };
assert!(matches!(grey.legibility_issues().as_slice(), [LegibilityIssue::LowContrastWithEdge { .. }]));
}
}