| 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 |
#include <cstdlib> |
| 24 |
|
| 25 |
#include <Base/Exception.h> |
| 26 |
|
| 27 |
#include "ColorModel.h" |
| 28 |
|
| 29 |
|
| 30 |
using namespace App; |
| 31 |
|
| 32 |
|
| 33 |
ColorModelPack ColorModelPack::createRedGreenBlue() |
| 34 |
{ |
| 35 |
ColorModelPack pack {ColorModelBlueGreenRed(), |
| 36 |
ColorModelGreenYellowRed(), |
| 37 |
ColorModelBlueCyanGreen(), |
| 38 |
"Red-Yellow-Green-Cyan-Blue"}; |
| 39 |
return pack; |
| 40 |
} |
| 41 |
|
| 42 |
ColorModelPack ColorModelPack::createBlueGreenRed() |
| 43 |
{ |
| 44 |
ColorModelPack pack {ColorModelRedGreenBlue(), |
| 45 |
ColorModelGreenCyanBlue(), |
| 46 |
ColorModelRedYellowGreen(), |
| 47 |
"Blue-Cyan-Green-Yellow-Red"}; |
| 48 |
return pack; |
| 49 |
} |
| 50 |
|
| 51 |
ColorModelPack ColorModelPack::createWhiteBlack() |
| 52 |
{ |
| 53 |
ColorModelPack pack {ColorModelBlackWhite(), |
| 54 |
ColorModelGrayWhite(), |
| 55 |
ColorModelBlackGray(), |
| 56 |
"White-Black"}; |
| 57 |
return pack; |
| 58 |
} |
| 59 |
|
| 60 |
ColorModelPack ColorModelPack::createBlackWhite() |
| 61 |
{ |
| 62 |
ColorModelPack pack {ColorModelWhiteBlack(), |
| 63 |
ColorModelGrayBlack(), |
| 64 |
ColorModelWhiteGray(), |
| 65 |
"Black-White"}; |
| 66 |
return pack; |
| 67 |
} |
| 68 |
|
| 69 |
ColorModelPack ColorModelPack::createRedWhiteBlue() |
| 70 |
{ |
| 71 |
ColorModelPack pack {ColorModelBlueWhiteRed(), |
| 72 |
ColorModelWhiteRed(), |
| 73 |
ColorModelBlueWhite(), |
| 74 |
"Red-White-Blue"}; |
| 75 |
return pack; |
| 76 |
} |
| 77 |
|
| 78 |
ColorField::ColorField() |
| 79 |
{ |
| 80 |
set(ColorModelBlueGreenRed(), -1.0f, 1.0f, 13); |
| 81 |
} |
| 82 |
|
| 83 |
ColorField::ColorField(const ColorModel& rclModel, float fMin, float fMax, std::size_t usCt) |
| 84 |
{ |
| 85 |
set(rclModel, fMin, fMax, usCt); |
| 86 |
} |
| 87 |
|
| 88 |
ColorField& ColorField::operator=(const ColorField& rclCF) |
| 89 |
{ |
| 90 |
colorField = rclCF.colorField; |
| 91 |
return *this; |
| 92 |
} |
| 93 |
|
| 94 |
void ColorField::set(const ColorModel& rclModel, float fMin, float fMax, std::size_t usCt) |
| 95 |
{ |
| 96 |
auto bounds = std::minmax(fMin, fMax); |
| 97 |
if (bounds.second <= bounds.first) { |
| 98 |
throw Base::ValueError("Maximum must be higher than minimum"); |
| 99 |
} |
| 100 |
|
| 101 |
this->fMin = bounds.first; |
| 102 |
this->fMax = bounds.second; |
| 103 |
colorModel = rclModel; |
| 104 |
ctColors = std::max<std::size_t>(usCt, colorModel.getCountColors()); |
| 105 |
rebuild(); |
| 106 |
} |
| 107 |
|
| 108 |
void ColorField::setColorModel(const ColorModel& rclModel) |
| 109 |
{ |
| 110 |
colorModel = rclModel; |
| 111 |
rebuild(); |
| 112 |
} |
| 113 |
|
| 114 |
void ColorField::rebuild() |
| 115 |
{ |
| 116 |
colorField.resize(ctColors); |
| 117 |
|
| 118 |
std::size_t usStep = |
| 119 |
std::min<std::size_t>(ctColors / (colorModel.getCountColors() - 1), ctColors - 1); |
| 120 |
std::size_t usInd1 = 0; |
| 121 |
std::size_t usInd2 = usStep; |
| 122 |
for (std::size_t i = 0; i < (colorModel.getCountColors() - 1); i++) { |
| 123 |
interpolate(colorModel.colors[i], usInd1, colorModel.colors[i + 1], usInd2); |
| 124 |
usInd1 = usInd2; |
| 125 |
if ((i + 1) == (colorModel.getCountColors() - 2)) { |
| 126 |
usInd2 = ctColors - 1; |
| 127 |
} |
| 128 |
else { |
| 129 |
usInd2 += usStep; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
fAscent = float(ctColors) / (fMax - fMin); |
| 134 |
fConstant = -fAscent * fMin; |
| 135 |
} |
| 136 |
|
| 137 |
// fills the array from color 1, index 1 to color 2, index 2 |
| 138 |
void ColorField::interpolate(Base::Color clCol1, std::size_t usInd1, Base::Color clCol2, std::size_t usInd2) |
| 139 |
{ |
| 140 |
float fStep = 1.0f, fLen = float(usInd2 - usInd1); |
| 141 |
|
| 142 |
colorField[usInd1] = clCol1; |
| 143 |
colorField[usInd2] = clCol2; |
| 144 |
|
| 145 |
float fR = (float(clCol2.r) - float(clCol1.r)) / fLen; |
| 146 |
float fG = (float(clCol2.g) - float(clCol1.g)) / fLen; |
| 147 |
float fB = (float(clCol2.b) - float(clCol1.b)) / fLen; |
| 148 |
|
| 149 |
for (std::size_t i = (usInd1 + 1); i < usInd2; i++) { |
| 150 |
float ucR = clCol1.r + fR * fStep; |
| 151 |
float ucG = clCol1.g + fG * fStep; |
| 152 |
float ucB = clCol1.b + fB * fStep; |
| 153 |
colorField[i] = Base::Color(ucR, ucG, ucB); |
| 154 |
fStep += 1.0f; |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
ColorGradientProfile::ColorGradientProfile() = default; |
| 160 |
|
| 161 |
bool ColorGradientProfile::isEqual(const ColorGradientProfile& cg) const |
| 162 |
{ |
| 163 |
if (tStyle != cg.tStyle) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
if (fMin != cg.fMin) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
if (fMax != cg.fMax) { |
| 170 |
return false; |
| 171 |
} |
| 172 |
if (!visibility.isEqual(cg.visibility)) { |
| 173 |
return false; |
| 174 |
} |
| 175 |
if (tColorModel != cg.tColorModel) { |
| 176 |
return false; |
| 177 |
} |
| 178 |
return true; |
| 179 |
} |
| 180 |
|
| 181 |
|
| 182 |
ColorGradient::ColorGradient() |
| 183 |
{ |
| 184 |
createStandardPacks(); |
| 185 |
setColorModel(); |
| 186 |
set(-1.0f, 1.0f, 13, ColorBarStyle::ZERO_BASED, Visibility::Default); |
| 187 |
} |
| 188 |
|
| 189 |
ColorGradient::ColorGradient(float fMin, |
| 190 |
float fMax, |
| 191 |
std::size_t usCtColors, |
| 192 |
ColorBarStyle tS, |
| 193 |
VisibilityFlags flags) |
| 194 |
{ |
| 195 |
createStandardPacks(); |
| 196 |
setColorModel(); |
| 197 |
set(fMin, fMax, usCtColors, tS, flags); |
| 198 |
} |
| 199 |
|
| 200 |
void ColorGradient::createStandardPacks() |
| 201 |
{ |
| 202 |
modelPacks.push_back(ColorModelPack::createRedGreenBlue()); |
| 203 |
modelPacks.push_back(ColorModelPack::createBlueGreenRed()); |
| 204 |
modelPacks.push_back(ColorModelPack::createRedWhiteBlue()); |
| 205 |
modelPacks.push_back(ColorModelPack::createWhiteBlack()); |
| 206 |
modelPacks.push_back(ColorModelPack::createBlackWhite()); |
| 207 |
} |
| 208 |
|
| 209 |
std::vector<std::string> ColorGradient::getColorModelNames() const |
| 210 |
{ |
| 211 |
std::vector<std::string> names; |
| 212 |
names.reserve(modelPacks.size()); |
| 213 |
for (const auto& it : modelPacks) { |
| 214 |
names.push_back(it.description); |
| 215 |
} |
| 216 |
return names; |
| 217 |
} |
| 218 |
|
| 219 |
void ColorGradient::setProfile(const ColorGradientProfile& pro) |
| 220 |
{ |
| 221 |
profile = pro; |
| 222 |
setColorModel(); |
| 223 |
rebuild(); |
| 224 |
} |
| 225 |
|
| 226 |
void ColorGradient::set(float fMin, |
| 227 |
float fMax, |
| 228 |
std::size_t usCt, |
| 229 |
ColorBarStyle tS, |
| 230 |
VisibilityFlags flags) |
| 231 |
{ |
| 232 |
auto bounds = std::minmax(fMin, fMax); |
| 233 |
if (bounds.second <= bounds.first) { |
| 234 |
throw Base::ValueError("Maximum must be higher than minimum"); |
| 235 |
} |
| 236 |
|
| 237 |
profile.fMin = bounds.first; |
| 238 |
profile.fMax = bounds.second; |
| 239 |
profile.ctColors = std::max<std::size_t>(usCt, getMinColors()); |
| 240 |
profile.tStyle = tS; |
| 241 |
profile.visibility = flags; |
| 242 |
rebuild(); |
| 243 |
} |
| 244 |
|
| 245 |
void ColorGradient::rebuild() |
| 246 |
{ |
| 247 |
switch (profile.tStyle) { |
| 248 |
case ColorBarStyle::FLOW: { |
| 249 |
colorField1.set(currentModelPack.totalModel, |
| 250 |
profile.fMin, |
| 251 |
profile.fMax, |
| 252 |
profile.ctColors); |
| 253 |
break; |
| 254 |
} |
| 255 |
case ColorBarStyle::ZERO_BASED: { |
| 256 |
if ((profile.fMin < 0.0f) && (profile.fMax > 0.0f)) { |
| 257 |
colorField1.set(currentModelPack.bottomModel, |
| 258 |
profile.fMin, |
| 259 |
0.0f, |
| 260 |
profile.ctColors / 2); |
| 261 |
colorField2.set(currentModelPack.topModel, |
| 262 |
0.0f, |
| 263 |
profile.fMax, |
| 264 |
profile.ctColors / 2); |
| 265 |
} |
| 266 |
else if (profile.fMin >= 0.0f) { |
| 267 |
colorField1.set(currentModelPack.topModel, 0.0f, profile.fMax, profile.ctColors); |
| 268 |
} |
| 269 |
else { |
| 270 |
colorField1.set(currentModelPack.bottomModel, profile.fMin, 0.0f, profile.ctColors); |
| 271 |
} |
| 272 |
break; |
| 273 |
} |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
std::size_t ColorGradient::getMinColors() const |
| 278 |
{ |
| 279 |
switch (profile.tStyle) { |
| 280 |
case ColorBarStyle::FLOW: |
| 281 |
return colorField1.getMinColors(); |
| 282 |
case ColorBarStyle::ZERO_BASED: { |
| 283 |
if ((profile.fMin < 0.0f) && (profile.fMax > 0.0f)) { |
| 284 |
return colorField1.getMinColors() + colorField2.getMinColors(); |
| 285 |
} |
| 286 |
else { |
| 287 |
return colorField1.getMinColors(); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
return 2; |
| 292 |
} |
| 293 |
|
| 294 |
void ColorGradient::setColorModel(std::size_t tModel) |
| 295 |
{ |
| 296 |
profile.tColorModel = tModel; |
| 297 |
setColorModel(); |
| 298 |
rebuild(); |
| 299 |
} |
| 300 |
|
| 301 |
void ColorGradient::setColorModel() |
| 302 |
{ |
| 303 |
if (profile.tColorModel < modelPacks.size()) { |
| 304 |
currentModelPack = modelPacks[profile.tColorModel]; |
| 305 |
} |
| 306 |
|
| 307 |
switch (profile.tStyle) { |
| 308 |
case ColorBarStyle::FLOW: { |
| 309 |
colorField1.setColorModel(currentModelPack.totalModel); |
| 310 |
colorField2.setColorModel(currentModelPack.bottomModel); |
| 311 |
break; |
| 312 |
} |
| 313 |
case ColorBarStyle::ZERO_BASED: { |
| 314 |
colorField1.setColorModel(currentModelPack.topModel); |
| 315 |
colorField2.setColorModel(currentModelPack.bottomModel); |
| 316 |
break; |
| 317 |
} |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
ColorLegend::ColorLegend() |
| 322 |
{ |
| 323 |
// default blue, green, red |
| 324 |
colorFields.emplace_back(0, 0, 1); |
| 325 |
colorFields.emplace_back(0, 1, 0); |
| 326 |
colorFields.emplace_back(1, 0, 0); |
| 327 |
|
| 328 |
names.emplace_back("Min"); |
| 329 |
names.emplace_back("Mid"); |
| 330 |
names.emplace_back("Max"); |
| 331 |
|
| 332 |
values.push_back(-1.0f); |
| 333 |
values.push_back(-0.333f); |
| 334 |
values.push_back(0.333f); |
| 335 |
values.push_back(1.0f); |
| 336 |
} |
| 337 |
|
| 338 |
bool ColorLegend::operator==(const ColorLegend& rclCL) const |
| 339 |
{ |
| 340 |
return (colorFields.size() == rclCL.colorFields.size()) && (names.size() == rclCL.names.size()) |
| 341 |
&& (values.size() == rclCL.values.size()) |
| 342 |
&& std::equal(colorFields.begin(), colorFields.end(), rclCL.colorFields.begin()) |
| 343 |
&& std::equal(names.begin(), names.end(), rclCL.names.begin()) |
| 344 |
&& std::equal(values.begin(), values.end(), rclCL.values.begin()) |
| 345 |
&& outsideGrayed == rclCL.outsideGrayed; |
| 346 |
} |
| 347 |
|
| 348 |
float ColorLegend::getValue(std::size_t ulPos) const |
| 349 |
{ |
| 350 |
if (ulPos < values.size()) { |
| 351 |
return values[ulPos]; |
| 352 |
} |
| 353 |
else { |
| 354 |
return 0.0f; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
bool ColorLegend::setValue(std::size_t ulPos, float fVal) |
| 359 |
{ |
| 360 |
if (ulPos < values.size()) { |
| 361 |
values[ulPos] = fVal; |
| 362 |
return true; |
| 363 |
} |
| 364 |
else { |
| 365 |
return false; |
| 366 |
} |
| 367 |
} |
| 368 |
|
| 369 |
Base::Color ColorLegend::getColor(std::size_t ulPos) const |
| 370 |
{ |
| 371 |
if (ulPos < colorFields.size()) { |
| 372 |
return colorFields[ulPos]; |
| 373 |
} |
| 374 |
else { |
| 375 |
return Base::Color(); |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
// color as: 0x00rrggbb |
| 380 |
uint32_t ColorLegend::getPackedColor(std::size_t ulPos) const |
| 381 |
{ |
| 382 |
Base::Color clRGB = getColor(ulPos); |
| 383 |
return clRGB.getPackedValue(); |
| 384 |
} |
| 385 |
|
| 386 |
std::string ColorLegend::getText(std::size_t ulPos) const |
| 387 |
{ |
| 388 |
if (ulPos < names.size()) { |
| 389 |
return names[ulPos]; |
| 390 |
} |
| 391 |
else { |
| 392 |
return ""; |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
std::size_t ColorLegend::addMin(const std::string& rclName) |
| 397 |
{ |
| 398 |
names.push_front(rclName); |
| 399 |
values.push_front(values.front() - 1.0f); |
| 400 |
|
| 401 |
Base::Color clNewRGB; |
| 402 |
clNewRGB.r = ((float)rand() / (float)RAND_MAX); |
| 403 |
clNewRGB.g = ((float)rand() / (float)RAND_MAX); |
| 404 |
clNewRGB.b = ((float)rand() / (float)RAND_MAX); |
| 405 |
|
| 406 |
colorFields.push_front(clNewRGB); |
| 407 |
|
| 408 |
return colorFields.size() - 1; |
| 409 |
} |
| 410 |
|
| 411 |
std::size_t ColorLegend::addMax(const std::string& rclName) |
| 412 |
{ |
| 413 |
names.push_back(rclName); |
| 414 |
values.push_back(values.back() + 1.0f); |
| 415 |
|
| 416 |
Base::Color clNewRGB; |
| 417 |
clNewRGB.r = ((float)rand() / (float)RAND_MAX); |
| 418 |
clNewRGB.g = ((float)rand() / (float)RAND_MAX); |
| 419 |
clNewRGB.b = ((float)rand() / (float)RAND_MAX); |
| 420 |
|
| 421 |
colorFields.push_back(clNewRGB); |
| 422 |
|
| 423 |
return colorFields.size() - 1; |
| 424 |
} |
| 425 |
|
| 426 |
bool ColorLegend::remove(std::size_t ulPos) |
| 427 |
{ |
| 428 |
if (ulPos < colorFields.size()) { |
| 429 |
colorFields.erase(colorFields.begin() + ulPos); |
| 430 |
names.erase(names.begin() + ulPos); |
| 431 |
values.erase(values.begin() + ulPos); |
| 432 |
|
| 433 |
return true; |
| 434 |
} |
| 435 |
|
| 436 |
return false; |
| 437 |
} |
| 438 |
|
| 439 |
void ColorLegend::removeFirst() |
| 440 |
{ |
| 441 |
if (!colorFields.empty()) { |
| 442 |
colorFields.erase(colorFields.begin()); |
| 443 |
names.erase(names.begin()); |
| 444 |
values.erase(values.begin()); |
| 445 |
} |
| 446 |
} |
| 447 |
|
| 448 |
void ColorLegend::removeLast() |
| 449 |
{ |
| 450 |
if (!colorFields.empty()) { |
| 451 |
colorFields.erase(colorFields.end() - 1); |
| 452 |
names.erase(names.end() - 1); |
| 453 |
values.erase(values.end() - 1); |
| 454 |
} |
| 455 |
} |
| 456 |
|
| 457 |
void ColorLegend::resize(std::size_t ulCt) |
| 458 |
{ |
| 459 |
if ((ulCt < 2) || (ulCt == colorFields.size())) { |
| 460 |
return; |
| 461 |
} |
| 462 |
|
| 463 |
if (ulCt > colorFields.size()) { |
| 464 |
int k = ulCt - colorFields.size(); |
| 465 |
for (int i = 0; i < k; i++) { |
| 466 |
addMin("new"); |
| 467 |
} |
| 468 |
} |
| 469 |
else { |
| 470 |
int k = colorFields.size() - ulCt; |
| 471 |
for (int i = 0; i < k; i++) { |
| 472 |
removeLast(); |
| 473 |
} |
| 474 |
} |
| 475 |
} |
| 476 |
|
| 477 |
bool ColorLegend::setColor(std::size_t ulPos, float ucRed, float ucGreen, float ucBlue) |
| 478 |
{ |
| 479 |
if (ulPos < names.size()) { |
| 480 |
colorFields[ulPos] = Base::Color(ucRed, ucGreen, ucBlue); |
| 481 |
return true; |
| 482 |
} |
| 483 |
|
| 484 |
return false; |
| 485 |
} |
| 486 |
|
| 487 |
// color as 0x00rrggbb |
| 488 |
bool ColorLegend::setColor(std::size_t ulPos, unsigned long ulColor) |
| 489 |
{ |
| 490 |
unsigned char ucRed = (unsigned char)((ulColor & 0x00ff0000) >> 16); |
| 491 |
unsigned char ucGreen = (unsigned char)((ulColor & 0x0000ff00) >> 8); |
| 492 |
unsigned char ucBlue = (unsigned char)(ulColor & 0x000000ff); |
| 493 |
return setColor(ulPos, ucRed, ucGreen, ucBlue); |
| 494 |
} |
| 495 |
|
| 496 |
bool ColorLegend::setText(std::size_t ulPos, const std::string& rclName) |
| 497 |
{ |
| 498 |
if (ulPos < names.size()) { |
| 499 |
names[ulPos] = rclName; |
| 500 |
return true; |
| 501 |
} |
| 502 |
|
| 503 |
return false; |
| 504 |
} |