Skip to main content

pfx_materials/
colour.rs

1pub fn linear_channel(c: f32) -> f32 {
2    if c <= 0.04045 {
3        c / 12.92
4    } else {
5        ((c + 0.055) / 1.055).powf(2.4)
6    }
7}
8
9pub fn encode_channel(c: f32) -> f32 {
10    if c <= 0.003_130_8 {
11        c * 12.92
12    } else {
13        1.055 * c.powf(1.0 / 2.4) - 0.055
14    }
15}
16
17pub fn srgb_hex(hex: u32) -> [f32; 3] {
18    let r = ((hex >> 16) & 0xff) as f32 / 255.0;
19    let g = ((hex >> 8) & 0xff) as f32 / 255.0;
20    let b = (hex & 0xff) as f32 / 255.0;
21    [linear_channel(r), linear_channel(g), linear_channel(b)]
22}
23
24pub(crate) fn luminance(c: [f32; 3]) -> f32 {
25    0.2126 * c[0] + 0.7152 * c[1] + 0.0722 * c[2]
26}