gepetto-viewer 6.0.0
An user-friendly Graphical Interface
Loading...
Searching...
No Matches
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
21
22#include <QColor>
23#include <QDebug>
24
25namespace gepetto {
26namespace gui {
27class ColorMap {
28 public:
29 static QColor interpolate(std::size_t nbColors, std::size_t index) {
30 if (index > nbColors)
31 qDebug() << "Nb colors:" << nbColors << "and index" << index;
32 return QColor::fromHslF((qreal)index / (qreal)nbColors, 1, 0.5);
33 }
34
35 ColorMap(std::size_t nbColors) : nbColors_(nbColors), currentIndex_(0) {
36 log2up_ = 0;
37 mask_ = 0;
38 std::size_t val = (nbColors > 0) ? nbColors : 1;
39 for (log2up_ = 0; val; ++log2up_, val >>= 1) mask_ = 2 * mask_ + 1;
40 }
41
42 QColor getColor(std::size_t index) const {
43 return ColorMap::interpolate(mask_, remap(index));
44 }
45
46 void getColor(std::size_t index, osgVector4& color) const {
47 QColor c = getColor(index);
48 color[0] = (float)c.redF();
49 color[1] = (float)c.greenF();
50 color[2] = (float)c.blueF();
51 color[3] = (float)c.alphaF();
52 }
53
54 QColor nextColor() {
55 QColor color = getColor(currentIndex_);
56 currentIndex(currentIndex_ + 1);
57 return color;
58 }
59
60 void currentIndex(std::size_t index) { currentIndex_ = index % nbColors_; }
61
64 std::size_t remap(const std::size_t& index) const {
65 std::size_t ret = 0;
66 std::size_t input = index;
67 for (std::size_t i = 0; i < log2up_; ++i) {
68 ret <<= 1;
69 if (input & 1) ++ret;
70 input >>= 1;
71 }
72 return ret;
73 }
74
75 private:
76 std::size_t nbColors_;
77 std::size_t mask_;
78 std::size_t log2up_;
79 std::size_t currentIndex_;
80};
81} // namespace gui
82} // namespace gepetto
83
84#endif // GEPETTO_GUI_COLORMAP_HH
Definition color-map.hh:27
void currentIndex(std::size_t index)
Definition color-map.hh:60
QColor nextColor()
Definition color-map.hh:54
std::size_t remap(const std::size_t &index) const
Definition color-map.hh:64
static QColor interpolate(std::size_t nbColors, std::size_t index)
Definition color-map.hh:29
ColorMap(std::size_t nbColors)
Definition color-map.hh:35
void getColor(std::size_t index, osgVector4 &color) const
Definition color-map.hh:46
QColor getColor(std::size_t index) const
Definition color-map.hh:42
::osg::Vec4f osgVector4
Definition config-osg.h:100
Definition action-search-bar.hh:27