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 | |
16 | ColorGradientSource::ColorGradientSource(QObject *parent) |
17 | : ChartDataSource(parent) |
18 | { |
19 | } |
20 | |
21 | int ColorGradientSource::itemCount() const |
22 | { |
23 | return m_itemCount; |
24 | } |
25 | |
26 | QVariant ColorGradientSource::item(int index) const |
27 | { |
28 | if (index < 0 || index >= m_colors.size()) { |
29 | return QVariant{}; |
30 | } |
31 | |
32 | return m_colors.at(i: index); |
33 | } |
34 | |
35 | QVariant ColorGradientSource::minimum() const |
36 | { |
37 | return QVariant{}; |
38 | } |
39 | |
40 | QVariant ColorGradientSource::maximum() const |
41 | { |
42 | return QVariant{}; |
43 | } |
44 | |
45 | QColor ColorGradientSource::baseColor() const |
46 | { |
47 | return m_baseColor; |
48 | } |
49 | |
50 | void 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 | |
61 | void 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 | |
72 | QVariantList ColorGradientSource::colors() const |
73 | { |
74 | QVariantList colorsVariant; |
75 | colorsVariant.reserve(asize: m_colors.count()); |
76 | for (const QColor &color : std::as_const(t: m_colors)) { |
77 | colorsVariant.append(t: color); |
78 | } |
79 | return colorsVariant; |
80 | } |
81 | |
82 | void 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(t: QColor::fromHsvF(h: newHue, s: m_baseColor.saturationF(), v: m_baseColor.valueF(), a: m_baseColor.alphaF())); |
94 | } |
95 | |
96 | Q_EMIT dataChanged(); |
97 | } |
98 | |
99 | #include "moc_ColorGradientSource.cpp" |
100 |