]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Color.C
change some __GNUG_ to __GNUG__
[lyx.git] / src / frontends / xforms / Color.C
1 /**
2  * \file Color.C
3  * Copyright 2002 the LyX Team
4  * Read the file COPYING
5  *
6  * \author Angus Leeming, a.leeming@ic.ac.uk
7  */
8
9 #include <config.h>
10
11 #ifdef __GNUG__
12 #pragma implementation
13 #endif
14
15 #include "Color.h"
16 #include <algorithm> // max
17 #include <cmath> // floor
18 #include FORMS_H_LOCATION
19
20 using std::max;
21 using std::min;
22
23 namespace {
24
25 int const nohue = -1;
26
27 } // namespace anon
28
29 RGBColor::RGBColor(HSVColor const & hsv)
30 {
31         double h = hsv.h;
32         double const s = hsv.s;
33         double const v = hsv.v;
34
35         double rd, gd, bd;
36
37         if (h == nohue || s == 0.0) {
38                 rd = gd = bd = v;
39         } else {
40                 if (h == 360.0) h = 0.0;
41                 h /= 60.0;
42
43                 int const j = max(0, static_cast<int>(::floor(h)));
44                 //if (j < 0) j = 0;
45
46                 double const f = h - j;
47                 double const p = v * (1.0 - s);
48                 double const q = v * (1.0 - (s * f));
49                 double const t = v * (1.0 - (s * (1.0 - f)));
50
51                 switch (j) {
52                 case 0:
53                         rd = v;
54                         gd = t;
55                         bd = p;
56                         break;
57                 case 1:
58                         rd = q;
59                         gd = v;
60                         bd = p;
61                         break;
62                 case 2:
63                         rd = p;
64                         gd = v;
65                         bd = t;
66                         break;
67                 case 3:
68                         rd = p;
69                         gd = q;
70                         bd = v;
71                         break;
72                 case 4:
73                         rd = t;
74                         gd = p;
75                         bd = v;
76                         break;
77                 case 5:
78                         rd = v;
79                         gd = p;
80                         bd = q;
81                         break;
82                 default:
83                         rd = v;
84                         gd = t;
85                         bd = p;
86                         break;  // should never happen.
87                 }
88         }
89
90         r = static_cast<int>(::floor((rd * 255.0) + 0.5));
91         g = static_cast<int>(::floor((gd * 255.0) + 0.5));
92         b = static_cast<int>(::floor((bd * 255.0) + 0.5));
93 }
94
95
96 HSVColor::HSVColor(RGBColor const & rgb)
97 {
98         double const r = rgb.r / 255.0;
99         double const g = rgb.g / 255.0;
100         double const b = rgb.b / 255.0;
101
102         double const maxval = max( max( r, g), b);
103         double const minval = min( min( r, g), b);
104
105         v = maxval;
106
107         double const diff = maxval - minval;
108         if (maxval != 0.0)
109                 s = diff / maxval;
110         else
111                 s = 0.0;
112
113         h = nohue;
114         if (s != 0.0) {
115                 double const rc = (maxval - r) / diff;
116                 double const gc = (maxval - g) / diff;
117                 double const bc = (maxval - b) / diff;
118
119                 if (r == maxval)
120                         h = bc - gc;
121                 else if (g == maxval)
122                         h = 2.0 + rc - bc;
123                 else if (b == maxval)
124                         h = 4.0 + gc - rc;
125
126                 h *= 60.0;
127                 if (h < 0)
128                         h += 360;
129         }
130 }