Debian Codegraph @

src/App/ColorModel.h

raw
1 /***************************************************************************
2 * Copyright (c) 2005 Werner Mayer <wmayer[at]users.sourceforge.net> *
3 * *
4 * This file is part of the FreeCAD CAx development system. *
5 * *
6 * This library is free software; you can redistribute it and/or *
7 * modify it under the terms of the GNU Library General Public *
8 * License as published by the Free Software Foundation; either *
9 * version 2 of the License, or (at your option) any later version. *
10 * *
11 * This library is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU Library General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU Library General Public *
17 * License along with this library; see the file COPYING.LIB. If not, *
18 * write to the Free Software Foundation, Inc., 59 Temple Place, *
19 * Suite 330, Boston, MA 02111-1307, USA *
20 * *
21 ***************************************************************************/
22
23
24 #ifndef APP_COLORMODEL_H
25 #define APP_COLORMODEL_H
26
27 #include "Material.h"
28 #include <Base/Bitmask.h>
29
30 #include <algorithm>
31 #include <deque>
32 #include <string>
33 #include <vector>
34
35 namespace App
36 {
37
38 enum class Visibility
39 {
40 Default,
41 Grayed,
42 Invisible
43 };
44
45 using VisibilityFlags = Base::Flags<Visibility>;
46
47 enum class ColorBarStyle
48 {
49 FLOW,
50 ZERO_BASED
51 };
52
53 } // namespace App
54
55 ENABLE_BITMASK_OPERATORS(App::Visibility)
56
57 namespace App
58 {
59
60 /**
61 * Abstract base class that calculates the matching RGB color to a given value.
62 */
63 class AppExport ValueFloatToRGB
64 {
65 public:
66 virtual Base::Color getColor(float fVal) const = 0;
67
68 protected:
69 ValueFloatToRGB() = default;
70 virtual ~ValueFloatToRGB() = default;
71 };
72
73
74 class AppExport ColorModel
75 {
76 public:
77 ColorModel() = default;
78 explicit ColorModel(std::size_t usCt)
79 {
80 colors.resize(usCt);
81 }
82 ColorModel(const ColorModel&) = default;
83 virtual ~ColorModel() = default;
84 ColorModel& operator=(const ColorModel&) = default;
85 std::size_t getCountColors() const
86 {
87 return colors.size();
88 }
89 std::vector<Base::Color> colors;
90 };
91
92 class AppExport ColorModelBlueGreenRed: public ColorModel
93 {
94 public:
95 ColorModelBlueGreenRed()
96 : ColorModel(5)
97 {
98 colors[0] = Base::Color(0, 0, 1);
99 colors[1] = Base::Color(0, 1, 1);
100 colors[2] = Base::Color(0, 1, 0);
101 colors[3] = Base::Color(1, 1, 0);
102 colors[4] = Base::Color(1, 0, 0);
103 }
104 };
105
106 class AppExport ColorModelBlueCyanGreen: public ColorModel
107 {
108 public:
109 ColorModelBlueCyanGreen()
110 : ColorModel(3)
111 {
112 colors[0] = Base::Color(0, 0, 1);
113 colors[1] = Base::Color(0, 1, 1);
114 colors[2] = Base::Color(0, 1, 0);
115 }
116 };
117
118 class AppExport ColorModelGreenYellowRed: public ColorModel
119 {
120 public:
121 ColorModelGreenYellowRed()
122 : ColorModel(3)
123 {
124 colors[0] = Base::Color(0, 1, 0);
125 colors[1] = Base::Color(1, 1, 0);
126 colors[2] = Base::Color(1, 0, 0);
127 }
128 };
129
130 class AppExport ColorModelRedGreenBlue: public ColorModel
131 {
132 public:
133 ColorModelRedGreenBlue()
134 : ColorModel(5)
135 {
136 colors[0] = Base::Color(1, 0, 0);
137 colors[1] = Base::Color(1, 1, 0);
138 colors[2] = Base::Color(0, 1, 0);
139 colors[3] = Base::Color(0, 1, 1);
140 colors[4] = Base::Color(0, 0, 1);
141 }
142 };
143
144 class AppExport ColorModelGreenCyanBlue: public ColorModel
145 {
146 public:
147 ColorModelGreenCyanBlue()
148 : ColorModel(3)
149 {
150 colors[0] = Base::Color(0, 1, 0);
151 colors[1] = Base::Color(0, 1, 1);
152 colors[2] = Base::Color(0, 0, 1);
153 }
154 };
155
156 class AppExport ColorModelRedYellowGreen: public ColorModel
157 {
158 public:
159 ColorModelRedYellowGreen()
160 : ColorModel(3)
161 {
162 colors[0] = Base::Color(1, 0, 0);
163 colors[1] = Base::Color(1, 1, 0);
164 colors[2] = Base::Color(0, 1, 0);
165 }
166 };
167
168 class AppExport ColorModelBlueWhiteRed: public ColorModel
169 {
170 public:
171 ColorModelBlueWhiteRed()
172 : ColorModel(5)
173 {
174 colors[0] = Base::Color(0, 0, 1);
175 colors[1] = Base::Color(float(85.0 / 255), float(170.0 / 255), 1);
176 colors[2] = Base::Color(1, 1, 1);
177 colors[3] = Base::Color(1, float(85.0 / 255), 0);
178 colors[4] = Base::Color(1, 0, 0);
179 }
180 };
181
182 class AppExport ColorModelBlueWhite: public ColorModel
183 {
184 public:
185 ColorModelBlueWhite()
186 : ColorModel(3)
187 {
188 colors[0] = Base::Color(0, 0, 1);
189 colors[1] = Base::Color(float(85.0 / 255), float(170.0 / 255), 1);
190 colors[2] = Base::Color(1, 1, 1);
191 }
192 };
193
194 class AppExport ColorModelWhiteRed: public ColorModel
195 {
196 public:
197 ColorModelWhiteRed()
198 : ColorModel(3)
199 {
200 colors[0] = Base::Color(1, 1, 1);
201 colors[1] = Base::Color(1, float(85.0 / 255), 0);
202 colors[2] = Base::Color(1, 0, 0);
203 }
204 };
205
206 class AppExport ColorModelBlackWhite: public ColorModel
207 {
208 public:
209 ColorModelBlackWhite()
210 : ColorModel(2)
211 {
212 colors[0] = Base::Color(0, 0, 0);
213 colors[1] = Base::Color(1, 1, 1);
214 }
215 };
216
217 class AppExport ColorModelBlackGray: public ColorModel
218 {
219 public:
220 ColorModelBlackGray()
221 : ColorModel(2)
222 {
223 colors[0] = Base::Color(0.0f, 0.0f, 0.0f);
224 colors[1] = Base::Color(0.5f, 0.5f, 0.5f);
225 }
226 };
227
228 class AppExport ColorModelGrayWhite: public ColorModel
229 {
230 public:
231 ColorModelGrayWhite()
232 : ColorModel(2)
233 {
234 colors[0] = Base::Color(0.5f, 0.5f, 0.5f);
235 colors[1] = Base::Color(1.0f, 1.0f, 1.0f);
236 }
237 };
238
239 class AppExport ColorModelWhiteBlack: public ColorModel
240 {
241 public:
242 ColorModelWhiteBlack()
243 : ColorModel(2)
244 {
245 colors[0] = Base::Color(1, 1, 1);
246 colors[1] = Base::Color(0, 0, 0);
247 }
248 };
249
250 class AppExport ColorModelWhiteGray: public ColorModel
251 {
252 public:
253 ColorModelWhiteGray()
254 : ColorModel(2)
255 {
256 colors[0] = Base::Color(1.0f, 1.0f, 1.0f);
257 colors[1] = Base::Color(0.5f, 0.5f, 0.5f);
258 }
259 };
260
261 class AppExport ColorModelGrayBlack: public ColorModel
262 {
263 public:
264 ColorModelGrayBlack()
265 : ColorModel(2)
266 {
267 colors[0] = Base::Color(0.5f, 0.5f, 0.5f);
268 colors[1] = Base::Color(0.0f, 0.0f, 0.0f);
269 }
270 };
271
272 struct AppExport ColorModelPack
273 {
274 ColorModel totalModel = ColorModelBlueGreenRed();
275 ColorModel topModel = ColorModelGreenYellowRed();
276 ColorModel bottomModel = ColorModelBlueCyanGreen();
277 std::string description;
278 static ColorModelPack createRedGreenBlue();
279 static ColorModelPack createBlueGreenRed();
280 static ColorModelPack createWhiteBlack();
281 static ColorModelPack createBlackWhite();
282 static ColorModelPack createRedWhiteBlue();
283 };
284
285 class AppExport ColorField
286 {
287 public:
288 ColorField();
289 ColorField(const ColorField& rclCF) = default;
290 ColorField(const ColorModel& rclModel, float fMin, float fMax, std::size_t usCt);
291 virtual ~ColorField() = default;
292
293 ColorField& operator=(const ColorField& rclCF);
294
295 std::size_t getCountColors() const
296 {
297 return ctColors;
298 }
299 void set(const ColorModel& rclModel, float fMin, float fMax, std::size_t usCt);
300 void setCountColors(std::size_t usCt)
301 {
302 set(colorModel, fMin, fMax, usCt);
303 }
304 void setRange(float fMin, float fMax)
305 {
306 set(colorModel, fMin, fMax, ctColors);
307 }
308 void getRange(float& rfMin, float& rfMax)
309 {
310 rfMin = fMin;
311 rfMax = fMax;
312 }
313 std::size_t getMinColors() const
314 {
315 return colorModel.getCountColors();
316 }
317 void setColorModel(const ColorModel& rclModel);
318 const ColorModel& getColorModel() const
319 {
320 return colorModel;
321 }
322 void setDirect(std::size_t usInd, Base::Color clCol)
323 {
324 colorField[usInd] = clCol;
325 }
326 float getMinValue() const
327 {
328 return fMin;
329 }
330 float getMaxValue() const
331 {
332 return fMax;
333 }
334
335 Base::Color getColor(std::size_t usIndex) const
336 {
337 return colorField[usIndex];
338 }
339 inline Base::Color getColor(float fVal) const;
340 inline std::size_t getColorIndex(float fVal) const;
341
342 protected:
343 ColorModel colorModel;
344 float fMin, fMax;
345 float fAscent, fConstant; // Index := _fConstant + _fAscent * WERT
346 std::size_t ctColors;
347 std::vector<Base::Color> colorField;
348
349 void rebuild();
350 void interpolate(Base::Color clCol1, std::size_t usPos1, Base::Color clCol2, std::size_t usPos2);
351 };
352
353 inline Base::Color ColorField::getColor(float fVal) const
354 {
355 // if the value is outside or at the border of the range
356 std::size_t ct = colorModel.getCountColors() - 1;
357 if (fVal <= fMin) {
358 return colorModel.colors[0];
359 }
360 else if (fVal >= fMax) {
361 return colorModel.colors[ct];
362 }
363
364 // get the Base::Color field position (with 0 < t < 1)
365 float t = (fVal - fMin) / (fMax - fMin);
366 Base::Color col(1.0f, 1.0f, 1.0f); // white as default
367 for (std::size_t i = 0; i < ct; i++) {
368 float r = (float)(i + 1) / (float)ct;
369 if (t < r) {
370 // calculate the exact position in the subrange
371 float s = t * float(ct) - float(i);
372 Base::Color c1 = colorModel.colors[i];
373 Base::Color c2 = colorModel.colors[i + 1];
374 col.r = (1.0f - s) * c1.r + s * c2.r;
375 col.g = (1.0f - s) * c1.g + s * c2.g;
376 col.b = (1.0f - s) * c1.b + s * c2.b;
377 break;
378 }
379 }
380
381 return col;
382 }
383
384 inline std::size_t ColorField::getColorIndex(float fVal) const
385 {
386 return std::size_t(
387 std::min<int>(std::max<int>(int(fConstant + fAscent * fVal), 0), int(ctColors - 1)));
388 }
389
390 struct AppExport ColorGradientProfile
391 {
392 ColorBarStyle tStyle {ColorBarStyle::FLOW};
393 float fMin {};
394 float fMax {};
395 std::size_t ctColors {};
396 std::size_t tColorModel {};
397 VisibilityFlags visibility {Visibility::Default};
398
399 ColorGradientProfile();
400 ColorGradientProfile(const ColorGradientProfile&) = default;
401 ColorGradientProfile& operator=(const ColorGradientProfile&) = default;
402
403 bool isEqual(const ColorGradientProfile&) const;
404 };
405
406 class AppExport ColorGradient
407 {
408 public:
409 ColorGradient();
410 ColorGradient(float fMin,
411 float fMax,
412 std::size_t usCtColors,
413 ColorBarStyle tS,
414 VisibilityFlags fl = Visibility::Default);
415 ColorGradient(const ColorGradient&) = default;
416 ColorGradient& operator=(const ColorGradient&) = default;
417 const ColorGradientProfile& getProfile() const
418 {
419 return profile;
420 }
421 void setProfile(const ColorGradientProfile& pro);
422
423 void set(float fMin, float fMax, std::size_t usCt, ColorBarStyle tS, VisibilityFlags fl);
424 void setRange(float fMin, float fMax)
425 {
426 set(fMin, fMax, profile.ctColors, profile.tStyle, profile.visibility);
427 }
428 void getRange(float& rfMin, float& rfMax) const
429 {
430 rfMin = profile.fMin;
431 rfMax = profile.fMax;
432 }
433 bool isOutOfRange(float fVal) const
434 {
435 return ((fVal < profile.fMin) || (fVal > profile.fMax));
436 }
437 std::size_t getCountColors() const
438 {
439 return profile.ctColors;
440 }
441 void setCountColors(std::size_t usCt)
442 {
443 set(profile.fMin, profile.fMax, usCt, profile.tStyle, profile.visibility);
444 }
445 void setStyle(ColorBarStyle tS)
446 {
447 set(profile.fMin, profile.fMax, profile.ctColors, tS, profile.visibility);
448 }
449 std::size_t getMinColors() const;
450 ColorBarStyle getStyle() const
451 {
452 return profile.tStyle;
453 }
454 void setOutsideGrayed(bool value)
455 {
456 profile.visibility.setFlag(Visibility::Grayed, value);
457 }
458 bool isOutsideGrayed() const
459 {
460 return profile.visibility.testFlag(Visibility::Grayed);
461 }
462 void setOutsideInvisible(bool value)
463 {
464 profile.visibility.setFlag(Visibility::Invisible, value);
465 }
466 bool isOutsideInvisible() const
467 {
468 return profile.visibility.testFlag(Visibility::Invisible);
469 }
470 void setColorModel(std::size_t tModel);
471 std::size_t getColorModelType() const
472 {
473 return profile.tColorModel;
474 }
475 inline const ColorModel& getColorModel() const;
476 std::vector<std::string> getColorModelNames() const;
477 float getMinValue() const
478 {
479 return profile.fMin;
480 }
481 float getMaxValue() const
482 {
483 return profile.fMax;
484 }
485
486 inline Base::Color getColor(float fVal) const;
487 inline std::size_t getColorIndex(float fVal) const;
488
489 private:
490 inline Base::Color _getColor(float fVal) const;
491
492 protected:
493 void createStandardPacks();
494
495 protected:
496 ColorGradientProfile profile;
497 ColorField colorField1, colorField2;
498 ColorModelPack currentModelPack;
499 std::vector<ColorModelPack> modelPacks;
500
501 void rebuild();
502 void setColorModel();
503 };
504
505
506 class AppExport ColorLegend
507 {
508 public:
509 ColorLegend();
510 ColorLegend(const ColorLegend& rclCL) = default;
511 virtual ~ColorLegend() = default;
512
513 ColorLegend& operator=(const ColorLegend& rclCL) = default;
514 bool operator==(const ColorLegend& rclCL) const;
515 bool operator!=(const ColorLegend& rclCL) const
516 {
517 return !(*this == rclCL);
518 }
519
520 void resize(std::size_t ulN);
521 std::size_t addMin(const std::string& rclName);
522 std::size_t addMax(const std::string& rclName);
523 bool remove(std::size_t ulPos);
524 void removeFirst();
525 void removeLast();
526
527 Base::Color getColor(std::size_t ulPos) const;
528 uint32_t getPackedColor(std::size_t ulPos) const;
529 bool setColor(std::size_t ulPos, float ucRed, float ucGreen, float ucBlue);
530 bool setColor(std::size_t ulPos, unsigned long ulColor);
531 float getValue(std::size_t ulPos) const;
532 bool setValue(std::size_t ulPos, float fVal);
533 std::string getText(std::size_t ulPos) const;
534 bool setText(std::size_t ulPos, const std::string& rclName);
535 std::size_t hasNumberOfFields() const
536 {
537 return colorFields.size();
538 }
539 void setOutsideGrayed(bool bOS)
540 {
541 outsideGrayed = bOS;
542 }
543 bool isOutsideGrayed() const
544 {
545 return outsideGrayed;
546 }
547 inline float getMinValue() const;
548 inline float getMaxValue() const;
549
550 inline Base::Color getColor(float fVal) const;
551 inline std::size_t getColorIndex(float fVal) const;
552
553 protected:
554 std::deque<Base::Color> colorFields;
555 std::deque<std::string> names;
556 std::deque<float> values;
557 bool outsideGrayed {false};
558 };
559
560 inline Base::Color ColorLegend::getColor(float fVal) const
561 {
562 std::deque<float>::const_iterator pI;
563 for (pI = values.begin(); pI != values.end(); ++pI) {
564 if (fVal < *pI) {
565 break;
566 }
567 }
568
569 if (outsideGrayed) {
570 if ((pI == values.begin()) || (pI == values.end())) {
571 return Base::Color(0.5f, 0.5f, 0.5f);
572 }
573 else {
574 return colorFields[pI - values.begin() - 1];
575 }
576 }
577
578 if (pI == values.begin()) {
579 return *colorFields.begin();
580 }
581 else if (pI == values.end()) {
582 return *(colorFields.end() - 1);
583 }
584 else {
585 return colorFields[pI - values.begin() - 1];
586 }
587 }
588
589 inline std::size_t ColorLegend::getColorIndex(float fVal) const
590 {
591 std::deque<float>::const_iterator pI;
592 for (pI = values.begin(); pI != values.end(); ++pI) {
593 if (fVal < *pI) {
594 break;
595 }
596 }
597
598 if (pI == values.begin()) {
599 return 0;
600 }
601 else if (pI == values.end()) {
602 return (std::size_t)(colorFields.size() - 1);
603 }
604 else {
605 return pI - values.begin() - 1;
606 }
607 }
608
609 inline float ColorLegend::getMinValue() const
610 {
611 return values.front();
612 }
613
614 inline float ColorLegend::getMaxValue() const
615 {
616 return values.back();
617 }
618
619 inline Base::Color ColorGradient::getColor(float fVal) const
620 {
621 Base::Color Color = _getColor(fVal);
622 if (isOutsideInvisible()) {
623 if (isOutOfRange(fVal)) {
624 Color.a = 0.2F;
625 }
626 }
627
628 return Color;
629 }
630
631 inline Base::Color ColorGradient::_getColor(float fVal) const
632 {
633 if (isOutsideGrayed()) {
634 if (isOutOfRange(fVal)) {
635 return Base::Color(0.5f, 0.5f, 0.5f);
636 }
637 }
638
639 switch (profile.tStyle) {
640 case ColorBarStyle::ZERO_BASED: {
641 if ((profile.fMin < 0.0f) && (profile.fMax > 0.0f)) {
642 if (fVal < 0.0f) {
643 return colorField1.getColor(fVal);
644 }
645 else {
646 return colorField2.getColor(fVal);
647 }
648 }
649 else {
650 return colorField1.getColor(fVal);
651 }
652 }
653
654 default:
655 case ColorBarStyle::FLOW: {
656 return colorField1.getColor(fVal);
657 }
658 }
659 }
660
661 inline std::size_t ColorGradient::getColorIndex(float fVal) const
662 {
663 switch (profile.tStyle) {
664 case ColorBarStyle::ZERO_BASED: {
665 if ((profile.fMin < 0.0f) && (profile.fMax > 0.0f)) {
666 if (fVal < 0.0f) {
667 return colorField1.getColorIndex(fVal);
668 }
669 else {
670 return std::size_t(colorField1.getCountColors()
671 + colorField2.getColorIndex(fVal));
672 }
673 }
674 else {
675 return colorField1.getColorIndex(fVal);
676 }
677 }
678
679 default:
680 case ColorBarStyle::FLOW: {
681 return colorField1.getColorIndex(fVal);
682 }
683 }
684 }
685
686 inline const ColorModel& ColorGradient::getColorModel() const
687 {
688 if (profile.tStyle == ColorBarStyle::ZERO_BASED) {
689 if (profile.fMax <= 0.0f) {
690 return currentModelPack.bottomModel;
691 }
692 else if (profile.fMin >= 0.0f) {
693 return currentModelPack.topModel;
694 }
695 else {
696 return currentModelPack.totalModel;
697 }
698 }
699
700 return currentModelPack.totalModel;
701 }
702
703 } // namespace App
704
705 #endif // APP_COLORMODEL_H