threers-codec 0.0.10

Pure-Rust media codecs: H.264/HEVC/VP9 encoders, H.264/HEVC/VP9/AV1 decoders, AAC/MP3/Opus/Vorbis/FLAC/WAV decoders, MP4/WebM/Matroska/Ogg, GIF/APNG, captions, and VideoToolbox acceleration on macOS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//! WCAG 2.x's colour contrast: relative luminance and contrast ratio, for
//! the UI's accessibility checks and caption legibility alike.

/// WCAG's relative luminance of an sRGB colour (components `0..=1`).
pub fn relative_luminance(c: [f32; 3]) -> f32 {
    // WCAG 2.x's own constant (0.03928), not the sRGB standard's 0.04045:
    // the two give the same result for 8-bit values.
    let lin = |v: f32| if v <= 0.03928 { v / 12.92 } else { ((v + 0.055) / 1.055).powf(2.4) };
    0.2126 * lin(c[0]) + 0.7152 * lin(c[1]) + 0.0722 * lin(c[2])
}

/// WCAG's contrast ratio between two sRGB colours, 1 to 21.
pub fn contrast_ratio(a: [f32; 3], b: [f32; 3]) -> f32 {
    let (la, lb) = (relative_luminance(a), relative_luminance(b));
    (la.max(lb) + 0.05) / (la.min(lb) + 0.05)
}