Mercurial > octave-nkf
comparison src/graphics.cc @ 6681:0458599c50d7
[project @ 2007-06-01 03:33:21 by jwe]
author | jwe |
---|---|
date | Fri, 01 Jun 2007 03:33:22 +0000 |
parents | 49724abe1236 |
children | 39e34c15f222 |
comparison
equal
deleted
inserted
replaced
6680:cd39d4a0b671 | 6681:0458599c50d7 |
---|---|
60 return val.is_empty () ? octave_value (octave_NaN) : val; | 60 return val.is_empty () ? octave_value (octave_NaN) : val; |
61 } | 61 } |
62 | 62 |
63 // --------------------------------------------------------------------- | 63 // --------------------------------------------------------------------- |
64 | 64 |
65 class color_property | 65 class |
66 radio_values | |
66 { | 67 { |
67 public: | 68 public: |
68 color_property (double r = 0, double g = 0, double b = 1) | 69 radio_values (const std::string& opt_string = std::string ()) |
70 { | |
71 size_t beg = 0; | |
72 size_t len = opt_string.length (); | |
73 bool done = len == 0; | |
74 | |
75 while (! done) | |
76 { | |
77 size_t end = opt_string.find ('|', beg); | |
78 | |
79 if (end == std::string::npos) | |
80 { | |
81 end = len; | |
82 done = true; | |
83 } | |
84 | |
85 std::string t = opt_string.substr (beg, end-beg); | |
86 | |
87 // Might want more error checking here... | |
88 if (t[0] == '{') | |
89 { | |
90 t = t.substr (1, t.length () - 2); | |
91 default_val = t; | |
92 } | |
93 else if (beg == 0) // ensure default value | |
94 default_val = t; | |
95 | |
96 possible_vals.insert (t); | |
97 | |
98 beg = end + 1; | |
99 } | |
100 }; | |
101 | |
102 radio_values (const radio_values& a) | |
103 : default_val (a.default_val), possible_vals (a.possible_vals) { } | |
104 | |
105 radio_values& operator = (const radio_values& a) | |
106 { | |
107 if (&a != this) | |
108 { | |
109 default_val = a.default_val; | |
110 possible_vals = a.possible_vals; | |
111 } | |
112 | |
113 return *this; | |
114 } | |
115 | |
116 std::string default_value (void) const { return default_val; } | |
117 | |
118 std::set<std::string> possible_values (void) const { return possible_vals; } | |
119 | |
120 bool validate (const std::string& val) | |
121 { | |
122 bool retval = true; | |
123 | |
124 if (possible_vals.find (val) == possible_vals.end ()) | |
125 { | |
126 error ("invalid value = %s", val.c_str ()); | |
127 retval = false; | |
128 } | |
129 | |
130 return retval; | |
131 } | |
132 | |
133 private: | |
134 // Might also want to cache | |
135 std::string default_val; | |
136 std::set<std::string> possible_vals; | |
137 }; | |
138 | |
139 class | |
140 radio_property | |
141 { | |
142 public: | |
143 radio_property (const radio_values& v) | |
144 : vals (v), current_val (v.default_value ()) { } | |
145 | |
146 radio_property (const radio_values& v, const std::string& initial_value) | |
147 : vals (v), current_val (initial_value) { } | |
148 | |
149 radio_property (const radio_property& a) | |
150 : vals (a.vals), current_val (a.current_val) { } | |
151 | |
152 radio_property& operator = (const radio_property& a) | |
153 { | |
154 if (&a != this) | |
155 { | |
156 vals = a.vals; | |
157 current_val = a.current_val; | |
158 } | |
159 | |
160 return *this; | |
161 } | |
162 | |
163 radio_property& operator = (const std::string& newval) | |
164 { | |
165 if (vals.validate (newval)) | |
166 current_val = newval; | |
167 | |
168 return *this; | |
169 } | |
170 | |
171 const std::string& current_value (void) const { return current_val; } | |
172 | |
173 private: | |
174 radio_values vals; | |
175 std::string current_val; | |
176 }; | |
177 | |
178 class | |
179 color_values | |
180 { | |
181 public: | |
182 color_values (double r = 0, double g = 0, double b = 1) | |
69 { | 183 { |
70 xrgb[0] = r; | 184 xrgb[0] = r; |
71 xrgb[1] = g; | 185 xrgb[1] = g; |
72 xrgb[2] = b; | 186 xrgb[2] = b; |
73 | 187 |
74 validate (); | 188 validate (); |
75 } | 189 } |
76 | 190 |
77 color_property (char c) | 191 color_values (const char c) |
78 { | 192 { |
79 c2rgb (c); | 193 if (! c2rgb (c)) |
80 } | 194 error ("invalid color specification"); |
81 | 195 } |
82 color_property (const octave_value& val) | 196 |
83 { | 197 color_values (const color_values& c) |
84 // FIXME -- need some error checking here. | 198 { |
85 | 199 xrgb[0] = c.xrgb[0]; |
86 Matrix m = val.matrix_value (); | 200 xrgb[1] = c.xrgb[1]; |
87 | 201 xrgb[2] = c.xrgb[2]; |
88 if (! error_state && m.numel () == 3) | 202 } |
203 | |
204 color_values& operator = (const color_values& c) | |
205 { | |
206 if (&c != this) | |
89 { | 207 { |
90 for (int i = 0; i < m.numel (); i++) | 208 xrgb[0] = c.xrgb[0]; |
91 xrgb[i] = m(i); | 209 xrgb[1] = c.xrgb[1]; |
92 | 210 xrgb[2] = c.xrgb[2]; |
93 validate (); | 211 |
94 } | 212 } |
95 else | 213 |
96 { | 214 return *this; |
97 std::string c = val.string_value (); | 215 } |
98 | 216 |
99 if (! error_state && c.length () == 1) | 217 const double* rgb (void) const { return xrgb; } |
100 c2rgb (c[0]); | |
101 else | |
102 error ("invalid color specification"); | |
103 } | |
104 } | |
105 | 218 |
106 void validate (void) const | 219 void validate (void) const |
107 { | 220 { |
108 for (int i = 0; i < 3; i++) | 221 for (int i = 0; i < 3; i++) |
109 { | 222 { |
113 break; | 226 break; |
114 } | 227 } |
115 } | 228 } |
116 } | 229 } |
117 | 230 |
118 operator octave_value (void) const | |
119 { | |
120 Matrix retval (1, 3); | |
121 | |
122 for (int i = 0; i < 3 ; i++) | |
123 retval(i) = xrgb[i]; | |
124 | |
125 return retval; | |
126 } | |
127 | |
128 const double* rgb (void) const | |
129 { | |
130 return xrgb; | |
131 } | |
132 | |
133 private: | 231 private: |
134 double xrgb[3]; | 232 double xrgb[3]; |
135 | 233 |
136 void c2rgb (char c) | 234 bool c2rgb (char c) |
137 { | 235 { |
138 double tmp_rgb[4] = {0, 0, 0}; | 236 double tmp_rgb[3] = {0, 0, 0}; |
237 bool retval = true; | |
139 | 238 |
140 switch(c) | 239 switch(c) |
141 { | 240 { |
142 case 'r': | 241 case 'r': |
143 tmp_rgb[0] = 1; | 242 tmp_rgb[0] = 1; |
166 case 'w': | 265 case 'w': |
167 tmp_rgb[0] = tmp_rgb[1] = tmp_rgb[2] = 1; | 266 tmp_rgb[0] = tmp_rgb[1] = tmp_rgb[2] = 1; |
168 break; | 267 break; |
169 | 268 |
170 default: | 269 default: |
171 error ("invalid color specification"); | 270 retval = false; |
172 } | 271 } |
173 | 272 |
174 if (! error_state) | 273 if (retval) |
175 { | 274 { |
176 for (int i = 0; i < 4; i++) | 275 for (int i = 0; i < 3; i++) |
177 xrgb[i] = tmp_rgb[i]; | 276 xrgb[i] = tmp_rgb[i]; |
178 } | 277 } |
278 | |
279 return retval; | |
179 } | 280 } |
180 }; | 281 }; |
181 | 282 |
182 class colormap_property | 283 |
284 class | |
285 color_property | |
286 { | |
287 public: | |
288 color_property (const color_values& c = color_values (), | |
289 const radio_values& v = radio_values ()) | |
290 : current_type (color_t), color_val (c), radio_val (v), | |
291 current_val (v.default_value ()) | |
292 { } | |
293 | |
294 color_property (const radio_values& v) | |
295 : current_type (radio_t), color_val (color_values ()), radio_val (v), | |
296 current_val (v.default_value ()) | |
297 { } | |
298 | |
299 color_property (const radio_values& v, const std::string& initial_value) | |
300 : current_type (radio_t), color_val (color_values ()), radio_val (v), | |
301 current_val (initial_value) | |
302 { } | |
303 | |
304 color_property (const octave_value& val) | |
305 : radio_val (), current_val () | |
306 { | |
307 // FIXME -- need some error checking here. | |
308 | |
309 if (val.is_string ()) | |
310 { | |
311 std::string s = val.string_value (); | |
312 | |
313 if (! s.empty ()) | |
314 { | |
315 color_values col (s[0]); | |
316 if (! error_state) | |
317 { | |
318 color_val = col; | |
319 current_type = color_t; | |
320 } | |
321 } | |
322 else | |
323 error ("invalid color specification"); | |
324 } | |
325 else if (val.is_real_matrix ()) | |
326 { | |
327 Matrix m = val.matrix_value (); | |
328 | |
329 if (m.numel () == 3) | |
330 { | |
331 color_values col (m (0), m (1), m(2)); | |
332 if (! error_state) | |
333 { | |
334 color_val = col; | |
335 current_type = color_t; | |
336 } | |
337 } | |
338 else | |
339 error ("invalid color specification"); | |
340 } | |
341 else | |
342 error ("invalid color specification"); | |
343 } | |
344 | |
345 operator octave_value (void) const | |
346 { | |
347 if (current_type == color_t) | |
348 { | |
349 Matrix retval (1, 3); | |
350 const double *xrgb = color_val.rgb (); | |
351 | |
352 for (int i = 0; i < 3 ; i++) | |
353 retval(i) = xrgb[i]; | |
354 | |
355 return retval; | |
356 } | |
357 | |
358 return current_val; | |
359 } | |
360 | |
361 color_property& operator = (const color_property& a) | |
362 { | |
363 if (&a != this) | |
364 { | |
365 current_type = a.current_type; | |
366 color_val = a.color_val; | |
367 radio_val = a.radio_val; | |
368 current_val = a.current_val; | |
369 } | |
370 | |
371 return *this; | |
372 } | |
373 | |
374 color_property& operator = (const std::string& newval) | |
375 { | |
376 if (radio_val.validate (newval)) | |
377 { | |
378 current_val = newval; | |
379 current_type = radio_t; | |
380 } | |
381 | |
382 return *this; | |
383 } | |
384 | |
385 color_property& operator = (const color_values& newval) | |
386 { | |
387 color_val = newval; | |
388 current_type = color_t; | |
389 | |
390 return *this; | |
391 } | |
392 | |
393 bool is_rgb (void) const { return (current_type == color_t); } | |
394 | |
395 bool is_radio (void) const { return (current_type == radio_t); } | |
396 | |
397 const double* rgb (void) const | |
398 { | |
399 if (current_type != color_t) | |
400 error ("color has no rgb value"); | |
401 | |
402 return color_val.rgb (); | |
403 } | |
404 | |
405 const std::string& current_value (void) const | |
406 { | |
407 if (current_type != radio_t) | |
408 error ("color has no radio value"); | |
409 | |
410 return current_val; | |
411 } | |
412 | |
413 private: | |
414 enum { color_t, radio_t } current_type; | |
415 color_values color_val; | |
416 radio_values radio_val; | |
417 std::string current_val; | |
418 }; | |
419 | |
420 class | |
421 colormap_property | |
183 { | 422 { |
184 public: | 423 public: |
185 colormap_property (const Matrix& m = Matrix ()) | 424 colormap_property (const Matrix& m = Matrix ()) |
186 : cmap (m) | 425 : cmap (m) |
187 { | 426 { |