]> git.lyx.org Git - lyx.git/blob - src/Color.C
hopefully fix tex2lyx linking.
[lyx.git] / src / Color.C
1 /**
2  * \file Color.C
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Angus Leeming
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "Color.h"
14
15 #include "LColor.h"
16
17 #include <cmath>
18 #include <sstream>
19 #include <iomanip>
20
21 #ifndef CXX_GLOBAL_CSTD
22 using std::floor;
23 #endif
24
25 using std::max;
26 using std::min;
27 using std::setw;
28
29 using std::istringstream;
30 using std::ostringstream;
31 using std::string;
32
33 namespace lyx {
34
35 namespace {
36
37 int const nohue = -1;
38
39 int hexstrToInt(string const & str)
40 {
41         int val = 0;
42         istringstream is(str);
43         is >> std::setbase(16) >> val;
44         return val;
45 }
46
47 } // namespace anon
48
49
50
51 string const X11hexname(RGBColor const & col)
52 {
53         ostringstream ostr;
54
55         ostr << '#' << std::setbase(16) << std::setfill('0')
56              << setw(2) << col.r
57              << setw(2) << col.g
58              << setw(2) << col.b;
59
60         return ostr.str();
61 }
62
63
64 RGBColor::RGBColor(string const & x11hexname)
65         : r(0), g(0), b(0)
66 {
67         BOOST_ASSERT(x11hexname.size() == 7 && x11hexname[0] == '#');
68         r = hexstrToInt(x11hexname.substr(1,2));
69         g = hexstrToInt(x11hexname.substr(3,2));
70         b = hexstrToInt(x11hexname.substr(5,2));
71 }
72
73
74 RGBColor::RGBColor(HSVColor const & hsv)
75 {
76         double h = hsv.h;
77         double const s = hsv.s;
78         double const v = hsv.v;
79
80         double rd, gd, bd;
81
82         if (h == nohue || s == 0.0) {
83                 rd = gd = bd = v;
84         } else {
85                 if (h == 360.0) h = 0.0;
86                 h /= 60.0;
87
88                 int const j = max(0, static_cast<int>(::floor(h)));
89                 //if (j < 0) j = 0;
90
91                 double const f = h - j;
92                 double const p = v * (1.0 - s);
93                 double const q = v * (1.0 - (s * f));
94                 double const t = v * (1.0 - (s * (1.0 - f)));
95
96                 switch (j) {
97                 case 0:
98                         rd = v;
99                         gd = t;
100                         bd = p;
101                         break;
102                 case 1:
103                         rd = q;
104                         gd = v;
105                         bd = p;
106                         break;
107                 case 2:
108                         rd = p;
109                         gd = v;
110                         bd = t;
111                         break;
112                 case 3:
113                         rd = p;
114                         gd = q;
115                         bd = v;
116                         break;
117                 case 4:
118                         rd = t;
119                         gd = p;
120                         bd = v;
121                         break;
122                 case 5:
123                         rd = v;
124                         gd = p;
125                         bd = q;
126                         break;
127                 default:
128                         rd = v;
129                         gd = t;
130                         bd = p;
131                         break;  // should never happen.
132                 }
133         }
134
135         r = static_cast<int>(::floor((rd * 255.0) + 0.5));
136         g = static_cast<int>(::floor((gd * 255.0) + 0.5));
137         b = static_cast<int>(::floor((bd * 255.0) + 0.5));
138 }
139
140
141 HSVColor::HSVColor(RGBColor const & rgb)
142 {
143         double const r = rgb.r / 255.0;
144         double const g = rgb.g / 255.0;
145         double const b = rgb.b / 255.0;
146
147         double const maxval = max(max(r, g), b);
148         double const minval = min(min(r, g), b);
149
150         v = maxval;
151
152         double const diff = maxval - minval;
153         if (maxval != 0.0)
154                 s = diff / maxval;
155         else
156                 s = 0.0;
157
158         h = nohue;
159         if (s != 0.0) {
160                 double const rc = (maxval - r) / diff;
161                 double const gc = (maxval - g) / diff;
162                 double const bc = (maxval - b) / diff;
163
164                 if (r == maxval)
165                         h = bc - gc;
166                 else if (g == maxval)
167                         h = 2.0 + rc - bc;
168                 else if (b == maxval)
169                         h = 4.0 + gc - rc;
170
171                 h *= 60.0;
172                 if (h < 0)
173                         h += 360;
174         }
175 }
176
177 } // namespace lyx