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