]> git.mxchange.org Git - simgear.git/blob - simgear/screen/colors.hxx
40ce19e6945ecd71ad716fd640daf7f1b5199dff
[simgear.git] / simgear / screen / colors.hxx
1 // colours.h 
2 //
3 // -- This header file contains color related functions
4 //
5 // Copyright (C) 2003  FlightGear Flight Simulator
6 //
7 // This program is free software; you can redistribute it and/or
8 // modify it under the terms of the GNU General Public License as
9 // published by the Free Software Foundation; either version 2 of the
10 // License, or (at your option) any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 // General Public License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program; if not, write to the Free Software
19 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 //
21 // $Id$
22
23
24 #ifndef _SG_COLORS_HXX
25 #define _SG_COLORS_HXX 1
26
27 #include <math.h>
28
29 #if defined( macintosh )
30 const float system_gamma = 1.4;
31
32 #elif defined (sgi)
33 const float system_gamma = 1.7;
34
35 #else   // others
36 const float system_gamma = 2.5;
37 #endif
38
39 // simple architecture independant gamma correction function.
40 inline void gamma_correct_rgb(float *color,
41                               float reff = 2.5, float system = system_gamma)
42 {
43     if (reff == system)
44        return;
45
46     float tmp = reff/system;
47     color[0] = pow(color[0], tmp);
48     color[1] = pow(color[1], tmp);
49     color[2] = pow(color[2], tmp);
50
51     if ( color[0] < 0.0 ) { color[0] = 0.0; }
52     if ( color[1] < 0.0 ) { color[1] = 0.0; }
53     if ( color[2] < 0.0 ) { color[2] = 0.0; }
54     if ( color[0] > 1.0 ) { color[0] = 1.0; }
55     if ( color[1] > 1.0 ) { color[1] = 1.0; }
56     if ( color[2] > 1.0 ) { color[2] = 1.0; }
57 };
58
59 inline void gamma_correct_c(float *color,
60                             float reff = 2.5, float system = system_gamma)
61 {
62    if (reff == system)
63       return;
64
65    *color = pow(*color, reff/system);
66    if ( *color < 0.0 ) { *color = 0.0; }
67    if ( *color > 1.0 ) { *color = 1.0; }
68 };
69
70 inline void gamma_restore_rgb(float *color,
71                               float reff = 2.5, float system = system_gamma)
72 {
73     if (reff == system)
74        return;
75
76     float tmp = system/reff;
77     color[0] = pow(color[0], tmp);
78     color[1] = pow(color[1], tmp);
79     color[2] = pow(color[2], tmp);
80
81     if ( color[0] < 0.0 ) { color[0] = 0.0; }
82     if ( color[1] < 0.0 ) { color[1] = 0.0; }
83     if ( color[2] < 0.0 ) { color[2] = 0.0; }
84     if ( color[0] > 1.0 ) { color[0] = 1.0; }
85     if ( color[1] > 1.0 ) { color[1] = 1.0; }
86     if ( color[2] > 1.0 ) { color[2] = 1.0; }
87 };
88
89 inline void gamma_restore_c(float *color,
90                             float reff = 2.5, float system = system_gamma)
91 {
92    if (reff == system)
93       return;
94
95    *color = pow(*color, system/reff);
96    if ( *color < 0.0 ) { *color = 0.0; }
97    if ( *color > 1.0 ) { *color = 1.0; }
98 };
99
100
101 #endif // _SG_COLORS_HXX
102