]> git.lyx.org Git - lyx.git/blob - src/frontends/xforms/Color.C
Converter patch from Dekel, Preference patch from Angus, menu patch from Rob
[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 <fstream> // ofstream
22 #include "Color.h"
23 #include "lyxlex.h"
24
25 using std::max;
26 using std::min;
27 using std::ofstream;
28
29 static int const nohue = -1;
30
31 RGBColor::RGBColor( HSVColor const & hsv )
32 {
33         double h = hsv.h;
34         double s = hsv.s;
35         double v = hsv.v;
36         
37         double rd, gd, bd;
38         
39         if( h == nohue || s == 0.0 ) {
40                 rd = gd = bd = v;
41         } else {
42                 if( h == 360.0 ) h = 0.0;
43                 h /= 60.0;
44
45                 int j = static_cast<int>( ::floor(h) );
46                 if( j < 0 ) j = 0;
47
48                 double f = h - j;
49                 double p = v * (1.0 - s);
50                 double q = v * (1.0 - (s*f));
51                 double t = v * (1.0 - (s*(1.0 - f)));
52
53                 switch( j ) {
54                 case 0:
55                         rd = v;
56                         gd = t;
57                         bd = p;
58                         break;
59                 case 1:
60                         rd = q;
61                         gd = v;
62                         bd = p;
63                         break;
64                 case 2:
65                         rd = p;
66                         gd = v;
67                         bd = t;
68                         break;
69                 case 3:
70                         rd = p;
71                         gd = q;
72                         bd = v;
73                         break;
74                 case 4:
75                         rd = t;
76                         gd = p;
77                         bd = v;
78                         break;
79                 case 5:
80                         rd = v;
81                         gd = p;
82                         bd = q;
83                         break;
84                 default:
85                         rd = v;
86                         gd = t;
87                         bd = p;
88                         break;  // should never happen.
89                 }
90         }
91
92         r = static_cast<int>( ::floor((rd * 255.0) + 0.5) );
93         g = static_cast<int>( ::floor((gd * 255.0) + 0.5) );
94         b = static_cast<int>( ::floor((bd * 255.0) + 0.5) );
95 }
96
97
98 HSVColor::HSVColor( RGBColor const & rgb )
99 {
100         // r, g, b lie in the range 0-1, not 0-255.
101         double r = rgb.r / 255.0;
102         double g = rgb.g / 255.0;
103         double b = rgb.b / 255.0;
104
105         double maxval = max( max( r, g ), b );
106         double minval = max( min( r, g ), b );
107
108         v = maxval;
109
110         double diff = maxval - minval;
111         if( maxval != 0.0 )
112                 s = diff / maxval;
113         else
114                 s = 0.0;
115
116         h = nohue;
117         if( s != 0.0 ) {
118                 double rc = (maxval - r) / diff;
119                 double gc = (maxval - g) / diff;
120                 double bc = (maxval - b) / diff;
121
122                 if( r == maxval )
123                         h = bc - gc;
124                 else if( g == maxval )
125                         h = 2.0 + rc - bc;
126                 else if( b == maxval )
127                         h = 4.0 + gc - rc;
128
129                 h *= 60.0;
130                 if ( h < 0 )
131                         h += 360;
132         }
133 }
134
135
136 // sorted by hand to prevent LyXLex from complaining on read().
137 static
138 keyword_item xformTags[] = {
139 //      { "\\gui_active_tab", FL_LIGHTER_COL1 },
140         { "\\gui_background", FL_COL1 },
141         { "\\gui_buttonbottom", FL_BOTTOM_BCOL },
142         { "\\gui_buttonleft", FL_LEFT_BCOL },
143         { "\\gui_buttonright", FL_RIGHT_BCOL },
144         { "\\gui_buttontop", FL_TOP_BCOL },
145         { "\\gui_inactive", FL_INACTIVE },
146         { "\\gui_push_button", FL_YELLOW },
147         { "\\gui_selected", FL_MCOL },  
148         { "\\gui_text", FL_BLACK }
149 };
150
151
152 static const int xformCount = sizeof(xformTags) / sizeof(keyword_item);
153
154
155 bool XformColor::read(string const & filename)
156 {
157         LyXLex lexrc( xformTags, xformCount );
158         if( !lexrc.setFile( filename ) )
159                 return false;
160
161         while( lexrc.IsOK() ) {
162                 int le = lexrc.lex();
163
164                 switch( le ) {
165                 case LyXLex::LEX_UNDEF:
166                         lexrc.printError("Unknown tag `$$Token'");
167                         continue; 
168                 case LyXLex::LEX_FEOF:
169                         continue;
170                 default: break;
171                 }
172
173                 RGBColor col;
174
175                 if( !lexrc.next() ) break;
176                 col.r = lexrc.GetInteger();
177
178                 if( !lexrc.next() ) break;
179                 col.g = lexrc.GetInteger();
180
181                 if( !lexrc.next() ) break;
182                 col.b = lexrc.GetInteger();
183
184                 fl_mapcolor(le, col.r, col.g, col.b);
185         }
186         
187         return true;
188 }
189
190
191 bool XformColor::write(string const & filename)
192 {
193         ofstream os(filename.c_str());
194         if (!os)
195                 return false;
196
197         os << "### This file is part of\n"
198            << "### ========================================================\n"
199            << "###          LyX, The Document Processor\n"
200            << "###\n"
201            << "###          Copyright 1995 Matthias Ettrich\n"
202            << "###          Copyright 1995-2000 The LyX Team.\n"
203            << "###\n"
204            << "### ========================================================\n"
205            << "\n"
206            << "# This file is written by LyX, if you want to make your own\n"
207            << "# modifications you should do them from inside LyX and save\n"
208            << "\n";
209
210         for( int i = 0; i < xformCount; ++i ) {
211                 string tag  = xformTags[i].tag;
212                 int colorID = xformTags[i].code;
213                 RGBColor color;
214
215                 fl_getmcolor(colorID, &color.r, &color.g, &color.b);
216
217                 os << tag + " "
218                    << color.r << " " << color.g << " " << color.b << "\n";
219         }
220
221         return true;
222 }