~ubuntu-branches/ubuntu/oneiric/soqt/oneiric

« back to all changes in this revision

Viewing changes to build/msvc6/src/Inventor/Qt/nodes/ColorEditor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Steve M. Robbins
  • Date: 2009-03-01 11:41:00 UTC
  • mfrom: (1.1.4 upstream) (5.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20090301114100-f4zz3n1oasa52fgk
Tags: 1.4.2~svn20090224-2
* Upload upstream SVN head version containing fixes to build with Coin 3
  (Closes: #515729, #515736, #515742).  Upstream indicated to me that
  SVN is stable enough to release.

* control: Update Standards-Version to 3.8.0; no changes required.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**************************************************************************\
2
 
 *
3
 
 *  This file is part of the Coin 3D visualization library.
4
 
 *  Copyright (C) 1998-2005 by Systems in Motion.  All rights reserved.
5
 
 *
6
 
 *  This library is free software; you can redistribute it and/or
7
 
 *  modify it under the terms of the GNU General Public License
8
 
 *  ("GPL") version 2 as published by the Free Software Foundation.
9
 
 *  See the file LICENSE.GPL at the root directory of this source
10
 
 *  distribution for additional information about the GNU GPL.
11
 
 *
12
 
 *  For using Coin with software that can not be combined with the GNU
13
 
 *  GPL, and for taking advantage of the additional benefits of our
14
 
 *  support services, please contact Systems in Motion about acquiring
15
 
 *  a Coin Professional Edition License.
16
 
 *
17
 
 *  See <URL:http://www.coin3d.org/> for more information.
18
 
 *
19
 
 *  Systems in Motion, Postboks 1283, Pirsenteret, 7462 Trondheim, NORWAY.
20
 
 *  <URL:http://www.sim.no/>.
21
 
 *
22
 
\**************************************************************************/
23
 
 
24
 
#include <Inventor/SbPList.h>
25
 
#include <Inventor/SoSceneManager.h>
26
 
#include <Inventor/fields/SoSFColor.h>
27
 
#include <Inventor/fields/SoMFColor.h>
28
 
#include <Inventor/fields/SoMFUInt32.h>
29
 
#include <Inventor/nodes/SoSwitch.h>
30
 
#include <Inventor/nodes/SoSeparator.h>
31
 
#include <Inventor/nodes/SoTexture2.h>
32
 
#include <Inventor/sensors/SoFieldSensor.h>
33
 
#include <Inventor/actions/SoGLRenderAction.h>
34
 
 
35
 
#include <Inventor/Qt/SoAny.h>
36
 
 
37
 
#include <Inventor/Qt/nodes/SoGuiViewportFix.h>
38
 
#include <Inventor/Qt/nodes/SoGuiPane.h>
39
 
#include <Inventor/Qt/nodes/SoGuiClickCounter.h>
40
 
#include <Inventor/Qt/nodes/SoGuiSlider1.h>
41
 
#include <Inventor/Qt/nodes/SoGuiSlider2.h>
42
 
 
43
 
#include <Inventor/Qt/nodes/SoGuiColorEditor.h>
44
 
#include <assert.h>
45
 
 
46
 
/*!
47
 
  \class SoGuiColorEditor Inventor/Qt/editors/SoGuiColorEditor.h
48
 
  \brief The SoGuiColorEditor class is a GUI component for interactively
49
 
  editing color fields.
50
 
*/
51
 
 
52
 
/*!
53
 
  \enum SoGuiColorEditor::Sliders
54
 
*/
55
 
 
56
 
/*!
57
 
  \val SoGuiColorEditor::NONE
58
 
*/
59
 
 
60
 
/*!
61
 
  \val SoGuiColorEditor::INTENSITY
62
 
*/
63
 
 
64
 
/*!
65
 
  \val SoGuiColorEditor::RGB
66
 
*/
67
 
 
68
 
/*!
69
 
  \val SoGuiColorEditor::HSV
70
 
*/
71
 
 
72
 
/*!
73
 
  \val SoGuiColorEditor::RGB_V
74
 
*/
75
 
 
76
 
/*!
77
 
  \val SoGuiColorEditor::RGB_HSV
78
 
*/
79
 
 
80
 
/*!
81
 
  \enum SoGuiColorEditor::Update
82
 
*/
83
 
 
84
 
/*!
85
 
  \val SoGuiColorEditor::CONTINUOUS
86
 
*/
87
 
 
88
 
/*!
89
 
  \val SoGuiColorEditor::AFTER_ACCEPT
90
 
*/
91
 
 
92
 
// *************************************************************************
93
 
 
94
 
class ColorEditor {
95
 
public:
96
 
  SoGuiColorEditor * api;
97
 
 
98
 
  void colorChange(void);
99
 
 
100
 
  SoFieldSensor * color_sensor;
101
 
  static void color_update_cb(void * closure, SoSensor * sensor);
102
 
 
103
 
  SoGuiPane * editor;
104
 
  SoGuiClickCounter * switcher;
105
 
  SoGuiSlider1 * slider_r; // red
106
 
  SoGuiSlider1 * slider_g; // green
107
 
  SoGuiSlider1 * slider_b; // blue
108
 
  SoGuiSlider1 * slider_h; // hue
109
 
  SoGuiSlider1 * slider_s; // saturation
110
 
  SoGuiSlider1 * slider_v; // value
111
 
  SoGuiSlider2 * slider_wheel;
112
 
 
113
 
  SoFieldSensor * sensor_r;
114
 
  SoFieldSensor * sensor_g;
115
 
  SoFieldSensor * sensor_b;
116
 
  SoFieldSensor * sensor_h;
117
 
  SoFieldSensor * sensor_s;
118
 
  SoFieldSensor * sensor_v;
119
 
  SoFieldSensor * sensor_wheel;
120
 
 
121
 
  static void update_r_cb(void * closure, SoSensor * sensor);
122
 
  static void update_g_cb(void * closure, SoSensor * sensor);
123
 
  static void update_b_cb(void * closure, SoSensor * sensor);
124
 
  static void update_h_cb(void * closure, SoSensor * sensor);
125
 
  static void update_s_cb(void * closure, SoSensor * sensor);
126
 
  static void update_v_cb(void * closure, SoSensor * sensor);
127
 
  static void update_wheel_cb(void * closure, SoSensor * sensor);
128
 
 
129
 
  static const char * editorscene[];
130
 
 
131
 
  static float calculateHue(float x, float y);
132
 
  static SbVec2f calculateFromHue(float h, float s, float v);
133
 
 
134
 
  void generateSliderTextureR(const SbColor & current, SbBool wysiwyg);
135
 
  void generateSliderTextureG(const SbColor & current, SbBool wysiwyg);
136
 
  void generateSliderTextureB(const SbColor & current, SbBool wysiwyg);
137
 
  void generateSliderTextureH(const SbColor & current, SbBool wysiwyg);
138
 
  void generateSliderTextureS(const SbColor & current, SbBool wysiwyg);
139
 
  void generateSliderTextureV(const SbColor & current, SbBool wysiwyg);
140
 
  void generateSliderTextureHSV(const SbColor & current, SbBool wysiwyg);
141
 
};
142
 
 
143
 
// *************************************************************************
144
 
 
145
 
#define PRIVATE(obj) ((ColorEditor *) ((SoGuiColorEditor *) obj)->internals)
146
 
#define PUBLIC(obj) (((ColorEditor *) obj)->api)
147
 
 
148
 
void
149
 
SoGuiColorEditor::initClass(void)
150
 
{
151
 
  SO_KIT_INIT_CLASS(SoGuiColorEditor, SoBaseKit, "BaseKit");
152
 
}
153
 
 
154
 
SO_KIT_SOURCE(SoGuiColorEditor);
155
 
 
156
 
SoGuiColorEditor::SoGuiColorEditor(void)
157
 
{
158
 
  this->internals = (void *) new ColorEditor;
159
 
  PRIVATE(this)->api = this;
160
 
 
161
 
  SO_KIT_CONSTRUCTOR(SoGuiColorEditor);
162
 
 
163
 
  SO_KIT_ADD_FIELD(wysiwyg, (FALSE));
164
 
  SO_KIT_ADD_FIELD(color, (SbColor(0.0f, 0.0f, 0.0f)));
165
 
  SO_KIT_ADD_FIELD(sliders, (SoGuiColorEditor::RGB_V));
166
 
  SO_KIT_ADD_FIELD(update, (SoGuiColorEditor::AFTER_ACCEPT));
167
 
 
168
 
  SO_KIT_DEFINE_ENUM_VALUE(Sliders, NONE);
169
 
  SO_KIT_DEFINE_ENUM_VALUE(Sliders, INTENSITY);
170
 
  SO_KIT_DEFINE_ENUM_VALUE(Sliders, RGB);
171
 
  SO_KIT_DEFINE_ENUM_VALUE(Sliders, HSV);
172
 
  SO_KIT_DEFINE_ENUM_VALUE(Sliders, RGB_V);
173
 
  SO_KIT_DEFINE_ENUM_VALUE(Sliders, RGB_HSV);
174
 
 
175
 
  SO_KIT_DEFINE_ENUM_VALUE(Update, CONTINUOUS);
176
 
  SO_KIT_DEFINE_ENUM_VALUE(Update, AFTER_ACCEPT);
177
 
 
178
 
  SO_KIT_SET_SF_ENUM_TYPE(sliders, Sliders);
179
 
  SO_KIT_SET_SF_ENUM_TYPE(update, Update);
180
 
 
181
 
  SO_KIT_ADD_CATALOG_ENTRY(root, SoGuiPane, TRUE, this, "", FALSE);
182
 
 
183
 
  SO_KIT_INIT_INSTANCE();
184
 
 
185
 
  PRIVATE(this)->switcher = NULL;
186
 
  PRIVATE(this)->sensor_r = NULL;
187
 
  PRIVATE(this)->sensor_g = NULL;
188
 
  PRIVATE(this)->sensor_b = NULL;
189
 
  PRIVATE(this)->sensor_h = NULL;
190
 
  PRIVATE(this)->sensor_s = NULL;
191
 
  PRIVATE(this)->sensor_v = NULL;
192
 
  PRIVATE(this)->sensor_wheel = NULL;
193
 
 
194
 
  PRIVATE(this)->editor = NULL;
195
 
 
196
 
  SoNode * scene = SoAny::loadSceneGraph(ColorEditor::editorscene);
197
 
  assert(scene != NULL);
198
 
  assert(scene->isOfType(SoGuiPane::getClassTypeId()));
199
 
 
200
 
  PRIVATE(this)->editor = (SoGuiPane *) scene;
201
 
  PRIVATE(this)->editor->ref();
202
 
  PRIVATE(this)->switcher = (SoGuiClickCounter *) SoAny::scanSceneForName(PRIVATE(this)->editor, "switcher");
203
 
  PRIVATE(this)->slider_r = (SoGuiSlider1 *) SoAny::scanSceneForName(PRIVATE(this)->editor, "slider_r", TRUE);
204
 
  PRIVATE(this)->slider_g = (SoGuiSlider1 *) SoAny::scanSceneForName(PRIVATE(this)->editor, "slider_g", TRUE);
205
 
  PRIVATE(this)->slider_b = (SoGuiSlider1 *) SoAny::scanSceneForName(PRIVATE(this)->editor, "slider_b", TRUE);
206
 
  PRIVATE(this)->slider_h = (SoGuiSlider1 *) SoAny::scanSceneForName(PRIVATE(this)->editor, "slider_h", TRUE);
207
 
  PRIVATE(this)->slider_s = (SoGuiSlider1 *) SoAny::scanSceneForName(PRIVATE(this)->editor, "slider_s", TRUE);
208
 
  PRIVATE(this)->slider_v = (SoGuiSlider1 *) SoAny::scanSceneForName(PRIVATE(this)->editor, "slider_v", TRUE);
209
 
  PRIVATE(this)->slider_wheel = (SoGuiSlider2 *) SoAny::scanSceneForName(PRIVATE(this)->editor, "colorwheel", TRUE);
210
 
  if ( PRIVATE(this)->slider_r ) {
211
 
    PRIVATE(this)->slider_r->ref();
212
 
    PRIVATE(this)->sensor_r = new SoFieldSensor(ColorEditor::update_r_cb, PRIVATE(this));
213
 
    PRIVATE(this)->sensor_r->attach(&(PRIVATE(this)->slider_r->value));
214
 
    PRIVATE(this)->generateSliderTextureR(this->color.getValue(), FALSE);
215
 
  }
216
 
  if ( PRIVATE(this)->slider_g ) {
217
 
    PRIVATE(this)->slider_g->ref();
218
 
    PRIVATE(this)->sensor_g = new SoFieldSensor(ColorEditor::update_g_cb, PRIVATE(this));
219
 
    PRIVATE(this)->sensor_g->attach(&(PRIVATE(this)->slider_g->value));
220
 
    PRIVATE(this)->generateSliderTextureG(this->color.getValue(), FALSE);
221
 
  }
222
 
  if ( PRIVATE(this)->slider_b ) {
223
 
    PRIVATE(this)->slider_b->ref();
224
 
    PRIVATE(this)->sensor_b = new SoFieldSensor(ColorEditor::update_b_cb, PRIVATE(this));
225
 
    PRIVATE(this)->sensor_b->attach(&(PRIVATE(this)->slider_b->value));
226
 
    PRIVATE(this)->generateSliderTextureB(this->color.getValue(), FALSE);
227
 
  }
228
 
  if ( PRIVATE(this)->slider_h ) {
229
 
    PRIVATE(this)->slider_h->ref();
230
 
    PRIVATE(this)->sensor_h = new SoFieldSensor(ColorEditor::update_h_cb, PRIVATE(this));
231
 
    PRIVATE(this)->sensor_h->attach(&(PRIVATE(this)->slider_h->value));
232
 
    PRIVATE(this)->generateSliderTextureH(this->color.getValue(), FALSE);
233
 
  }
234
 
  if ( PRIVATE(this)->slider_s ) {
235
 
    PRIVATE(this)->slider_s->ref();
236
 
    PRIVATE(this)->sensor_s = new SoFieldSensor(ColorEditor::update_s_cb, PRIVATE(this));
237
 
    PRIVATE(this)->sensor_s->attach(&(PRIVATE(this)->slider_s->value));
238
 
    PRIVATE(this)->generateSliderTextureS(this->color.getValue(), FALSE);
239
 
 
240
 
  }
241
 
  if ( PRIVATE(this)->slider_v ) {
242
 
    PRIVATE(this)->slider_v->ref();
243
 
    PRIVATE(this)->sensor_v = new SoFieldSensor(ColorEditor::update_v_cb, PRIVATE(this));
244
 
    PRIVATE(this)->sensor_v->attach(&(PRIVATE(this)->slider_v->value));
245
 
    PRIVATE(this)->generateSliderTextureV(this->color.getValue(), FALSE);
246
 
  }
247
 
  if ( PRIVATE(this)->slider_wheel ) {
248
 
    PRIVATE(this)->slider_wheel->ref();
249
 
    PRIVATE(this)->sensor_wheel = new SoFieldSensor(ColorEditor::update_wheel_cb, PRIVATE(this));
250
 
    PRIVATE(this)->sensor_wheel->attach(&(PRIVATE(this)->slider_wheel->value));
251
 
    PRIVATE(this)->generateSliderTextureHSV(this->color.getValue(), FALSE);
252
 
  }
253
 
 
254
 
  PRIVATE(this)->editor->unrefNoDelete();
255
 
  this->setAnyPart("root", scene);
256
 
 
257
 
  PRIVATE(this)->color_sensor = new SoFieldSensor(ColorEditor::color_update_cb, PRIVATE(this));
258
 
  PRIVATE(this)->color_sensor->attach(&(this->color));
259
 
 
260
 
  // SoGuiViewportFix * viewportfix = (SoGuiViewportFix *) SoAny::scanSceneForName(inherited::getSceneGraph(), "viewportfix", FALSE);
261
 
  // assert(viewportfix != NULL);
262
 
  // PRIVATE(this)->editor->objectSize.connectFrom(&(viewportfix->viewportSize));
263
 
}
264
 
 
265
 
SoGuiColorEditor::~SoGuiColorEditor(void)
266
 
{
267
 
  ColorEditor * instance = PRIVATE(this);
268
 
  delete instance;
269
 
}
270
 
 
271
 
// *************************************************************************
272
 
 
273
 
// CAT_MOD BEGIN
274
 
const char *
275
 
ColorEditor::editorscene[] = {
276
 
  "#Inventor V2.1 ascii",
277
 
  "DEF pane SoGuiPane {",
278
 
  "  worldSize 1 1 0",
279
 
  "  objectSize 400 340 0",
280
 
  //   Calculate positions
281
 
  "  SoGuiPosition {",
282
 
  "    position = DEF windowgeometry Calculator {",
283
 
  "      A = USE pane.objectSize",
284
 
  "      B 120 120 0",                               // colorwheel size
285
 
  "      C 140 30 0",                                // clickcounter size
286
 
  "      a 2",                                       // width of frame
287
 
  "      expression [",
288
 
  "        \"oA = vec3f(a, A[1] - C[1] - a, 0)\"",   // clickcounter pos
289
 
  "        \"oB = vec3f(A[0] - B[0] - a, A[1] - B[1] - a, 0)\"",  // colorwheel pos
290
 
  "      ]",
291
 
  "    }.oA",
292
 
  "  }",
293
 
  //   Create a click counter
294
 
  "  DEF switcher SoGuiClickCounter {",
295
 
  "    size = USE windowgeometry.C",
296
 
  "    first 2   # the INTENSITY sliders",
297
 
  "    last 6    # the RGB_HSV sliders",
298
 
  "    value 5   # start with the RGB_V group",
299
 
  "  }",
300
 
  //   Create a frame around the click counter area    
301
 
  "  SoGuiFrame {",
302
 
  "    size = USE windowgeometry.C",
303
 
  "    width 2",
304
 
  "    design EMBOSS",
305
 
  "  }",
306
 
  //   Create labels for the click counter
307
 
  "  Separator {",
308
 
  "    Scale {",
309
 
  "      scaleFactor 2 2 2",
310
 
  "    }",
311
 
  "    DEF sliderlabel SoGuiLabel {",
312
 
  "      which = USE switcher.value",
313
 
  "      text [",
314
 
  "        \"?\"",
315
 
  "        \"NONE\"",
316
 
  "        \"VALUE\"",
317
 
  "        \"RGB\"",
318
 
  "        \"HSV\"",
319
 
  "        \"RGBV\"",
320
 
  "        \"RGBHSV\"",
321
 
  "      ]",
322
 
  "    }",
323
 
  "  }",
324
 
  //   Create a frame around the color wheel
325
 
  "  SoGuiPosition { position = USE windowgeometry.oB }",
326
 
  "  SoGuiFrame {",
327
 
  "    size = USE windowgeometry.B",
328
 
  "    design EMBOSS",
329
 
  "    width 2",
330
 
  "    complement TRUE",
331
 
  "  }",
332
 
  //   Create the color wheel
333
 
  "  DEF colorwheel SoGuiSlider2 {",
334
 
  "    size = USE windowgeometry.B",
335
 
  "  }",
336
 
  //   Position the sliders
337
 
  "  SoGuiPosition {",
338
 
  "    position = DEF slidergeometry Calculator {",
339
 
  "      A = USE pane.objectSize",
340
 
  "      B = USE windowgeometry.B",                    // color wheel size
341
 
  "      a = USE windowgeometry.a",                    // width of frame
342
 
  "      b 10",                                        // space
343
 
  "      c 30",                                        // width of text
344
 
  "      expression [",
345
 
  "        \"ta = (A[1] - (2*a + B[1]) -  7*b) / 6\"", // slider height
346
 
  "        \"tb = A[0] - 2*b - c\"",                   // slider width
347
 
  "        \"oA = vec3f(tb, ta, 0)\"",                 // slider size
348
 
  "        \"oB = vec3f(c+b , b, 0)\"",                // slider pos
349
 
  "        \"oC = vec3f(0, ta + b, 0)\"",              // slider translation
350
 
  "        \"oD = vec3f(-c, 0, 0)\"",                  // slider label translation
351
 
  "      ]",
352
 
  "    }.oB",
353
 
  "  }",
354
 
  //   Create the sliders depending on the click count value
355
 
  "  Switch {",
356
 
  "    whichChild = USE switcher.value",
357
 
  "    Group {", // never to be rendered...
358
 
  "      DEF slider_r SoGuiSlider1 { size = USE slidergeometry.oA }",
359
 
  "      DEF slider_g SoGuiSlider1 { size = USE slidergeometry.oA }",
360
 
  "      DEF slider_b SoGuiSlider1 { size = USE slidergeometry.oA }",
361
 
  "      DEF slider_h SoGuiSlider1 { size = USE slidergeometry.oA }",
362
 
  "      DEF slider_s SoGuiSlider1 { size = USE slidergeometry.oA }",
363
 
  "      DEF slider_v SoGuiSlider1 { size = USE slidergeometry.oA }",
364
 
  "      DEF slider_translation SoGuiTranslation { translation = USE slidergeometry.oC }",
365
 
  "      DEF slider_frame SoGuiFrame {",
366
 
  "        size = USE slidergeometry.oA ",
367
 
  "        design EMBOSS",
368
 
  "        width 2",
369
 
  "        complement TRUE",
370
 
  "      }",
371
 
  "      DEF label_translation SoGuiTranslation { translation = USE slidergeometry.oD }",
372
 
  "      DEF label_scale Scale { scaleFactor 2 2 2 }",
373
 
  "      DEF label_r Separator {",
374
 
  "        USE label_translation",
375
 
  "        USE label_scale",
376
 
  "        SoGuiLabel { text \"R\" }",
377
 
  "      }",
378
 
  "      DEF label_g Separator {",
379
 
  "        USE label_translation",
380
 
  "        USE label_scale",
381
 
  "        SoGuiLabel { text \"G\" }",
382
 
  "      }",
383
 
  "      DEF label_b Separator {",
384
 
  "        USE label_translation",
385
 
  "        USE label_scale",
386
 
  "        SoGuiLabel { text \"B\" }",
387
 
  "      }",
388
 
  "      DEF label_h Separator {",
389
 
  "        USE label_translation",
390
 
  "        USE label_scale",
391
 
  "        SoGuiLabel { text \"H\" }",
392
 
  "      }",
393
 
  "      DEF label_s Separator {",
394
 
  "        USE label_translation",
395
 
  "        USE label_scale",
396
 
  "        SoGuiLabel { text \"S\" }",
397
 
  "      }",
398
 
  "      DEF label_v Separator {",
399
 
  "        USE label_translation",
400
 
  "        USE label_scale",
401
 
  "        SoGuiLabel { text \"V\" }",
402
 
  "      }",
403
 
  "    }",
404
 
  "    DEF sliders_NONE Group {", // in case we search for names once, instead of indexing
405
 
  "    }",                        // children directly inside setCurrentSliders()
406
 
  "    DEF sliders_INTENSITY Group {",
407
 
  "      USE slider_frame",       // frames before sliders - the knobs must paint over frames
408
 
  "      USE slider_v",
409
 
  "      USE label_v",
410
 
  "    }",
411
 
  "    DEF sliders_RGB Group {",
412
 
  "      USE slider_frame",
413
 
  "      USE slider_b",
414
 
  "      USE label_b",
415
 
  "      USE slider_translation",
416
 
  "      USE slider_frame",
417
 
  "      USE slider_g",
418
 
  "      USE label_g",
419
 
  "      USE slider_translation",
420
 
  "      USE slider_frame",
421
 
  "      USE slider_r",
422
 
  "      USE label_r",
423
 
  "    }",
424
 
  "    DEF sliders_HSV Group {",
425
 
  "      USE slider_frame",
426
 
  "      USE slider_v",
427
 
  "      USE label_v",
428
 
  "      USE slider_translation",
429
 
  "      USE slider_frame",
430
 
  "      USE slider_s",
431
 
  "      USE label_s",
432
 
  "      USE slider_translation",
433
 
  "      USE slider_frame",
434
 
  "      USE slider_h",
435
 
  "      USE label_h",
436
 
  "    }",
437
 
  "    DEF sliders_RGB_V Group {",
438
 
  "      USE sliders_INTENSITY",
439
 
  "      USE slider_translation",
440
 
  "      USE sliders_RGB",
441
 
  "    }",
442
 
  "    DEF sliders_RGB_HSV Group {",
443
 
  "      USE sliders_HSV",
444
 
  "      USE slider_translation",
445
 
  "      USE sliders_RGB",
446
 
  "    }",
447
 
  "  } # Switch",
448
 
  "} # Pane pane",
449
 
  NULL
450
 
};
451
 
// CAT_MOD END
452
 
 
453
 
// *************************************************************************
454
 
 
455
 
void
456
 
ColorEditor::colorChange(void)
457
 
{
458
 
  float r = 0.0f, g = 0.0f, b = 0.0f, h = 0.0f, s = 0.0f, v = 0.0f;
459
 
  SbColor color(PUBLIC(this)->color.getValue());
460
 
  color.getValue(r, g, b);
461
 
  color.getHSVValue(h, s, v);
462
 
 
463
 
  SbVec2f wheel(this->calculateFromHue(h, s, v));
464
 
 
465
 
  this->sensor_r->detach();
466
 
  this->sensor_g->detach();
467
 
  this->sensor_b->detach();
468
 
  this->sensor_h->detach();
469
 
  this->sensor_s->detach();
470
 
  this->sensor_v->detach();
471
 
  this->sensor_wheel->detach();
472
 
 
473
 
  if ( r != this->slider_r->value.getValue() ) this->slider_r->value.setValue(r);
474
 
  if ( g != this->slider_g->value.getValue() ) this->slider_g->value.setValue(g);
475
 
  if ( b != this->slider_b->value.getValue() ) this->slider_b->value.setValue(b);
476
 
  if ( h != this->slider_h->value.getValue() ) this->slider_h->value.setValue(h);
477
 
  if ( s != this->slider_s->value.getValue() ) this->slider_s->value.setValue(s);
478
 
  if ( v != this->slider_v->value.getValue() ) this->slider_v->value.setValue(v);
479
 
  if ( wheel != this->slider_wheel->value.getValue() ) this->slider_wheel->value.setValue(wheel);
480
 
 
481
 
  assert(PUBLIC(this) != NULL);
482
 
  if ( PUBLIC(this)->wysiwyg.getValue() ) {
483
 
    this->generateSliderTextureR(color, TRUE);
484
 
    this->generateSliderTextureG(color, TRUE);
485
 
    this->generateSliderTextureB(color, TRUE);
486
 
    this->generateSliderTextureH(color, TRUE);
487
 
    this->generateSliderTextureS(color, TRUE);
488
 
    this->generateSliderTextureV(color, TRUE);
489
 
    this->generateSliderTextureHSV(color, TRUE);
490
 
  }
491
 
 
492
 
  this->sensor_r->attach(&(this->slider_r->value));
493
 
  this->sensor_g->attach(&(this->slider_g->value));
494
 
  this->sensor_b->attach(&(this->slider_b->value));
495
 
  this->sensor_h->attach(&(this->slider_h->value));
496
 
  this->sensor_s->attach(&(this->slider_s->value));
497
 
  this->sensor_v->attach(&(this->slider_v->value));
498
 
  this->sensor_wheel->attach(&(this->slider_wheel->value));
499
 
}
500
 
 
501
 
void
502
 
ColorEditor::color_update_cb(void * closure, SoSensor * sensor)
503
 
{
504
 
  assert(closure != NULL);
505
 
  ColorEditor * me = (ColorEditor *) closure;
506
 
  me->colorChange();
507
 
}
508
 
 
509
 
// *************************************************************************
510
 
 
511
 
void
512
 
ColorEditor::update_r_cb(void * closure, SoSensor * sensor)
513
 
{
514
 
  ColorEditor * me = (ColorEditor *) closure;
515
 
  float r, g, b;
516
 
  SbColor color = PUBLIC(me)->color.getValue();
517
 
  color.getValue(r, g, b);
518
 
  r = me->slider_r->value.getValue();
519
 
  color.setValue(r, g, b);
520
 
  PUBLIC(me)->color.setValue(color);
521
 
}
522
 
 
523
 
void
524
 
ColorEditor::update_g_cb(void * closure, SoSensor * sensor)
525
 
{
526
 
  ColorEditor * me = (ColorEditor *) closure;
527
 
  float r, g, b;
528
 
  SbColor color = PUBLIC(me)->color.getValue();
529
 
  color.getValue(r, g, b);
530
 
  g = me->slider_g->value.getValue();
531
 
  color.setValue(r, g, b);
532
 
  PUBLIC(me)->color.setValue(color);
533
 
}
534
 
 
535
 
void
536
 
ColorEditor::update_b_cb(void * closure, SoSensor * sensor)
537
 
{
538
 
  ColorEditor * me = (ColorEditor *) closure;
539
 
  float r, g, b;
540
 
  SbColor color = PUBLIC(me)->color.getValue();
541
 
  color.getValue(r, g, b);
542
 
  b = me->slider_b->value.getValue();
543
 
  color.setValue(r, g, b);
544
 
  PUBLIC(me)->color.setValue(color);
545
 
}
546
 
 
547
 
void
548
 
ColorEditor::update_h_cb(void * closure, SoSensor * sensor)
549
 
{
550
 
  ColorEditor * me = (ColorEditor *) closure;
551
 
  float h, s, v;
552
 
  SbColor color = PUBLIC(me)->color.getValue();
553
 
  color.getHSVValue(h, s, v);
554
 
  h = me->slider_h->value.getValue();
555
 
  color.setHSVValue(h, s, v);
556
 
  PUBLIC(me)->color.setValue(color);
557
 
}
558
 
 
559
 
void
560
 
ColorEditor::update_s_cb(void * closure, SoSensor * sensor)
561
 
{
562
 
  ColorEditor * me = (ColorEditor *) closure;
563
 
  float h, s, v;
564
 
  SbColor color = PUBLIC(me)->color.getValue();
565
 
  color.getHSVValue(h, s, v);
566
 
  s = me->slider_s->value.getValue();
567
 
  color.setHSVValue(h, s, v);
568
 
  PUBLIC(me)->color.setValue(color);
569
 
}
570
 
 
571
 
void
572
 
ColorEditor::update_v_cb(void * closure, SoSensor * sensor)
573
 
{
574
 
  ColorEditor * me = (ColorEditor *) closure;
575
 
  float h, s, v;
576
 
  SbColor color = PUBLIC(me)->color.getValue();
577
 
  color.getHSVValue(h, s, v);
578
 
  v = me->slider_v->value.getValue();
579
 
  color.setHSVValue(h, s, v);
580
 
  PUBLIC(me)->color.setValue(color);
581
 
}
582
 
 
583
 
void
584
 
ColorEditor::update_wheel_cb(void * closure, SoSensor * sensor)
585
 
{
586
 
  ColorEditor * me = (ColorEditor *) closure;
587
 
  SbVec2f value = me->slider_wheel->value.getValue();
588
 
  value = value * 2.0f - SbVec2f(1.0f, 1.0f);
589
 
  if ( value.length() > 1.0f ) {
590
 
    value.normalize();
591
 
    SbVec2f reverse = (value + SbVec2f(1.0f, 1.0f)) / 2.0f;
592
 
    me->slider_wheel->value.setValue(reverse);
593
 
  }
594
 
  float h;
595
 
  if ( value[0] == 0.0f ) h = ((value[1] < 0.0f) ? 3.0f : 1.0f) * float(M_PI) / 2.0f;
596
 
  else h = (float) atan(value[1] / value[0]);
597
 
  if ( value[0] < 0.0f ) h += float(M_PI); 
598
 
  if ( h < 0.0f ) h += (2.0f * float(M_PI));
599
 
  h /= 2.0f * float(M_PI); 
600
 
  float s = SoQtMin(value.length(), 1.0f); // float precision bugfix
601
 
 
602
 
  float a = 0.0f, b = 0.0f, v = 1.0f;
603
 
  SbColor existing = PUBLIC(me)->color.getValue();
604
 
  existing.getHSVValue(a, b, v);
605
 
 
606
 
  SbColor color;
607
 
  color.setHSVValue(h, s, v);
608
 
  PUBLIC(me)->color.setValue(color);
609
 
}
610
 
 
611
 
// *************************************************************************
612
 
 
613
 
float
614
 
ColorEditor::calculateHue(float x, float y)
615
 
{
616
 
  float hue;
617
 
  if ( x == 0.0f ) hue = ((y < 0.0f) ? 3.0f : 1.0f) * float(M_PI) / 2.0f;
618
 
  else hue = (float) atan(y / x);
619
 
  if ( x < 0.0f ) hue += float(M_PI); 
620
 
  if ( hue < 0.0f ) hue += (2.0f * float(M_PI));
621
 
  return hue / (2.0f * float(M_PI)); 
622
 
}
623
 
 
624
 
SbVec2f
625
 
ColorEditor::calculateFromHue(float h, float s, float v)
626
 
{
627
 
  float hue = h * 2.0f * float(M_PI);
628
 
  return SbVec2f(((float) cos(hue) * s + 1.0f) / 2.0f, ((float) sin(hue) * s + 1.0f) / 2.0f);
629
 
}
630
 
 
631
 
// *************************************************************************
632
 
 
633
 
void
634
 
ColorEditor::generateSliderTextureR(const SbColor & current, SbBool wysiwyg)
635
 
{
636
 
  assert(this->slider_r != NULL);
637
 
  float red, green, blue;
638
 
  current.getValue(red, green, blue);
639
 
  if ( ! wysiwyg ) {
640
 
    green = blue = 0.0f;
641
 
  }
642
 
  SoTexture2 * texture = SO_GET_PART(this->slider_r, "surfaceTexture", SoTexture2);
643
 
  assert(texture);
644
 
  texture->wrapS.setValue(SoTexture2::CLAMP);
645
 
  texture->wrapT.setValue(SoTexture2::CLAMP);
646
 
  SbVec2s size(256, 1);
647
 
  int nc = 3;
648
 
  texture->image.setValue(size, nc, NULL);
649
 
  texture->model.setValue(SoTexture2::DECAL);
650
 
  unsigned char * bytes = texture->image.startEditing(size, nc);
651
 
  int x, y;
652
 
  for ( x = 0; x < size[0]; x++ ) { 
653
 
    red = (float) x / (float) (size[0] - 1);
654
 
    for ( y = 0; y < size[1]; y++ ) { 
655
 
      bytes[(size[0]*y+x)*nc+0] = (unsigned char) (red * 255.0f);
656
 
      bytes[(size[0]*y+x)*nc+1] = (unsigned char) (green * 255.0f);
657
 
      bytes[(size[0]*y+x)*nc+2] = (unsigned char) (blue * 255.0f);
658
 
      // if ( nc > 3 ) bytes[(size[0]*y+x)*nc+4] = 255;
659
 
    }
660
 
  }
661
 
  texture->image.finishEditing();
662
 
}
663
 
 
664
 
void
665
 
ColorEditor::generateSliderTextureG(const SbColor & current, SbBool wysiwyg)
666
 
{
667
 
  assert(this->slider_g != NULL);
668
 
  float red, green, blue;
669
 
  current.getValue(red, green, blue);
670
 
  if ( ! wysiwyg ) {
671
 
    red = blue = 0.0f;
672
 
  }
673
 
  SoTexture2 * texture = SO_GET_PART(this->slider_g, "surfaceTexture", SoTexture2);
674
 
  assert(texture);
675
 
  texture->wrapS.setValue(SoTexture2::CLAMP);
676
 
  texture->wrapT.setValue(SoTexture2::CLAMP);
677
 
  SbVec2s size(256, 1);
678
 
  int nc = 3;
679
 
  texture->image.setValue(size, nc, NULL);
680
 
  texture->model.setValue(SoTexture2::DECAL);
681
 
  unsigned char * bytes = texture->image.startEditing(size, nc);
682
 
  int x, y;
683
 
  for ( x = 0; x < size[0]; x++ ) { 
684
 
    green = (float) x / (float) (size[0] - 1);
685
 
    for ( y = 0; y < size[1]; y++ ) { 
686
 
      bytes[(size[0]*y+x)*nc+0] = (unsigned char) (red * 255.0f);
687
 
      bytes[(size[0]*y+x)*nc+1] = (unsigned char) (green * 255.0f);
688
 
      bytes[(size[0]*y+x)*nc+2] = (unsigned char) (blue * 255.0f);
689
 
      // if ( nc > 3 ) bytes[(size[0]*y+x)*nc+4] = 255;
690
 
    }
691
 
  }
692
 
  texture->image.finishEditing();
693
 
}
694
 
 
695
 
void
696
 
ColorEditor::generateSliderTextureB(const SbColor & current, SbBool wysiwyg)
697
 
{
698
 
  assert(this->slider_b != NULL);
699
 
  float red, green, blue;
700
 
  current.getValue(red, green, blue);
701
 
  if ( ! wysiwyg ) {
702
 
    red = green = 0.0f;
703
 
  }
704
 
  SoTexture2 * texture = SO_GET_PART(this->slider_b, "surfaceTexture", SoTexture2);
705
 
  assert(texture);
706
 
  texture->wrapS.setValue(SoTexture2::CLAMP);
707
 
  texture->wrapT.setValue(SoTexture2::CLAMP);
708
 
  SbVec2s size(256, 1);
709
 
  int nc = 3;
710
 
  texture->image.setValue(size, nc, NULL);
711
 
  texture->model.setValue(SoTexture2::DECAL);
712
 
  unsigned char * bytes = texture->image.startEditing(size, nc);
713
 
  int x, y;
714
 
  for ( x = 0; x < size[0]; x++ ) { 
715
 
    blue = (float) x / (float) (size[0] - 1);
716
 
    for ( y = 0; y < size[1]; y++ ) { 
717
 
      bytes[(size[0]*y+x)*nc+0] = (unsigned char) (red * 255.0f);
718
 
      bytes[(size[0]*y+x)*nc+1] = (unsigned char) (green * 255.0f);
719
 
      bytes[(size[0]*y+x)*nc+2] = (unsigned char) (blue * 255.0f);
720
 
      // if ( nc > 3 ) bytes[(size[0]*y+x)*nc+4] = 255;
721
 
    }
722
 
  }
723
 
  texture->image.finishEditing();
724
 
}
725
 
 
726
 
void
727
 
ColorEditor::generateSliderTextureH(const SbColor & current, SbBool wysiwyg)
728
 
{
729
 
  assert(this->slider_h != NULL);
730
 
  float hue, saturation, value;
731
 
  current.getHSVValue(hue, saturation, value);
732
 
  if ( ! wysiwyg ) {
733
 
    saturation = 1.0f;
734
 
    value = 1.0f;
735
 
  }
736
 
  SoTexture2 * texture = SO_GET_PART(this->slider_h, "surfaceTexture", SoTexture2);
737
 
  assert(texture);
738
 
  texture->wrapS.setValue(SoTexture2::CLAMP);
739
 
  texture->wrapT.setValue(SoTexture2::CLAMP);
740
 
  SbVec2s size(256, 1);
741
 
  int nc = 3;
742
 
  texture->image.setValue(size, nc, NULL);
743
 
  texture->model.setValue(SoTexture2::DECAL);
744
 
  unsigned char * bytes = texture->image.startEditing(size, nc);
745
 
  int x, y;
746
 
  for ( x = 0; x < size[0]; x++ ) { 
747
 
    const float hue = (float) x / (float) (size[0] - 1);
748
 
    for ( y = 0; y < size[1]; y++ ) { 
749
 
      float r = 0.0f, g = 0.0f, b = 0.0f;
750
 
      SbColor color(r, g, b); 
751
 
      color.setHSVValue(hue, saturation, value);
752
 
      color.getValue(r, g, b);
753
 
      bytes[(size[0]*y+x)*nc+0] = (unsigned char) (r * 255.0f);
754
 
      bytes[(size[0]*y+x)*nc+1] = (unsigned char) (g * 255.0f);
755
 
      bytes[(size[0]*y+x)*nc+2] = (unsigned char) (b * 255.0f);
756
 
      // if ( nc > 3 ) bytes[(size[0]*y+x)*nc+4] = 255;
757
 
    }
758
 
  }
759
 
  texture->image.finishEditing();
760
 
}
761
 
 
762
 
void
763
 
ColorEditor::generateSliderTextureS(const SbColor & current, SbBool wysiwyg)
764
 
{
765
 
  assert(this->slider_s != NULL);
766
 
  float hue, saturation, value;
767
 
  current.getHSVValue(hue, saturation, value);
768
 
  if ( ! wysiwyg ) {
769
 
    hue = 0.0f;
770
 
    value = 1.0f;
771
 
  }
772
 
  SoTexture2 * texture = SO_GET_PART(this->slider_s, "surfaceTexture", SoTexture2);
773
 
  assert(texture);
774
 
  texture->wrapS.setValue(SoTexture2::CLAMP);
775
 
  texture->wrapT.setValue(SoTexture2::CLAMP);
776
 
  SbVec2s size(256, 1);
777
 
  int nc = 3;
778
 
  texture->image.setValue(size, nc, NULL);
779
 
  texture->model.setValue(SoTexture2::DECAL);
780
 
  unsigned char * bytes = texture->image.startEditing(size, nc);
781
 
  int x, y;
782
 
  for ( x = 0; x < size[0]; x++ ) { 
783
 
    const float saturation = (float) x / (float) (size[0] - 1);
784
 
    for ( y = 0; y < size[1]; y++ ) { 
785
 
      float r = 0.0f, g = 0.0f, b = 0.0f;
786
 
      SbColor color(r, g, b); 
787
 
      color.setHSVValue(hue, saturation, value);
788
 
      color.getValue(r, g, b);
789
 
      bytes[(size[0]*y+x)*nc+0] = (unsigned char) (r * 255.0f);
790
 
      bytes[(size[0]*y+x)*nc+1] = (unsigned char) (g * 255.0f);
791
 
      bytes[(size[0]*y+x)*nc+2] = (unsigned char) (b * 255.0f);
792
 
      // if ( nc > 3 ) bytes[(size[0]*y+x)*nc+4] = 255;
793
 
    }
794
 
  }
795
 
  texture->image.finishEditing();
796
 
}
797
 
 
798
 
void
799
 
ColorEditor::generateSliderTextureV(const SbColor & current, SbBool wysiwyg)
800
 
{
801
 
  assert(this->slider_v != NULL);
802
 
  float hue, saturation, value;
803
 
  current.getHSVValue(hue, saturation, value);
804
 
  if ( ! wysiwyg ) {
805
 
    hue = 0.0f;
806
 
    saturation = 1.0f;
807
 
  }
808
 
  SoTexture2 * texture = SO_GET_PART(this->slider_v, "surfaceTexture", SoTexture2);
809
 
  assert(texture);
810
 
  texture->wrapS.setValue(SoTexture2::CLAMP);
811
 
  texture->wrapT.setValue(SoTexture2::CLAMP);
812
 
  SbVec2s size(256, 1);
813
 
  int nc = 3;
814
 
  texture->image.setValue(size, nc, NULL);
815
 
  texture->model.setValue(SoTexture2::DECAL);
816
 
  unsigned char * bytes = texture->image.startEditing(size, nc);
817
 
  int x, y;
818
 
  for ( x = 0; x < size[0]; x++ ) { 
819
 
    const float value = (float) x / (float) (size[0] - 1);
820
 
    for ( y = 0; y < size[1]; y++ ) { 
821
 
      float r = 0.0f, g = 0.0f, b = 0.0f;
822
 
      SbColor color(r, g, b); 
823
 
      color.setHSVValue(hue, saturation, value);
824
 
      color.getValue(r, g, b);
825
 
      bytes[(size[0]*y+x)*nc+0] = (unsigned char) (r * 255.0f);
826
 
      bytes[(size[0]*y+x)*nc+1] = (unsigned char) (g * 255.0f);
827
 
      bytes[(size[0]*y+x)*nc+2] = (unsigned char) (b * 255.0f);
828
 
      // if ( nc > 3 ) bytes[(size[0]*y+x)*nc+4] = 255;
829
 
    }
830
 
  }
831
 
  texture->image.finishEditing();
832
 
}
833
 
 
834
 
void
835
 
ColorEditor::generateSliderTextureHSV(const SbColor & current, SbBool wysiwyg)
836
 
{
837
 
  assert(this->slider_wheel != NULL);
838
 
  float hue, saturation, value;
839
 
  current.getHSVValue(hue, saturation, value);
840
 
  if ( ! wysiwyg ) {
841
 
    value = 1.0f;
842
 
  }
843
 
 
844
 
  SoTexture2 * texture = SO_GET_PART(this->slider_wheel, "surfaceTexture", SoTexture2);
845
 
  assert(texture);
846
 
  texture->wrapS.setValue(SoTexture2::CLAMP);
847
 
  texture->wrapT.setValue(SoTexture2::CLAMP);
848
 
  SbVec2s size(256, 256);
849
 
  int nc = 3;
850
 
  texture->image.setValue(size, nc, NULL);
851
 
  texture->model.setValue(SoTexture2::DECAL);
852
 
  unsigned char * bytes = texture->image.startEditing(size, nc);
853
 
  int x, y;
854
 
  const float halfx = (float) size[0] / 2.0f;
855
 
  const float halfy = (float) size[1] / 2.0f;
856
 
  for ( y = 0; y < size[1]; y++ ) { 
857
 
    const float ypos = ((float) y - halfy) / halfy;
858
 
    for ( x = 0; x < size[0]; x++ ) { 
859
 
      const float xpos = ((float) x - halfx) / halfx; 
860
 
      const float saturation = (float) sqrt(xpos * xpos + ypos * ypos);
861
 
      float hue = ColorEditor::calculateHue(xpos, ypos);
862
 
      float red = 0.0f, green = 0.0f, blue = 0.0f;
863
 
      SbColor color(red, green, blue); 
864
 
      if ( saturation <= 1.0f ) color.setHSVValue(hue, saturation, value);
865
 
      color.getValue(red, green, blue);
866
 
      bytes[(size[0]*y+x)*nc+0] = (unsigned char) (red * 255.0f);
867
 
      bytes[(size[0]*y+x)*nc+1] = (unsigned char) (green * 255.0f);
868
 
      bytes[(size[0]*y+x)*nc+2] = (unsigned char) (blue * 255.0f);
869
 
      // if ( nc > 3 ) bytes[(size[0]*y+x)*nc+4] = 255;
870
 
    }
871
 
  }
872
 
  texture->image.finishEditing();
873
 
}
874
 
 
875
 
// *************************************************************************
876
 
 
877
 
#undef PUBLIC
878
 
#undef PRIVATE
879