#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TextContrast {
pub gamma: f32,
pub contrast: f32,
}
impl TextContrast {
pub const OFF: Self = Self {
gamma: 0.0,
contrast: 0.0,
};
pub const DEFAULT: Self = Self {
gamma: 1.2,
contrast: 0.2,
};
pub const STRONGER: Self = Self {
gamma: 1.0,
contrast: 0.5,
};
pub const STRONGEST: Self = Self {
gamma: 1.0,
contrast: 1.0,
};
pub fn is_off(self) -> bool {
self.gamma <= 0.0
}
pub(crate) fn packed(self) -> u32 {
if self.is_off() {
return 0;
}
let g = ((self.gamma.clamp(0.0, 4.0) / 4.0) * 65535.0).round() as u32;
let g = g.max(1);
let c = (self.contrast.clamp(0.0, 1.0) * 65535.0).round() as u32;
g | (c << 16)
}
fn unpacked(packed: u32) -> Option<(f32, f32)> {
if packed == 0 {
return None;
}
let g = (packed & 0xffff) as f32 / 65535.0 * 4.0;
let c = (packed >> 16) as f32 / 65535.0;
Some((g, c))
}
pub fn corrected_coverage(self, coverage: f32, text_rgb: [f32; 3]) -> f32 {
let Some((gamma, contrast)) = Self::unpacked(self.packed()) else {
return coverage;
};
let coverage = coverage.clamp(0.0, 1.0);
let src_lin = luminance(text_rgb);
let src = srgb_encode(src_lin);
let dst = 1.0 - src;
let dst_lin = srgb_decode(dst);
let dst_g = dst.powf(gamma);
let c = contrast * dst_g;
let a = coverage + (1.0 - coverage) * c * coverage;
if (src - dst).abs() < 1.0 / 256.0 {
return a.clamp(0.0, 1.0);
}
let src_g = src.powf(gamma);
let out = (dst_g + (src_g - dst_g) * a).max(0.0).powf(1.0 / gamma);
((srgb_decode(out) - dst_lin) / (src_lin - dst_lin)).clamp(0.0, 1.0)
}
}
impl Default for TextContrast {
fn default() -> Self {
Self::DEFAULT
}
}
fn luminance([r, g, b]: [f32; 3]) -> f32 {
0.2126 * r + 0.7152 * g + 0.0722 * b
}
fn srgb_encode(x: f32) -> f32 {
let x = x.clamp(0.0, 1.0);
if x <= 0.003_130_8 {
x * 12.92
} else {
1.055 * x.powf(1.0 / 2.4) - 0.055
}
}
fn srgb_decode(x: f32) -> f32 {
let x = x.clamp(0.0, 1.0);
if x <= 0.04045 {
x / 12.92
} else {
((x + 0.055) / 1.055).powf(2.4)
}
}
#[cfg(test)]
mod tests {
use super::*;
const BLACK: [f32; 3] = [0.0, 0.0, 0.0];
const WHITE: [f32; 3] = [1.0, 1.0, 1.0];
fn blended_byte(text: f32, page: f32, alpha: f32) -> u8 {
let lin = srgb_decode(page) + (srgb_decode(text) - srgb_decode(page)) * alpha;
(srgb_encode(lin) * 255.0).round() as u8
}
#[test]
fn off_leaves_coverage_alone() {
for i in 0..=10 {
let a = i as f32 / 10.0;
assert_eq!(TextContrast::OFF.corrected_coverage(a, BLACK), a);
}
}
#[test]
fn empty_and_full_coverage_are_kept() {
for text in [BLACK, WHITE, [0.2, 0.05, 0.6], [0.5, 0.5, 0.5]] {
let t = TextContrast::DEFAULT;
assert!(t.corrected_coverage(0.0, text).abs() < 1e-4, "{text:?}");
assert!(
(t.corrected_coverage(1.0, text) - 1.0).abs() < 1e-4,
"{text:?}"
);
}
}
#[test]
fn gamma_one_blends_black_on_white_as_srgb_values() {
let t = TextContrast {
gamma: 1.0,
contrast: 0.0,
};
assert_eq!(blended_byte(0.0, 1.0, 0.5), 188);
let a = t.corrected_coverage(0.5, BLACK);
let byte = blended_byte(0.0, 1.0, a);
assert!((127..=129).contains(&byte), "got {byte}");
}
#[test]
fn gamma_one_blends_white_on_black_as_srgb_values() {
let t = TextContrast {
gamma: 1.0,
contrast: 0.0,
};
let a = t.corrected_coverage(0.5, WHITE);
let byte = blended_byte(1.0, 0.0, a);
assert!((127..=129).contains(&byte), "got {byte}");
}
#[test]
fn dark_text_edges_darken_and_light_text_edges_lighten() {
let t = TextContrast::DEFAULT;
for i in 1..10 {
let a = i as f32 / 10.0;
assert!(t.corrected_coverage(a, BLACK) > a, "dark text at {a}");
assert!(t.corrected_coverage(a, WHITE) < a, "light text at {a}");
}
}
#[test]
fn coverage_stays_monotonic() {
for text in [BLACK, WHITE, [0.3, 0.3, 0.3], [0.05, 0.1, 0.5]] {
let t = TextContrast::DEFAULT;
let mut last = -1.0;
for i in 0..=255 {
let a = t.corrected_coverage(i as f32 / 255.0, text);
assert!(a >= last - 1e-6, "{text:?} at {i}: {a} < {last}");
last = a;
}
}
}
#[test]
fn mid_grey_text_gets_the_contrast_term_only() {
let grey = srgb_decode(0.5);
let t = TextContrast {
gamma: 1.0,
contrast: 0.0,
};
let a = t.corrected_coverage(0.3, [grey; 3]);
assert!((a - 0.3).abs() < 1e-3, "got {a}");
}
#[test]
fn packing_round_trips_within_a_step() {
let t = TextContrast {
gamma: 1.37,
contrast: 0.42,
};
let (g, c) = TextContrast::unpacked(t.packed()).unwrap();
assert!((g - 1.37).abs() < 1e-4 && (c - 0.42).abs() < 1e-4);
assert_eq!(TextContrast::OFF.packed(), 0);
assert_ne!(
TextContrast {
gamma: 1e-9,
contrast: 0.0
}
.packed(),
0
);
}
}