19 #include <boost/foreach.hpp>
20 #include <boost/tuple/tuple.hpp>
21 #include <boost/math/special_functions/fpclassify.hpp>
52 double valueMin = 40.0f;
53 double valueMax = 75.0f;
57 lookupTable.resize(9);
60 lookupTable[0] =
OColor(76.0f / 255.0f, 230.0f / 255.0f, 0.0f);
61 lookupTable[1] =
OColor(85.0f / 255.0f, 1.0f, 0.0f);
62 lookupTable[2] =
OColor(209.0f / 255.0f, 1.0f, 115.0f / 255.0f);
63 lookupTable[3] =
OColor(1.0f, 1.0f, 81.0f / 255.0f);
64 lookupTable[4] =
OColor(1.0f, 1.0f, 0.0f);
65 lookupTable[5] =
OColor(1.0f, 170.0f / 255.0f, 0.0f);
66 lookupTable[6] =
OColor(1.0f, 85.0f / 255.0f, 0.0f);
67 lookupTable[7] =
OColor(227.0f / 255.0f, 0.0f, 0.0f);
68 lookupTable[8] =
OColor(168.0f / 255.0f, 0.0f, 0.0f);
75 QString pref =
"PaletteDefault";
82 for (
size_t i = 0; i < lookupTable.size(); ++i)
87 pref +
"Color" + QString(
uintToStr(i).c_str()), color.
r, color.
g,
89 lookupTable[i] = color;
106 QString pref =
"PaletteDefault";
115 BOOST_FOREACH (boost::tie(value, color),
getColorMap())
119 color.
r, color.
g, color.
b);
127 const std::vector<OColor>& lookupTable)
129 const size_t nbColors = lookupTable.size();
130 const size_t nbValues = lookupTable.size() - 1;
131 assert(nbColors > 2 &&
"A color map needs at least three colors");
135 for (
size_t i = 0; i < lookupTable.size(); ++i)
177 if (TYElement::operator!=(other))
208 std::pair<TYPalette::color_map_iter, bool> status =
_colorMap.insert(std::make_pair(value, color));
211 status.first->second = color;
213 return status.second;
220 "This should never happen as the last value is always supposed to be +inf");
223 if (boost::math::isfinite(actual_value))
233 if (new_value >= after->first)
241 if (new_value <= before->first)
247 OColor color = where->second;
249 _colorMap.insert(after, std::make_pair(new_value, color));
261 if (boost::math::isfinite(actual_value) &&
280 DOM_Element colorMapElem = domDoc.createElement(
"colorMap");
286 BOOST_FOREACH (boost::tie(value, color),
getColorMap())
288 DOM_Element colorMapEntry = domDoc.createElement(
"colorMapEntry");
290 colorMapEntry.setAttribute(
"value", value);
292 colorMapEntry.setAttribute(
"color", colorcode);
294 colorMapElem.appendChild(colorMapEntry);
298 domNewElem.appendChild(colorMapElem);
312 DOM_Element lookupTableElem = domDoc.createElement(
"lookupTable");
313 domNewElem.appendChild(lookupTableElem);
317 lookupTableElem.setAttribute(
"nbColors", nbColors);
320 std::ostringstream oss;
323 BOOST_FOREACH (boost::tie(value, color),
getColorMap())
331 QDomText data = domDoc.createTextNode(tab);
332 lookupTableElem.appendChild(data);
340 QDomNodeList children = domElement.elementsByTagName(
"lookupTable");
346 if (children.length() == 0)
352 "No lookupTable element while reading the legacy 'Palette' named %s (id %s).",
353 this->
_name.toLocal8Bit().data(), this->getID().toString().toLocal8Bit().data());
356 if (children.length() > 1)
359 "While reading the 'Palette' named %s (id %s) : only one lookupTable element is expected.",
360 this->
_name.toLocal8Bit().data(), this->getID().toString().toLocal8Bit().data());
363 assert(children.length() == 1);
364 QDomElement lookupTableElement = children.at(0).toElement();
365 QString elemName = lookupTableElement.nodeName();
366 assert(elemName ==
"lookupTable");
374 lookupTable.resize(nbColors);
379 std::istringstream iss(colors.toLatin1().data());
382 unsigned int rgba = 0;
383 for (
unsigned int i = 0; i < nbColors; ++i)
386 lookupTable[i].setAsRGBA(rgba);
395 QDomNodeList children = domElement.elementsByTagName(
"colorMap");
397 if (children.length() == 0)
403 "Invalid legacy format (pre colorMap) while reading the 'Palette' named %s (id %s).",
404 this->
_name.toLocal8Bit().data(), this->getID().toString().toLocal8Bit().data());
407 if (children.length() > 1)
410 "one colorMap element is expected.",
411 this->
_name.toLocal8Bit().data(),
412 this->getID().toString().toLocal8Bit().data());
415 assert(children.length() == 1);
416 QDomElement colorMapElement = children.at(0).toElement();
417 QString elemName = colorMapElement.nodeName();
418 assert(elemName ==
"colorMap");
419 children = domElement.elementsByTagName(
"colorMapEntry");
422 for (
unsigned i = 0; i < children.length(); ++i)
428 const QDomElement entryElement = children.item(i).toElement();
435 "(id %s) : expected value field not '%s'",
436 i, this->
_name.toLocal8Bit().data(),
437 this->getID().toString().toLocal8Bit().data(),
449 QTextStream str(&msg);
450 entryElement.save(str, 0);
452 "(id %s) : expected RGB field not '%s'",
453 i, this->
_name.toLocal8Bit().data(),
454 this->getID().toString().toLocal8Bit().data(),
455 msg.toLocal8Bit().data());
462 color.
setAsRGBA(
static_cast<unsigned>(rgb));
487 assert(amplitude > std::numeric_limits<values_type>::epsilon());
494 color_map_type::const_iterator it =
_colorMap.lower_bound(value);
495 assert(it !=
_colorMap.end() &&
"This should never happen.");
502 color_map_type::const_iterator it =
_colorMap.begin();
514 color_map_type::const_iterator it =
_colorMap.begin();
530 bool status = xmlManager.
load(qFileName, elements);
532 if (elements.size() == 1)
547 bool status = xmlManager.
save(qFileName) == 0;
553 float hueRange[2], saturationRange[2], valueRange[2];
QDomDocument DOM_Document
SmartPtr< TYPalette > LPTYPalette
Smart pointer sur TYPalette.
std::vector< LPTYElement > LPTYElementArray
#define TYDIRPREFERENCEMANAGER
const char * xml2cstring(const QDomNode &node)
Representation d'une palette graphique (fichier header)
TY_EXT_GRAPHIC_INST(TYPalette)
TY_EXTENSION_INST(TYPalette)
unsigned int getAsRGBA() const
static const OColor BLACK
static const OColor WHITE
void getAsHSB(float &hue, float &saturation, float &brightness) const
void setAsRGBA(const unsigned int val)
virtual void error(const char *message,...)
static OMessageManager * get()
virtual const char * getClassName() const
static OPrototype * safeDownCast(OPrototype *pObject)
static void getLinearColorTable(const unsigned int &nbColors, const float *hueRange, const float *saturationRange, const float *valueRange, OLookupTable &outColors)
Generation de la table de couleurs basee sur une rampe lineaire ( y = x+0.5/255 )
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
virtual DOM_Element toXML(DOM_Element &domElement)
QString _name
Nom courant de l'element.
TYElement & operator=(const TYElement &other)
virtual int fromXML(DOM_Element domElement)
QString generateName(const char *classname)
Retourne le nom de la classe associe a un nombre.
static TYNameManager * get()
Retourne l'instance singleton.
Classe de definition d'une palette.
virtual std::string toString() const
size_t getNbValues() const
Getter for the number of values.
bool removeValue(values_type value)
Removes an existing bound.
void makeLinearPalette(unsigned int nb_colors, float value_min, float value_max)
Make a linear palette from a min/max couple.
const OColor & getColorFromValue(values_type value) const
Set de la lookup table.
bool resetFromPreferences()
Reset the colormap from the preferences XXX.
const OColor & getInfColor() const
Get de la couleur pour la borne inf.
const color_map_type & getColorMap() const
Get de la lookup table.
size_t getNbColors() const
Getter for the number of colors.
void resetToDefault()
Reset the colormap to a hard-coded default.
const OColor & getSupColor() const
Get/Set de la couleur pour la borne sup.
const OColor & getColorFromIndex(unsigned i) const
Get de la couleur par index.
DOM_Element save_legacy_XML(DOM_Element &domElement)
implement loading legacy (pre v4.6) XML format for TYPalettes
TYPalette()
A TYPalette represents a mapping from values to colors.
static LPTYPalette loadPaletteFromFile(const QString &qFileName)
Create a new TYPalette from an XML file Holding only one Palette.
bool operator==(const TYPalette &other) const
bool savePaletteToFile(const QString &qFileName)
Save the Palette into a file with the name given.
values_type getValueMax() const
Get de la valeur max.
TYPalette & operator=(const TYPalette &other)
TYPalette::values_type getValueFromIndex(unsigned i) const
Get de la valeur par index.
virtual DOM_Element toXML(DOM_Element &domElement)
bool moveValue(values_type old_value, values_type new_value)
Moves an existing bound,.
float values_type
The real number type used to store values (typically double or float)
void saveToPreferences()
Saves the colormap to the preferences XXX.
virtual ~TYPalette()
Destructeur. Destructeur de la classe TYPalette.
void resetcolorMapFromColors(values_type valueMin, values_type valueMax, const std::vector< OColor > &lookupTable)
Reset the colormap as a linear scale with the given colors between minValue and maxValue.
values_type normalize(values_type value) const
Normalize a value, ie convert a value between min and max to a percentage.
static const values_type infinity
virtual bool deepCopy(const TYElement *pOther, bool copyId=true, bool pUseCopyTag=false)
virtual int fromXML(DOM_Element domElement)
values_type getValueMin() const
Get de la valeur min.
bool insertColor(values_type value, const OColor &color)
Insert a new color or update an existing one.
color_map_type::iterator color_map_iter
int load_legacy_XML(const DOM_Element &domElement)
implement loading legacy (pre v4.6) XML format for TYPalettes
int save(QString fileName)
int load(const QString &fileName, LPTYElementArray &eltCollection)
void createDoc(QString docName, QString version)
int addElement(TYElement *pElt)
std::vector< OColor > OLookupTable
Collection de OColor.
std::string uintToStr(unsigned int val)