1// Copyright (C) 2023 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only
3
4#include "commonutils_p.h"
5#include "qquickgraphstexturedata_p.h"
6
7#include <QtGraphs/private/qgraphsglobal_p.h>
8
9QT_BEGIN_NAMESPACE
10
11QQuickGraphsTextureData::QQuickGraphsTextureData() {}
12
13QQuickGraphsTextureData::~QQuickGraphsTextureData() {}
14
15void QQuickGraphsTextureData::createGradient(QLinearGradient gradient)
16{
17 const qreal textureWidth = CommonUtils::maxTextureSize();
18 setSize(QSize(textureWidth, gradientTextureHeight));
19 setFormat(QQuick3DTextureData::RGBA8);
20 setHasTransparency(false);
21
22 // Make sure the gradient fills the whole image
23 gradient.setFinalStop(x: textureWidth, y: gradientTextureHeight);
24 gradient.setStart(x: 0., y: 0.);
25
26 QByteArray imageData;
27
28 QByteArray gradientScanline;
29 gradientScanline.resize(size: textureWidth * 4); // RGBA8
30 auto stops = gradient.stops();
31
32 int x = 0;
33 for (int i = 1; i < stops.size(); i++) {
34 QColor startColor = stops.at(i: i - 1).second;
35 QColor endColor = stops.at(i).second;
36 float w = 0.0f;
37 w = (stops.at(i).first - stops.at(i: i - 1).first) * textureWidth;
38
39 if (startColor.alphaF() < 1.0 || endColor.alphaF() < 1.0)
40 setHasTransparency(true);
41
42 for (int t = 0; t <= static_cast<int>(w); t++) {
43 QColor color = linearInterpolate(startColor, endColor, value: t / w);
44 int offset = x * 4;
45 gradientScanline.data()[offset + 0] = char(color.red());
46 gradientScanline.data()[offset + 1] = char(color.green());
47 gradientScanline.data()[offset + 2] = char(color.blue());
48 gradientScanline.data()[offset + 3] = char(color.alpha());
49 x++;
50 }
51 }
52 for (int y = 0; y < gradientTextureHeight; y++)
53 imageData += gradientScanline;
54 setTextureData(imageData);
55}
56
57QColor QQuickGraphsTextureData::linearInterpolate(QColor startColor, QColor endColor, float value)
58{
59 QColor output;
60
61 output.setRedF(startColor.redF() + (value * (endColor.redF() - startColor.redF())));
62 output.setGreenF(startColor.greenF() + (value * (endColor.greenF() - startColor.greenF())));
63 output.setBlueF(startColor.blueF() + (value * (endColor.blueF() - startColor.blueF())));
64 output.setAlphaF(startColor.alphaF() + (value * (endColor.alphaF() - startColor.alphaF())));
65
66 return output;
67}
68
69QT_END_NAMESPACE
70

source code of qtgraphs/src/graphs3d/qml/qquickgraphstexturedata.cpp