| 1 |
/* +------------------------------------------------------------------------+ |
| 2 |
| Mobile Robot Programming Toolkit (MRPT) | |
| 3 |
| https://www.mrpt.org/ | |
| 4 |
| | |
| 5 |
| Copyright (c) 2005-2026, Individual contributors, see AUTHORS file | |
| 6 |
| See: https://www.mrpt.org/Authors - All rights reserved. | |
| 7 |
| Released under BSD License. See: https://www.mrpt.org/License | |
| 8 |
+------------------------------------------------------------------------+ */ |
| 9 |
|
| 10 |
#include "img-precomp.h" // Precompiled headers |
| 11 |
// |
| 12 |
#include <mrpt/img/TColorManager.h> |
| 13 |
|
| 14 |
using namespace mrpt::img; |
| 15 |
using namespace std; |
| 16 |
|
| 17 |
TColorManager::TColorManager(bool use_standard_colors_first_in /* = true */) : |
| 18 |
use_standard_colors_first(use_standard_colors_first_in) |
| 19 |
{ |
| 20 |
this->reset(); |
| 21 |
} |
| 22 |
|
| 23 |
TColorManager::~TColorManager() = default; |
| 24 |
TColor TColorManager::getNextTColor() |
| 25 |
{ |
| 26 |
if (have_exceeded_colors) |
| 27 |
{ |
| 28 |
// pick and return a random color triad |
| 29 |
return TColor( |
| 30 |
rand() % (color_thresh + 1), rand() % (color_thresh + 1), rand() % (color_thresh + 1)); |
| 31 |
} |
| 32 |
|
| 33 |
// start updating by the step if we don't use (or have already used) the |
| 34 |
// standard colors |
| 35 |
if (!use_standard_colors_first || have_used_standard_colors) |
| 36 |
{ |
| 37 |
this->advanceRGBCounters(); |
| 38 |
} |
| 39 |
else |
| 40 |
{ |
| 41 |
bool used_red = used_colors.find(TColor::red()) != used_colors.end(); |
| 42 |
bool used_green = used_colors.find(TColor::green()) != used_colors.end(); |
| 43 |
bool used_blue = used_colors.find(TColor::blue()) != used_colors.end(); |
| 44 |
|
| 45 |
// fixed order of usage |
| 46 |
// red -> green -> blue |
| 47 |
if (!used_red) |
| 48 |
{ |
| 49 |
curr_color = TColor::red(); |
| 50 |
} |
| 51 |
else if (!used_green) |
| 52 |
{ |
| 53 |
curr_color = TColor::green(); |
| 54 |
} |
| 55 |
else if (!used_blue) |
| 56 |
{ |
| 57 |
curr_color = TColor::blue(); |
| 58 |
have_used_standard_colors = true; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
this->markColorAsUsed(curr_color); |
| 63 |
return curr_color; |
| 64 |
} // end of getNextTColor |
| 65 |
|
| 66 |
TColorf TColorManager::getNextTColorf() |
| 67 |
{ |
| 68 |
return TColorf(this->getNextTColor()); |
| 69 |
} // end of getNextTColor |
| 70 |
|
| 71 |
void TColorManager::advanceRGBCounters() |
| 72 |
{ |
| 73 |
// method is used only when we either don't use or have already used the |
| 74 |
// standard colors. |
| 75 |
ASSERT_(!use_standard_colors_first || have_used_standard_colors); |
| 76 |
|
| 77 |
THROW_EXCEPTION("Not yet implemented."); |
| 78 |
// if standard colors have already been used then at first color is |
| 79 |
// TColor::blue |
| 80 |
if (curr_color == TColor::blue()) |
| 81 |
{ |
| 82 |
curr_color = TColor(); |
| 83 |
|
| 84 |
color_step_triad.B = color_step; |
| 85 |
} |
| 86 |
|
| 87 |
// first advance blue until we reach the limit 255 |
| 88 |
// then advance green until we reach the limit 255 |
| 89 |
// finally advance red until we reach the limit 255 |
| 90 |
|
| 91 |
// TODO - how do i do this? |
| 92 |
curr_color += color_step_triad; |
| 93 |
|
| 94 |
// if the standard colors are to be used first make sure that the color we |
| 95 |
// end up on is not a standard one. |
| 96 |
if (use_standard_colors_first && (curr_color == TColor::red() || curr_color == TColor::green() || |
| 97 |
curr_color == TColor::blue())) |
| 98 |
{ |
| 99 |
this->advanceRGBCounters(); |
| 100 |
} |
| 101 |
|
| 102 |
} // end of advanceRGBCounters |
| 103 |
|
| 104 |
void TColorManager::markColorAsUsed(TColor color) |
| 105 |
{ |
| 106 |
using namespace std; |
| 107 |
pair<set<TColor>::const_iterator, bool> ret = used_colors.insert(color); |
| 108 |
|
| 109 |
// Should always return True as element shouldn't exist prior to this call |
| 110 |
ASSERT_(ret.second); |
| 111 |
} |
| 112 |
|
| 113 |
void TColorManager::reset() |
| 114 |
{ |
| 115 |
used_colors.clear(); |
| 116 |
|
| 117 |
curr_color = TColor(); |
| 118 |
color_step_triad = TColor(0, 0, 50); |
| 119 |
|
| 120 |
have_exceeded_colors = false; |
| 121 |
use_standard_colors_first = true; |
| 122 |
have_used_standard_colors = false; |
| 123 |
} |
| 124 |
|
| 125 |
bool TColorManager::checkStdColorsUsed() |
| 126 |
{ |
| 127 |
bool ret = used_colors.find(TColor::red()) != used_colors.end() && |
| 128 |
used_colors.find(TColor::green()) != used_colors.end() && |
| 129 |
used_colors.find(TColor::blue()) != used_colors.end(); |
| 130 |
|
| 131 |
return ret; |
| 132 |
} |