1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use super::rgb::*;
#[derive(Debug, Default, Copy, Clone, PartialEq)]
pub struct SRGB(pub u8, pub u8, pub u8);
pub fn srgb2linear(v: f32) -> f32 {
if v <= 0.04045 {
v / 12.92
} else {
((v + 0.055) * 1.0 / 1.055).powf(2.4)
}
}
pub fn linear2srgb(v: f32) -> f32 {
if v <= 0.0031308 {
12.92 * v
} else {
1.055 * f32::powf(v, 1.0 / 2.4) - 0.055
}
}
impl From<SRGB> for RGB
{
fn from(srgb:SRGB) -> Self
{
Self
{
0:srgb2linear(srgb.0 as f32 / 255.0),
1:srgb2linear(srgb.1 as f32 / 255.0),
2:srgb2linear(srgb.2 as f32 / 255.0),
}
}
}
impl From<RGB> for SRGB
{
fn from(rgb:RGB) -> Self
{
Self
{
0:(linear2srgb(rgb.0) * 255.0) as u8,
1:(linear2srgb(rgb.1) * 255.0) as u8,
2:(linear2srgb(rgb.2) * 255.0) as u8,
}
}
}
impl From<SRGB> for RGBSpectrum
{
fn from(srgb:SRGB) -> Self
{
let rgb : RGB = srgb.into();
RGBSpectrum::new(rgb.0, rgb.1, rgb.2)
}
}