fast_srgb8

Function srgb8_to_f32

source
pub const fn srgb8_to_f32(c: u8) -> f32
Expand description

Convert from a 8-bit sRGB component to a linear f32.

This is the inverse of srgb8_to_f32 — and c: u8 is roundtripped through it, as shown below:

use fast_srgb8::{f32_to_srgb8, srgb8_to_f32};
for c in 0..=255u8 {
    // f32_to_srgb8(srgb8_to_f32(c)) is an identity operation
    assert_eq!(f32_to_srgb8(srgb8_to_f32(c)), c);
}

The implementation of this function isn’t particularly clever — it just uses a precomputed lookup table of all 256 results. That has a benefit in that it allows this function to be a const fn, which is somewhat nice: generally color constants hardcoded in source code are sRGB, and this means you can use them to produce linear constants.

In practice this is way faster than the naive approach, and I’m unaware of any faster ways of implementing it, but it’s not really amenable to SIMD, so no SIMD version is provided.