rainbow_shared/
lib.rs

1pub fn linear_to_srgb(l: f32) -> f32 {
2    if l < 0.0031308 {
3        12.92 * l
4    } else {
5        1.055f32.mul_add(l.powf(0.416666657 /* 2.4.recip() */), -0.055)
6    }
7}
8
9// https://github.com/apitrace/dxsdk/blob/4978eb035ef0de7385f865ebde8f77b37236fd4c/Include/d3dx_dxgiformatconvert.inl#L287
10pub fn srgb_to_linear(l: f32) -> f32 {
11    if l < 0.04045 {
12        l * 0.0773993805 // = 12.92.recip()
13    } else {
14        l.mul_add(0.947867333, 0.0521327034).powf(2.4)
15    }
16}
17
18pub fn map_all<T: Copy, U>(color: [T; 4], f: impl Fn(T) -> U) -> [U; 4] {
19    [f(color[0]), f(color[1]), f(color[2]), f(color[3])]
20}
21
22/// Applies a function to the RGB components of a color.
23/// The alpha component isn't changed.
24pub fn map_color(color: [f32; 4], f: impl Fn(f32) -> f32) -> [f32; 4] {
25    [f(color[0]), f(color[1]), f(color[2]), color[3]]
26}
27
28pub fn map_alpha(color: [f32; 4], f: impl Fn(f32) -> f32) -> [f32; 4] {
29    [color[0], color[1], color[2], f(color[3])]
30}