color-map.hh
Go to the documentation of this file.
1 // Copyright (c) 2015-2018, LAAS-CNRS
2 // Authors: Joseph Mirabel (joseph.mirabel@laas.fr)
3 //
4 // This file is part of gepetto-viewer.
5 // gepetto-viewer is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // gepetto-viewer is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // gepetto-viewer. If not, see <http://www.gnu.org/licenses/>.
16 
17 #ifndef GEPETTO_GUI_COLORMAP_HH
18 #define GEPETTO_GUI_COLORMAP_HH
19 
20 #include <QColor>
21 #include <QDebug>
22 
24 
25 namespace gepetto {
26  namespace gui {
27  class ColorMap {
28  public:
29  static QColor interpolate (std::size_t nbColors, std::size_t index)
30  {
31  if (index > nbColors)
32  qDebug() << "Nb colors:" << nbColors << "and index" << index;
33  return QColor::fromHslF((qreal)index / (qreal)nbColors, 1, 0.5);
34  }
35 
36  ColorMap (std::size_t nbColors) :
37  nbColors_ (nbColors),
38  currentIndex_ (0)
39  {
40  log2up_ = 0;
41  mask_ = 0;
42  std::size_t val = (nbColors > 0)?nbColors:1;
43  for (log2up_ = 0; val; ++log2up_, val >>= 1) mask_ = 2*mask_ + 1;
44  }
45 
46  QColor getColor (std::size_t index) const {
47  return ColorMap::interpolate(mask_, remap (index));
48  }
49 
50  void getColor (std::size_t index, osgVector4& color) const {
51  QColor c = getColor(index);
52  color[0] = (float)c.redF();
53  color[1] = (float)c.greenF();
54  color[2] = (float)c.blueF();
55  color[3] = (float)c.alphaF();
56  }
57 
58  QColor nextColor () {
59  QColor color = getColor (currentIndex_);
60  currentIndex(currentIndex_ + 1);
61  return color;
62  }
63 
64  void currentIndex (std::size_t index) {
65  currentIndex_ = index % nbColors_;
66  }
67 
70  std::size_t remap (const std::size_t& index) const {
71  std::size_t ret = 0;
72  std::size_t input = index;
73  for (std::size_t i = 0; i < log2up_; ++i) {
74  ret <<= 1;
75  if (input & 1) ++ret;
76  input >>= 1;
77  }
78  return ret;
79  }
80 
81  private:
82  std::size_t nbColors_;
83  std::size_t mask_;
84  std::size_t log2up_;
85  std::size_t currentIndex_;
86  };
87  } // namespace gui
88 } // namespace gepetto
89 
90 #endif // GEPETTO_GUI_COLORMAP_HH
void getColor(std::size_t index, osgVector4 &color) const
Definition: color-map.hh:50
QColor nextColor()
Definition: color-map.hh:58
static QColor interpolate(std::size_t nbColors, std::size_t index)
Definition: color-map.hh:29
::osg::Vec4f osgVector4
Definition: config-osg.h:110
void currentIndex(std::size_t index)
Definition: color-map.hh:64
ColorMap(std::size_t nbColors)
Definition: color-map.hh:36
Definition: action-search-bar.hh:27
QColor getColor(std::size_t index) const
Definition: color-map.hh:46
std::size_t remap(const std::size_t &index) const
Definition: color-map.hh:70
Definition: color-map.hh:27