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