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