| 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 | |
| 9 | QQuickGraphsTextureData::QQuickGraphsTextureData() {} |
| 10 | |
| 11 | QQuickGraphsTextureData::~QQuickGraphsTextureData() {} |
| 12 | |
| 13 | void QQuickGraphsTextureData::createGradient(QLinearGradient gradient) |
| 14 | { |
| 15 | const qreal textureWidth = CommonUtils::maxTextureSize(); |
| 16 | setSize(QSize(textureWidth, gradientTextureHeight)); |
| 17 | setFormat(QQuick3DTextureData::RGBA8); |
| 18 | setHasTransparency(false); |
| 19 | |
| 20 | // Make sure the gradient fills the whole image |
| 21 | gradient.setFinalStop(x: textureWidth, y: gradientTextureHeight); |
| 22 | gradient.setStart(x: 0., y: 0.); |
| 23 | |
| 24 | QByteArray imageData; |
| 25 | |
| 26 | QByteArray gradientScanline; |
| 27 | gradientScanline.resize(size: textureWidth * 4); // RGBA8 |
| 28 | auto stops = gradient.stops(); |
| 29 | |
| 30 | int x = 0; |
| 31 | for (int i = 1; i < stops.size(); i++) { |
| 32 | QColor startColor = stops.at(i: i - 1).second; |
| 33 | QColor endColor = stops.at(i).second; |
| 34 | int w = 0; |
| 35 | w = (stops.at(i).first - stops.at(i: i - 1).first) * textureWidth; |
| 36 | |
| 37 | if (startColor.alphaF() < 1.0 || endColor.alphaF() < 1.0) |
| 38 | setHasTransparency(true); |
| 39 | |
| 40 | for (int t = 0; t <= w; t++) { |
| 41 | QColor color = linearInterpolate(startColor, endColor, value: t / float(w)); |
| 42 | int offset = x * 4; |
| 43 | gradientScanline.data()[offset + 0] = char(color.red()); |
| 44 | gradientScanline.data()[offset + 1] = char(color.green()); |
| 45 | gradientScanline.data()[offset + 2] = char(color.blue()); |
| 46 | gradientScanline.data()[offset + 3] = char(color.alpha()); |
| 47 | x++; |
| 48 | } |
| 49 | } |
| 50 | for (int y = 0; y < gradientTextureHeight; y++) |
| 51 | imageData += gradientScanline; |
| 52 | setTextureData(imageData); |
| 53 | } |
| 54 | |
| 55 | QColor QQuickGraphsTextureData::linearInterpolate(QColor startColor, QColor endColor, float value) |
| 56 | { |
| 57 | QColor output; |
| 58 | |
| 59 | output.setRedF(startColor.redF() + (value * (endColor.redF() - startColor.redF()))); |
| 60 | output.setGreenF(startColor.greenF() + (value * (endColor.greenF() - startColor.greenF()))); |
| 61 | output.setBlueF(startColor.blueF() + (value * (endColor.blueF() - startColor.blueF()))); |
| 62 | output.setAlphaF(startColor.alphaF() + (value * (endColor.alphaF() - startColor.alphaF()))); |
| 63 | |
| 64 | return output; |
| 65 | } |
| 66 |
