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