File size: 15,018 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 | // 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 *
* *
***************************************************************************/
#include <cstdlib>
#include <Base/Exception.h>
#include "ColorModel.h"
using namespace App;
ColorModelPack ColorModelPack::createRedGreenBlue()
{
ColorModelPack pack {ColorModelBlueGreenRed(),
ColorModelGreenYellowRed(),
ColorModelBlueCyanGreen(),
"Red-Yellow-Green-Cyan-Blue"};
return pack;
}
ColorModelPack ColorModelPack::createBlueGreenRed()
{
ColorModelPack pack {ColorModelRedGreenBlue(),
ColorModelGreenCyanBlue(),
ColorModelRedYellowGreen(),
"Blue-Cyan-Green-Yellow-Red"};
return pack;
}
ColorModelPack ColorModelPack::createWhiteBlack()
{
ColorModelPack pack {ColorModelBlackWhite(),
ColorModelGrayWhite(),
ColorModelBlackGray(),
"White-Black"};
return pack;
}
ColorModelPack ColorModelPack::createBlackWhite()
{
ColorModelPack pack {ColorModelWhiteBlack(),
ColorModelGrayBlack(),
ColorModelWhiteGray(),
"Black-White"};
return pack;
}
ColorModelPack ColorModelPack::createRedWhiteBlue()
{
ColorModelPack pack {ColorModelBlueWhiteRed(),
ColorModelWhiteRed(),
ColorModelBlueWhite(),
"Red-White-Blue"};
return pack;
}
ColorField::ColorField()
{
set(ColorModelBlueGreenRed(), -1.0f, 1.0f, 13);
}
ColorField::ColorField(const ColorModel& rclModel, float fMin, float fMax, std::size_t usCt)
{
set(rclModel, fMin, fMax, usCt);
}
ColorField& ColorField::operator=(const ColorField& rclCF)
{
colorField = rclCF.colorField;
return *this;
}
void ColorField::set(const ColorModel& rclModel, float fMin, float fMax, std::size_t usCt)
{
auto bounds = std::minmax(fMin, fMax);
if (bounds.second <= bounds.first) {
throw Base::ValueError("Maximum must be higher than minimum");
}
this->fMin = bounds.first;
this->fMax = bounds.second;
colorModel = rclModel;
ctColors = std::max<std::size_t>(usCt, colorModel.getCountColors());
rebuild();
}
void ColorField::setColorModel(const ColorModel& rclModel)
{
colorModel = rclModel;
rebuild();
}
void ColorField::rebuild()
{
colorField.resize(ctColors);
std::size_t usStep =
std::min<std::size_t>(ctColors / (colorModel.getCountColors() - 1), ctColors - 1);
std::size_t usInd1 = 0;
std::size_t usInd2 = usStep;
for (std::size_t i = 0; i < (colorModel.getCountColors() - 1); i++) {
interpolate(colorModel.colors[i], usInd1, colorModel.colors[i + 1], usInd2);
usInd1 = usInd2;
if ((i + 1) == (colorModel.getCountColors() - 2)) {
usInd2 = ctColors - 1;
}
else {
usInd2 += usStep;
}
}
fAscent = float(ctColors) / (fMax - fMin);
fConstant = -fAscent * fMin;
}
// fills the array from color 1, index 1 to color 2, index 2
void ColorField::interpolate(Base::Color clCol1, std::size_t usInd1, Base::Color clCol2, std::size_t usInd2)
{
float fStep = 1.0f, fLen = float(usInd2 - usInd1);
colorField[usInd1] = clCol1;
colorField[usInd2] = clCol2;
float fR = (float(clCol2.r) - float(clCol1.r)) / fLen;
float fG = (float(clCol2.g) - float(clCol1.g)) / fLen;
float fB = (float(clCol2.b) - float(clCol1.b)) / fLen;
for (std::size_t i = (usInd1 + 1); i < usInd2; i++) {
float ucR = clCol1.r + fR * fStep;
float ucG = clCol1.g + fG * fStep;
float ucB = clCol1.b + fB * fStep;
colorField[i] = Base::Color(ucR, ucG, ucB);
fStep += 1.0f;
}
}
ColorGradientProfile::ColorGradientProfile() = default;
bool ColorGradientProfile::isEqual(const ColorGradientProfile& cg) const
{
if (tStyle != cg.tStyle) {
return false;
}
if (fMin != cg.fMin) {
return false;
}
if (fMax != cg.fMax) {
return false;
}
if (!visibility.isEqual(cg.visibility)) {
return false;
}
if (tColorModel != cg.tColorModel) {
return false;
}
return true;
}
ColorGradient::ColorGradient()
{
createStandardPacks();
setColorModel();
set(-1.0f, 1.0f, 13, ColorBarStyle::ZERO_BASED, Visibility::Default);
}
ColorGradient::ColorGradient(float fMin,
float fMax,
std::size_t usCtColors,
ColorBarStyle tS,
VisibilityFlags flags)
{
createStandardPacks();
setColorModel();
set(fMin, fMax, usCtColors, tS, flags);
}
void ColorGradient::createStandardPacks()
{
modelPacks.push_back(ColorModelPack::createRedGreenBlue());
modelPacks.push_back(ColorModelPack::createBlueGreenRed());
modelPacks.push_back(ColorModelPack::createRedWhiteBlue());
modelPacks.push_back(ColorModelPack::createWhiteBlack());
modelPacks.push_back(ColorModelPack::createBlackWhite());
}
std::vector<std::string> ColorGradient::getColorModelNames() const
{
std::vector<std::string> names;
names.reserve(modelPacks.size());
for (const auto& it : modelPacks) {
names.push_back(it.description);
}
return names;
}
void ColorGradient::setProfile(const ColorGradientProfile& pro)
{
profile = pro;
setColorModel();
rebuild();
}
void ColorGradient::set(float fMin,
float fMax,
std::size_t usCt,
ColorBarStyle tS,
VisibilityFlags flags)
{
auto bounds = std::minmax(fMin, fMax);
if (bounds.second <= bounds.first) {
throw Base::ValueError("Maximum must be higher than minimum");
}
profile.fMin = bounds.first;
profile.fMax = bounds.second;
profile.ctColors = std::max<std::size_t>(usCt, getMinColors());
profile.tStyle = tS;
profile.visibility = flags;
rebuild();
}
void ColorGradient::rebuild()
{
switch (profile.tStyle) {
case ColorBarStyle::FLOW: {
colorField1.set(currentModelPack.totalModel,
profile.fMin,
profile.fMax,
profile.ctColors);
break;
}
case ColorBarStyle::ZERO_BASED: {
if ((profile.fMin < 0.0f) && (profile.fMax > 0.0f)) {
colorField1.set(currentModelPack.bottomModel,
profile.fMin,
0.0f,
profile.ctColors / 2);
colorField2.set(currentModelPack.topModel,
0.0f,
profile.fMax,
profile.ctColors / 2);
}
else if (profile.fMin >= 0.0f) {
colorField1.set(currentModelPack.topModel, 0.0f, profile.fMax, profile.ctColors);
}
else {
colorField1.set(currentModelPack.bottomModel, profile.fMin, 0.0f, profile.ctColors);
}
break;
}
}
}
std::size_t ColorGradient::getMinColors() const
{
switch (profile.tStyle) {
case ColorBarStyle::FLOW:
return colorField1.getMinColors();
case ColorBarStyle::ZERO_BASED: {
if ((profile.fMin < 0.0f) && (profile.fMax > 0.0f)) {
return colorField1.getMinColors() + colorField2.getMinColors();
}
else {
return colorField1.getMinColors();
}
}
}
return 2;
}
void ColorGradient::setColorModel(std::size_t tModel)
{
profile.tColorModel = tModel;
setColorModel();
rebuild();
}
void ColorGradient::setColorModel()
{
if (profile.tColorModel < modelPacks.size()) {
currentModelPack = modelPacks[profile.tColorModel];
}
switch (profile.tStyle) {
case ColorBarStyle::FLOW: {
colorField1.setColorModel(currentModelPack.totalModel);
colorField2.setColorModel(currentModelPack.bottomModel);
break;
}
case ColorBarStyle::ZERO_BASED: {
colorField1.setColorModel(currentModelPack.topModel);
colorField2.setColorModel(currentModelPack.bottomModel);
break;
}
}
}
ColorLegend::ColorLegend()
{
// default blue, green, red
colorFields.emplace_back(0, 0, 1);
colorFields.emplace_back(0, 1, 0);
colorFields.emplace_back(1, 0, 0);
names.emplace_back("Min");
names.emplace_back("Mid");
names.emplace_back("Max");
values.push_back(-1.0f);
values.push_back(-0.333f);
values.push_back(0.333f);
values.push_back(1.0f);
}
bool ColorLegend::operator==(const ColorLegend& rclCL) const
{
return (colorFields.size() == rclCL.colorFields.size()) && (names.size() == rclCL.names.size())
&& (values.size() == rclCL.values.size())
&& std::equal(colorFields.begin(), colorFields.end(), rclCL.colorFields.begin())
&& std::equal(names.begin(), names.end(), rclCL.names.begin())
&& std::equal(values.begin(), values.end(), rclCL.values.begin())
&& outsideGrayed == rclCL.outsideGrayed;
}
float ColorLegend::getValue(std::size_t ulPos) const
{
if (ulPos < values.size()) {
return values[ulPos];
}
else {
return 0.0f;
}
}
bool ColorLegend::setValue(std::size_t ulPos, float fVal)
{
if (ulPos < values.size()) {
values[ulPos] = fVal;
return true;
}
else {
return false;
}
}
Base::Color ColorLegend::getColor(std::size_t ulPos) const
{
if (ulPos < colorFields.size()) {
return colorFields[ulPos];
}
else {
return Base::Color();
}
}
// color as: 0x00rrggbb
uint32_t ColorLegend::getPackedColor(std::size_t ulPos) const
{
Base::Color clRGB = getColor(ulPos);
return clRGB.getPackedValue();
}
std::string ColorLegend::getText(std::size_t ulPos) const
{
if (ulPos < names.size()) {
return names[ulPos];
}
else {
return "";
}
}
std::size_t ColorLegend::addMin(const std::string& rclName)
{
names.push_front(rclName);
values.push_front(values.front() - 1.0f);
Base::Color clNewRGB;
clNewRGB.r = ((float)rand() / (float)RAND_MAX);
clNewRGB.g = ((float)rand() / (float)RAND_MAX);
clNewRGB.b = ((float)rand() / (float)RAND_MAX);
colorFields.push_front(clNewRGB);
return colorFields.size() - 1;
}
std::size_t ColorLegend::addMax(const std::string& rclName)
{
names.push_back(rclName);
values.push_back(values.back() + 1.0f);
Base::Color clNewRGB;
clNewRGB.r = ((float)rand() / (float)RAND_MAX);
clNewRGB.g = ((float)rand() / (float)RAND_MAX);
clNewRGB.b = ((float)rand() / (float)RAND_MAX);
colorFields.push_back(clNewRGB);
return colorFields.size() - 1;
}
bool ColorLegend::remove(std::size_t ulPos)
{
if (ulPos < colorFields.size()) {
colorFields.erase(colorFields.begin() + ulPos);
names.erase(names.begin() + ulPos);
values.erase(values.begin() + ulPos);
return true;
}
return false;
}
void ColorLegend::removeFirst()
{
if (!colorFields.empty()) {
colorFields.erase(colorFields.begin());
names.erase(names.begin());
values.erase(values.begin());
}
}
void ColorLegend::removeLast()
{
if (!colorFields.empty()) {
colorFields.erase(colorFields.end() - 1);
names.erase(names.end() - 1);
values.erase(values.end() - 1);
}
}
void ColorLegend::resize(std::size_t ulCt)
{
if ((ulCt < 2) || (ulCt == colorFields.size())) {
return;
}
if (ulCt > colorFields.size()) {
int k = ulCt - colorFields.size();
for (int i = 0; i < k; i++) {
addMin("new");
}
}
else {
int k = colorFields.size() - ulCt;
for (int i = 0; i < k; i++) {
removeLast();
}
}
}
bool ColorLegend::setColor(std::size_t ulPos, float ucRed, float ucGreen, float ucBlue)
{
if (ulPos < names.size()) {
colorFields[ulPos] = Base::Color(ucRed, ucGreen, ucBlue);
return true;
}
return false;
}
// color as 0x00rrggbb
bool ColorLegend::setColor(std::size_t ulPos, unsigned long ulColor)
{
unsigned char ucRed = (unsigned char)((ulColor & 0x00ff0000) >> 16);
unsigned char ucGreen = (unsigned char)((ulColor & 0x0000ff00) >> 8);
unsigned char ucBlue = (unsigned char)(ulColor & 0x000000ff);
return setColor(ulPos, ucRed, ucGreen, ucBlue);
}
bool ColorLegend::setText(std::size_t ulPos, const std::string& rclName)
{
if (ulPos < names.size()) {
names[ulPos] = rclName;
return true;
}
return false;
}
|