##// END OF EJS Templates
Sets differents colors for components of a vector
Alexandre Leroux -
r584:17624d030c39
parent child
Show More
@@ -0,0 +1,19
1 #ifndef SCIQLOP_COLORUTILS_H
2 #define SCIQLOP_COLORUTILS_H
3
4 #include <vector>
5
6 class QColor;
7
8 /**
9 * Utility class with methods for colors
10 */
11 struct ColorUtils {
12 /// Generates a color scale from min / max values and a number of colors.
13 /// The algorithm uses the HSV color model to generate color variations (see
14 /// http://doc.qt.io/qt-4.8/qcolor.html#the-hsv-color-model)
15 static std::vector<QColor> colors(const QColor &minColor, const QColor &maxColor,
16 int nbColors) noexcept;
17 };
18
19 #endif // SCIQLOP_COLORUTILS_H
@@ -0,0 +1,29
1 #include "Common/ColorUtils.h"
2
3 #include <QtGui/QColor>
4
5 std::vector<QColor> ColorUtils::colors(const QColor &minColor, const QColor &maxColor,
6 int nbColors) noexcept
7 {
8 auto result = std::vector<QColor>{};
9
10 if (nbColors == 1) {
11 result.push_back(minColor);
12 }
13 else if (nbColors > 0) {
14 const auto nbSteps = static_cast<double>(nbColors - 1);
15
16 const auto colorHStep = (maxColor.hue() - minColor.hue()) / nbSteps;
17 const auto colorSStep = (maxColor.saturation() - minColor.saturation()) / nbSteps;
18 const auto colorVStep = (maxColor.value() - minColor.value()) / nbSteps;
19 const auto colorAStep = (maxColor.alpha() - minColor.alpha()) / nbSteps;
20
21 for (auto i = 0; i < nbColors; ++i) {
22 result.push_back(QColor::fromHsv(
23 minColor.hue() + i * colorHStep, minColor.saturation() + i * colorSStep,
24 minColor.value() + i * colorVStep, minColor.alpha() + i * colorAStep));
25 }
26 }
27
28 return result;
29 }
@@ -1,6 +1,8
1 1 #include "Visualization/VisualizationGraphHelper.h"
2 2 #include "Visualization/qcustomplot.h"
3 3
4 #include <Common/ColorUtils.h>
5
4 6 #include <Data/ScalarSeries.h>
5 7 #include <Data/VectorSeries.h>
6 8
@@ -84,9 +86,12 struct PlottablesCreator<T,
84 86 // Gets the number of components of the data series
85 87 auto componentCount = dataSeries.valuesData()->componentCount();
86 88
89 auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount);
90
87 91 // For each component of the data series, creates a QCPGraph to add to the plot
88 92 for (auto i = 0; i < componentCount; ++i) {
89 93 auto graph = plot.addGraph();
94 graph->setPen(QPen{colors.at(i)});
90 95
91 96 result.insert({i, graph});
92 97 }
General Comments 0
You need to be logged in to leave comments. Login now