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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
#pragma once
#include "Color.h"
enum ColorSpace { kUninitializedColorSpace = -1, kGammaColorSpace = 0, kLinearColorSpace, kMaxColorSpace };
ColorSpace GetActiveColorSpace ();
// http://www.opengl.org/registry/specs/EXT/framebuffer_sRGB.txt
// http://www.opengl.org/registry/specs/EXT/texture_sRGB_decode.txt
// { cs / 12.92, cs <= 0.04045 }
// { ((cs + 0.055)/1.055)^2.4, cs > 0.04045 }
inline float GammaToLinearSpace (float value)
{
if (value <= 0.04045F)
return value / 12.92F;
else if (value < 1.0F)
return pow((value + 0.055F)/1.055F, 2.4F);
else
return pow(value, 2.4F);
}
// http://www.opengl.org/registry/specs/EXT/framebuffer_sRGB.txt
// http://www.opengl.org/registry/specs/EXT/texture_sRGB_decode.txt
// { 0.0, 0 <= cl
// { 12.92 * c, 0 < cl < 0.0031308
// { 1.055 * cl^0.41666 - 0.055, 0.0031308 <= cl < 1
// { 1.0, cl >= 1 <- This has been adjusted since we want to maintain HDR colors
inline float LinearToGammaSpace (float value)
{
if (value <= 0.0F)
return 0.0F;
else if (value <= 0.0031308F)
return 12.92F * value;
else if (value <= 1.0F)
return 1.055F * powf(value, 0.41666F) - 0.055F;
else
return powf(value, 0.41666F);
}
inline float GammaToLinearSpaceXenon(float val)
{
float ret;
if (val < 0)
ret = 0;
else if (val < 0.25f)
ret = 0.25f * val;
else if (val < 0.375f)
ret = (1.0f/16.0f) + 0.5f*(val-0.25f);
else if (val < 0.75f)
ret = 0.125f + 1.0f*(val-0.375f);
else if (val < 1.0f)
ret = 0.5f + 2.0f*(val-0.75f);
else
ret = 1.0f;
return ret;
}
inline float LinearToGammaSpaceXenon(float val)
{
float ret;
if (val < 0)
ret = 0;
else if (val < (1.0f/16.0f))
ret = 4.0f * val;
else if (val < (1.0f/8.0f))
ret = (1.0f/4.0f) + 2.0f*(val-(1.0f/16.0f));
else if (val < 0.5f)
ret = 0.375f + 1.0f*(val-0.125f);
else if (val < 1.0f)
ret = 0.75f + 0.5f*(val-0.50f);
else
ret = 1.0f;
return ret;
}
inline ColorRGBAf GammaToLinearSpace (const ColorRGBAf& value)
{
return ColorRGBAf(GammaToLinearSpace(value.r), GammaToLinearSpace(value.g), GammaToLinearSpace(value.b), value.a);
}
inline ColorRGBAf LinearToGammaSpace (const ColorRGBAf& value)
{
return ColorRGBAf(LinearToGammaSpace(value.r), LinearToGammaSpace(value.g), LinearToGammaSpace(value.b), value.a);
}
inline ColorRGBAf GammaToLinearSpaceXenon (const ColorRGBAf& value)
{
return ColorRGBAf(GammaToLinearSpaceXenon(value.r), GammaToLinearSpaceXenon(value.g), GammaToLinearSpaceXenon(value.b), value.a);
}
inline ColorRGBAf LinearToGammaSpaceXenon (const ColorRGBAf& value)
{
return ColorRGBAf(LinearToGammaSpaceXenon(value.r), LinearToGammaSpaceXenon(value.g), LinearToGammaSpaceXenon(value.b), value.a);
}
inline float GammaToActiveColorSpace (float value)
{
if (GetActiveColorSpace () == kLinearColorSpace)
return GammaToLinearSpace(value);
else
return value;
}
inline ColorRGBAf GammaToActiveColorSpace (const ColorRGBAf& value)
{
if (GetActiveColorSpace () == kLinearColorSpace)
return GammaToLinearSpace(value);
else
return value;
}
inline ColorRGBAf ActiveToGammaColorSpace (const ColorRGBAf& value)
{
if (GetActiveColorSpace () == kLinearColorSpace)
return LinearToGammaSpace(value);
else
return value;
}
|