# source:trunk/src/lol/image/color.h@2187Tweet

Last change on this file since 2187 was 2187, checked in by sam, 8 years ago

image: a few colour conversion routines.

File size: 2.4 KB
Line
1//
2// Lol Engine
3//
4// Copyright: (c) 2010-2012 Sam Hocevar <sam@hocevar.net>
5//   This program is free software; you can redistribute it and/or
6//   modify it under the terms of the Do What The Fuck You Want To
8//   http://www.wtfpl.net/ for more details.
9//
10
11//
12// The Color class
13// ---------------
14// Provides various color conversion routines.
15//
16
17#if !defined __LOL_IMAGE_COLOR_H__
18#define __LOL_IMAGE_COLOR_H__
19
20#include <lol/math/vector.h>
21
22namespace lol
23{
24
25class Color
26{
27public:
28    /*
29     * Convert linear RGB to sRGB
30     */
31    static vec3 LinearRGBTosRGB(vec3 c)
32    {
33        using std::pow;
34        vec3 ret = 12.92 * c;
35        if (c.r > 0.0031308)
36            ret.r = 1.055 * pow(c.r, 1.0 / 2.4) - 0.055;
37        if (c.g > 0.0031308)
38            ret.g = 1.055 * pow(c.g, 1.0 / 2.4) - 0.055;
39        if (c.b > 0.0031308)
40            ret.b = 1.055 * pow(c.b, 1.0 / 2.4) - 0.055;
41        return ret;
42    }
43
44    static vec4 LinearRGBTosRGB(vec4 c)
45    {
46        return vec4(LinearRGBTosRGB(c.rgb), c.a);
47    }
48
49    /*
50     * Convert sRGB to linear RGB
51     */
52    static vec3 sRGBToLinearRGB(vec3 c)
53    {
54        using std::pow;
55        vec3 ret = 1.0 / 12.92 * c;
56        if (c.r > 0.04045)
57            ret.r = pow(c.r + 0.055, 2.4) / pow(1.055, 2.4);
58        if (c.g > 0.04045)
59            ret.g = pow(c.g + 0.055, 2.4) / pow(1.055, 2.4);
60        if (c.b > 0.04045)
61            ret.b = pow(c.b + 0.055, 2.4) / pow(1.055, 2.4);
62        return ret;
63    }
64
65    static vec4 sRGBToLinearRGB(vec4 c)
66    {
67        return vec4(sRGBToLinearRGB(c.rgb), c.a);
68    }
69
70    /*
71     * Convert linear RGB to CIE XYZ
72     */
73    static vec3 LinearRGBToCIEXYZ(vec3 c)
74    {
75        mat3 m(vec3(3.2406, -0.9689, 0.0557),
76               vec3(-1.5372, 1.8758, -0.2040),
77               vec3(-0.4986, 0.0415, 1.0570));
78        return m * c;
79    }
80
81    static vec4 LinearRGBToCIEXYZ(vec4 c)
82    {
83        return vec4(LinearRGBToCIEXYZ(c.rgb), c.a);
84    }
85
86    /*
87     * Convert CIE XYZ to linear RGB
88     */
89    static vec3 CIEXYZToLinearRGB(vec3 c)
90    {
91        mat3 m(vec3(0.4124, 0.2126, 0.0193),
92               vec3(0.3576, 0.7152, 0.1192),
93               vec3(0.1805, 0.0722, 0.9505));
94        return m * c;
95    }
96
97    static vec4 CIEXYZToLinearRGB(vec4 c)
98    {
99        return vec4(CIEXYZToLinearRGB(c.rgb), c.a);
100    }
101};
102
103} /* namespace lol */
104
105#endif // __LOL_IMAGE_COLOR_H__
106
Note: See TracBrowser for help on using the repository browser.