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