KQuickCharts

ColorGradientSource.cpp
1/*
2 * This file is part of KQuickCharts
3 * SPDX-FileCopyrightText: 2019 Marco Martin <mart@kde.org>
4 * SPDX-FileCopyrightText: 2019 David Edmundson <davidedmundson@kde.org>
5 * SPDX-FileCopyrightText: 2019 Arjen Hiemstra <ahiemstra@heimr.nl>
6 *
7 * SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
8 */
9
10#include "ColorGradientSource.h"
11
12#include <QVariant>
13
14#include <QDebug>
15
16ColorGradientSource::ColorGradientSource(QObject *parent)
17 : ChartDataSource(parent)
18{
19}
20
21int ColorGradientSource::itemCount() const
22{
23 return m_itemCount;
24}
25
26QVariant ColorGradientSource::item(int index) const
27{
28 if (index < 0 || index >= m_colors.size()) {
29 return QVariant{};
30 }
31
32 return m_colors.at(index);
33}
34
35QVariant ColorGradientSource::minimum() const
36{
37 return QVariant{};
38}
39
40QVariant ColorGradientSource::maximum() const
41{
42 return QVariant{};
43}
44
45QColor ColorGradientSource::baseColor() const
46{
47 return m_baseColor;
48}
49
50void ColorGradientSource::setBaseColor(const QColor &newBaseColor)
51{
52 if (newBaseColor == m_baseColor) {
53 return;
54 }
55
56 m_baseColor = newBaseColor;
57 regenerateColors();
58 Q_EMIT baseColorChanged();
59}
60
61void ColorGradientSource::setItemCount(int newItemCount)
62{
63 if (newItemCount == m_itemCount) {
64 return;
65 }
66
67 m_itemCount = newItemCount;
68 regenerateColors();
69 Q_EMIT itemCountChanged();
70}
71
72QVariantList ColorGradientSource::colors() const
73{
74 QVariantList colorsVariant;
75 colorsVariant.reserve(m_colors.count());
76 for (const QColor &color : std::as_const(m_colors)) {
77 colorsVariant.append(color);
78 }
79 return colorsVariant;
80}
81
82void ColorGradientSource::regenerateColors()
83{
84 if (!m_baseColor.isValid() || m_itemCount <= 0) {
85 return;
86 }
87
88 m_colors.clear();
89
90 for (int i = 0; i < m_itemCount; ++i) {
91 auto newHue = m_baseColor.hsvHueF() + i * (1.0 / m_itemCount);
92 newHue = newHue - int(newHue);
93 m_colors.append(QColor::fromHsvF(newHue, m_baseColor.saturationF(), m_baseColor.valueF(), m_baseColor.alphaF()));
94 }
95
96 Q_EMIT dataChanged();
97}
98
99#include "moc_ColorGradientSource.cpp"
Abstract base class for data sources.
float alphaF() const const
QColor fromHsvF(float h, float s, float v, float a)
float hsvHueF() const const
bool isValid() const const
float saturationF() const const
float valueF() const const
void append(QList< T > &&value)
const_reference at(qsizetype i) const const
void clear()
qsizetype count() const const
qsizetype size() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:08:53 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.