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
|
// --------------------------------------------------------------------------
// OpenMS -- Open-Source Mass Spectrometry
// --------------------------------------------------------------------------
// Copyright The OpenMS Team -- Eberhard Karls University Tuebingen,
// ETH Zurich, and Freie Universitaet Berlin 2002-2013.
//
// This software is released under a three-clause BSD license:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of any author or any participating institution
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
// For a full list of authors, refer to the file AUTHORS.
// --------------------------------------------------------------------------
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL ANY OF THE AUTHORS OR THE CONTRIBUTING
// INSTITUTIONS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
// OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
// OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// --------------------------------------------------------------------------
// $Maintainer: Timo Sachsenberg $
// $Authors: Marc Sturm $
// --------------------------------------------------------------------------
// OpenMS includes
#include <OpenMS/VISUAL/MultiGradient.h>
#include <cstdlib>
using namespace std;
namespace OpenMS
{
using namespace Exception;
MultiGradient::MultiGradient() :
pos_col_(),
interpolation_mode_(IM_LINEAR)
{
pos_col_[0] = Qt::white;
pos_col_[100] = Qt::black;
}
MultiGradient::MultiGradient(const MultiGradient & multigradient) :
pos_col_(multigradient.pos_col_),
interpolation_mode_(multigradient.interpolation_mode_),
pre_(multigradient.pre_),
pre_min_(multigradient.pre_min_),
pre_size_(multigradient.pre_size_),
pre_steps_(multigradient.pre_steps_)
{
}
MultiGradient & MultiGradient::operator=(const MultiGradient & rhs)
{
if (this == &rhs)
{
return *this;
}
pos_col_ = rhs.pos_col_,
interpolation_mode_ = rhs.interpolation_mode_;
pre_ = rhs.pre_;
pre_min_ = rhs.pre_min_;
pre_size_ = rhs.pre_size_;
pre_steps_ = rhs.pre_steps_;
return *this;
}
MultiGradient::~MultiGradient()
{
}
Size MultiGradient::size() const
{
return pos_col_.size();
}
void MultiGradient::insert(DoubleReal position, QColor color)
{
if (position >= 0 && position <= 100)
{
pos_col_[position] = color;
}
else
{
throw InvalidRange(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
}
UInt MultiGradient::position(UInt index)
{
if (index > size() - 1)
{
throw IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
map<DoubleReal, QColor>::iterator it = pos_col_.begin();
for (Size i = 0; i < index; ++i)
{
++it;
}
return it->first;
}
QColor MultiGradient::color(UInt index)
{
if (index > size() - 1)
{
throw IndexOverflow(__FILE__, __LINE__, __PRETTY_FUNCTION__);
}
map<DoubleReal, QColor>::iterator it = pos_col_.begin();
for (Size i = 0; i < index; ++i)
{
++it;
}
return it->second;
}
QColor MultiGradient::interpolatedColorAt(DoubleReal position) const
{
if (position <= 0.0)
{
return pos_col_.begin()->second;
}
if (position >= 100.0)
{
return (--(pos_col_.end()))->second;
}
//linear
if (interpolation_mode_ == IM_LINEAR)
{
map<DoubleReal, QColor>::const_iterator it1 = pos_col_.lower_bound(position);
if (std::abs(it1->first - position) < numeric_limits<DoubleReal>::epsilon()) // compare double
{
return it1->second;
}
else
{
map<DoubleReal, QColor>::const_iterator it0 = it1;
--it0;
DoubleReal factor = (position - it0->first) / (it1->first - it0->first);
return QColor(Int(factor * it1->second.red() + (1 - factor) * it0->second.red() + 0.001)
, Int(factor * it1->second.green() + (1 - factor) * it0->second.green() + 0.001)
, Int(factor * it1->second.blue() + (1 - factor) * it0->second.blue() + 0.001));
}
}
//stairs
else
{
map<DoubleReal, QColor>::const_iterator it = pos_col_.upper_bound(position);
--it;
return it->second;
}
}
QColor MultiGradient::interpolatedColorAt(DoubleReal position, DoubleReal min, DoubleReal max) const
{
return interpolatedColorAt((position - min) / (max - min) * 100.0);
}
void MultiGradient::setInterpolationMode(MultiGradient::InterpolationMode mode)
{
interpolation_mode_ = mode;
}
MultiGradient::InterpolationMode MultiGradient::getInterpolationMode() const
{
return interpolation_mode_;
}
string MultiGradient::toString() const
{
stringstream out;
//Interpolation Mode
if (getInterpolationMode() == IM_LINEAR)
{
out << "Linear|";
}
else if (getInterpolationMode() == IM_STAIRS)
{
out << "Stairs|";
}
for (map<DoubleReal, QColor>::const_iterator it = pos_col_.begin(); it != pos_col_.end(); ++it)
{
if (it != pos_col_.begin())
{
out << ";";
}
out << it->first << "," << it->second.name().toStdString();
}
return out.str();
}
void MultiGradient::fromString(const string & gradient)
{
pos_col_.clear();
if (gradient == "")
{
pos_col_[0] = Qt::white;
pos_col_[100] = Qt::black;
return;
}
string g(gradient);
string::iterator tmp(g.begin());
DoubleReal tmp_pos = 0;
for (string::iterator it = g.begin(); it != g.end(); ++it)
{
if (*it == '|')
{
//interploation mode
if (string(tmp, it) == "Linear")
{
setInterpolationMode(IM_LINEAR);
}
else if (string(tmp, it) == "Stairs")
{
setInterpolationMode(IM_STAIRS);
}
}
else if (*it == ';')
{
pos_col_[tmp_pos] = QColor(string(tmp, it).c_str());
tmp = it + 1;
}
else if (*it == ',')
{
tmp_pos = QString(string(tmp, it).c_str()).toDouble();
tmp = it + 1;
}
}
// last entry
pos_col_[tmp_pos] = QColor(string(tmp, g.end()).c_str());
}
void MultiGradient::activatePrecalculationMode(DoubleReal min, DoubleReal max, UInt steps)
{
//add security margin to range to avoid numerical problems
pre_min_ = std::min(min, max) - 0.000005;
pre_size_ = fabs(max - min) + 0.00001;
pre_steps_ = steps - 1;
pre_.clear();
pre_.reserve(steps);
for (Size step = 0; step < steps; ++step)
{
pre_.push_back(interpolatedColorAt(step, 0, pre_steps_));
//cout << pre_.back().red() << " " << pre_.back().green() << " " << pre_.back().blue() << endl;
}
}
void MultiGradient::deactivatePrecalculationMode()
{
pre_.clear();
}
bool MultiGradient::exists(DoubleReal position)
{
return pos_col_.find(position) != pos_col_.end();
}
bool MultiGradient::remove(DoubleReal position)
{
if (position < 0 + std::numeric_limits<DoubleReal>::epsilon() || position > 100 - std::numeric_limits<DoubleReal>::epsilon())
{
return false;
}
map<DoubleReal, QColor>::iterator it = pos_col_.find(position);
if (it != pos_col_.end())
{
pos_col_.erase(it);
return true;
}
return false;
}
// static
MultiGradient MultiGradient::getDefaultGradientLinearIntensityMode()
{
MultiGradient mg;
mg.fromString("Linear|0,#eeeeee;1,#ffea00;6,#ff0000;14,#aa00ff;23,#5500ff;100,#000000");
return mg;
}
// static
MultiGradient MultiGradient::getDefaultGradientLogarithmicIntensityMode()
{
MultiGradient mg;
mg.fromString("Linear|0,#EEEEEE;100,#000000");
return mg;
}
} //namespace OpenMS
|