From 17624d030c393a78f440e7e442d7c5e0ebb3bba4 2017-08-18 14:18:58 From: Alexandre Leroux Date: 2017-08-18 14:18:58 Subject: [PATCH] Sets differents colors for components of a vector --- diff --git a/gui/include/Common/ColorUtils.h b/gui/include/Common/ColorUtils.h new file mode 100644 index 0000000..2ef4f9e --- /dev/null +++ b/gui/include/Common/ColorUtils.h @@ -0,0 +1,19 @@ +#ifndef SCIQLOP_COLORUTILS_H +#define SCIQLOP_COLORUTILS_H + +#include + +class QColor; + +/** + * Utility class with methods for colors + */ +struct ColorUtils { + /// Generates a color scale from min / max values and a number of colors. + /// The algorithm uses the HSV color model to generate color variations (see + /// http://doc.qt.io/qt-4.8/qcolor.html#the-hsv-color-model) + static std::vector colors(const QColor &minColor, const QColor &maxColor, + int nbColors) noexcept; +}; + +#endif // SCIQLOP_COLORUTILS_H diff --git a/gui/src/Common/ColorUtils.cpp b/gui/src/Common/ColorUtils.cpp new file mode 100644 index 0000000..c4f6324 --- /dev/null +++ b/gui/src/Common/ColorUtils.cpp @@ -0,0 +1,29 @@ +#include "Common/ColorUtils.h" + +#include + +std::vector ColorUtils::colors(const QColor &minColor, const QColor &maxColor, + int nbColors) noexcept +{ + auto result = std::vector{}; + + if (nbColors == 1) { + result.push_back(minColor); + } + else if (nbColors > 0) { + const auto nbSteps = static_cast(nbColors - 1); + + const auto colorHStep = (maxColor.hue() - minColor.hue()) / nbSteps; + const auto colorSStep = (maxColor.saturation() - minColor.saturation()) / nbSteps; + const auto colorVStep = (maxColor.value() - minColor.value()) / nbSteps; + const auto colorAStep = (maxColor.alpha() - minColor.alpha()) / nbSteps; + + for (auto i = 0; i < nbColors; ++i) { + result.push_back(QColor::fromHsv( + minColor.hue() + i * colorHStep, minColor.saturation() + i * colorSStep, + minColor.value() + i * colorVStep, minColor.alpha() + i * colorAStep)); + } + } + + return result; +} diff --git a/gui/src/Visualization/VisualizationGraphHelper.cpp b/gui/src/Visualization/VisualizationGraphHelper.cpp index bcff078..022a6a1 100644 --- a/gui/src/Visualization/VisualizationGraphHelper.cpp +++ b/gui/src/Visualization/VisualizationGraphHelper.cpp @@ -1,6 +1,8 @@ #include "Visualization/VisualizationGraphHelper.h" #include "Visualization/qcustomplot.h" +#include + #include #include @@ -84,9 +86,12 @@ struct PlottablesCreatorcomponentCount(); + auto colors = ColorUtils::colors(Qt::blue, Qt::red, componentCount); + // For each component of the data series, creates a QCPGraph to add to the plot for (auto i = 0; i < componentCount; ++i) { auto graph = plot.addGraph(); + graph->setPen(QPen{colors.at(i)}); result.insert({i, graph}); }