]> git.tdb.fi Git - libs/gl.git/blob - source/core/color.h
Move the Resource function override of Texture classes into backend
[libs/gl.git] / source / core / color.h
1 #ifndef MSP_GL_COLOR_H_
2 #define MSP_GL_COLOR_H_
3
4 #include <cmath>
5
6 namespace Msp {
7 namespace GL {
8
9 inline float to_srgb(float c)
10 { return (c<=0.0031308f ? 12.92f*c : 1.055f*std::pow(c, 1/2.4f)-0.055f); }
11
12 inline float to_linear(float c)
13 { return (c<=0.04045 ? c/12.92f : std::pow((c+0.055f)/1.055f, 2.4f)); }
14
15 struct Color
16 {
17         float r = 1.0f;
18         float g = 1.0f;
19         float b = 1.0f;
20         float a = 1.0f;
21
22         Color() = default;
23         Color(float v): r(v), g(v), b(v) { }
24         Color(float r_, float g_, float b_): r(r_), g(g_), b(b_) { }
25         Color(float r_, float g_, float b_, float a_): r(r_), g(g_), b(b_), a(a_) { }
26
27         Color operator*(float f) const { return Color(r*f, g*f, b*f, a); }
28         Color operator+(const Color &c) const { return Color(r+c.r, g+c.g, b+c.b, 1-(1-a)*(1-c.a)); }
29         bool operator==(const Color &c) const { return (r==c.r && g==c.g && b==c.b && a==c.a); }
30         bool operator!=(const Color &c) const { return !operator==(c); }
31
32         Color to_srgb() const { return Color(GL::to_srgb(r), GL::to_srgb(g), GL::to_srgb(b), a); }
33         Color to_linear() const { return Color(GL::to_linear(r), GL::to_linear(g), GL::to_linear(b), a); }
34 };
35
36 } // namespace GL
37 } // namespace Msp
38
39 #endif