1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
// Copyright (C) 2020 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
#include "gradienttexture.h"
#include <QtCore/QSize>
#include <QtGui/QGradient>
GradientTexture::GradientTexture()
{
updateTexture();
}
int GradientTexture::height() const
{
return m_height;
}
int GradientTexture::width() const
{
return m_width;
}
QColor GradientTexture::startColor() const
{
return m_startColor;
}
QColor GradientTexture::endColor() const
{
return m_endColor;
}
void GradientTexture::setHeight(int height)
{
if (m_height == height)
return;
m_height = height;
emit heightChanged(m_height);
updateTexture();
}
void GradientTexture::setWidth(int width)
{
if (m_width == width)
return;
m_width = width;
emit widthChanged(m_width);
updateTexture();
}
//! [property]
void GradientTexture::setStartColor(QColor startColor)
{
if (m_startColor == startColor)
return;
m_startColor = startColor;
emit startColorChanged(m_startColor);
updateTexture();
}
//! [property]
void GradientTexture::setEndColor(QColor endColor)
{
if (m_endColor == endColor)
return;
m_endColor = endColor;
emit endColorChanged(m_endColor);
updateTexture();
}
//! [updateTexture]
void GradientTexture::updateTexture()
{
setSize(QSize(m_width, m_height));
setFormat(QQuick3DTextureData::RGBA8);
setHasTransparency(false);
setTextureData(generateTexture());
}
//! [updateTexture]
//! [generateTexture]
QByteArray GradientTexture::generateTexture()
{
QByteArray imageData;
// Create a horizontal gradient between startColor and endColor
// Create a single scanline and reuse that data for each
QByteArray gradientScanline;
gradientScanline.resize(m_width * 4); // RGBA8
for (int x = 0; x < m_width; ++x) {
QColor color = linearInterpolate(m_startColor, m_endColor, x / float(m_width));
int offset = x * 4;
gradientScanline.data()[offset + 0] = char(color.red());
gradientScanline.data()[offset + 1] = char(color.green());
gradientScanline.data()[offset + 2] = char(color.blue());
gradientScanline.data()[offset + 3] = char(255);
}
for (int y = 0; y < m_height; ++y)
imageData += gradientScanline;
return imageData;
}
//! [generateTexture]
QColor GradientTexture::linearInterpolate(const QColor &color1, const QColor &color2, float value)
{
QColor output;
output.setRedF(color1.redF() + (value * (color2.redF() - color1.redF())));
output.setGreenF(color1.greenF() + (value * (color2.greenF() - color1.greenF())));
output.setBlueF(color1.blueF() + (value * (color2.blueF() - color1.blueF())));
return output;
}
|