]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Color.C
Color patch from Angus, KDE patch from Johnm menu patch from Rob, and the usual unint...
[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 #ifdef __GNUG_
15 #pragma implementation
16 #endif
17
18 #include <algorithm> // max
19 #include <cmath> // floor
20 #include "Color.h"
21
22 using std::max;
23 using std::min;
24
25 static int const nohue = -1;
26
27 RGB::RGB( HSV const & hsv )
28 {
29         double h = hsv.h;
30         double s = hsv.s;
31         double v = hsv.v;
32         
33         double rd, gd, bd;
34         
35         if( h == nohue || s == 0.0 ) {
36                 rd = gd = bd = v;
37         } else {
38                 if( h == 360.0 ) h = 0.0;
39                 h /= 60.0;
40
41                 int j = static_cast<int>( ::floor(h) );
42                 if( j < 0 ) j = 0;
43
44                 double f = h - j;
45                 double p = v * (1.0 - s);
46                 double q = v * (1.0 - (s*f));
47                 double t = v * (1.0 - (s*(1.0 - f)));
48
49                 switch( j ) {
50                 case 0:
51                         rd = v;
52                         gd = t;
53                         bd = p;
54                         break;
55                 case 1:
56                         rd = q;
57                         gd = v;
58                         bd = p;
59                         break;
60                 case 2:
61                         rd = p;
62                         gd = v;
63                         bd = t;
64                         break;
65                 case 3:
66                         rd = p;
67                         gd = q;
68                         bd = v;
69                         break;
70                 case 4:
71                         rd = t;
72                         gd = p;
73                         bd = v;
74                         break;
75                 case 5:
76                         rd = v;
77                         gd = p;
78                         bd = q;
79                         break;
80                 default:
81                         rd = v;
82                         gd = t;
83                         bd = p;
84                         break;  // should never happen.
85                 }
86         }
87
88         r = static_cast<int>( ::floor((rd * 255.0) + 0.5) );
89         g = static_cast<int>( ::floor((gd * 255.0) + 0.5) );
90         b = static_cast<int>( ::floor((bd * 255.0) + 0.5) );
91 }
92
93
94 HSV::HSV( RGB const & rgb )
95 {
96         // r, g, b lie in the range 0-1, not 0-255.
97         double r = rgb.r / 255.0;
98         double g = rgb.g / 255.0;
99         double b = rgb.b / 255.0;
100
101         double maxval = max( max( r, g ), b );
102         double minval = max( min( r, g ), b );
103
104         v = maxval;
105
106         double 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 rc = (maxval - r) / diff;
115                 double gc = (maxval - g) / diff;
116                 double 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 }
130