6#include "gradientimageparameters.h"
8#include "asyncimagerendercallback.h"
10#include "helperqttypes.h"
11#include "rgbcolorspace.h"
16#include <qnamespace.h>
18#include <qsharedpointer.h>
23GradientImageParameters::GradientImageParameters()
25 setFirstColorCieLchD50A(GenericColor{0, 0, 0, 1});
26 setFirstColorCieLchD50A(GenericColor{1000, 0, 0, 1});
35GenericColor GradientImageParameters::completlyNormalizedAndBounded(
const GenericColor &color)
38 if (color.second < 0) {
39 result.second = color.second * (-1);
40 result.third = fmod(color.third + 180, 360);
42 result.second = color.second;
43 result.third = fmod(color.third, 360);
45 if (result.third < 0) {
48 result.first = qBound<qreal>(0, color.first, 100);
49 result.fourth = qBound<qreal>(0, color.fourth, 1);
56void GradientImageParameters::setFirstColorCieLchD50A(
const GenericColor &newFirstColor)
58 GenericColor correctedNewFirstColor =
59 completlyNormalizedAndBounded(newFirstColor);
60 if (!(m_firstColorCorrected == correctedNewFirstColor)) {
61 m_firstColorCorrected = correctedNewFirstColor;
71void GradientImageParameters::setSecondColorCieLchD50A(
const GenericColor &newSecondColor)
73 GenericColor correctedNewSecondColor =
74 completlyNormalizedAndBounded(newSecondColor);
75 if (!(m_secondColorCorrectedAndAltered == correctedNewSecondColor)) {
76 m_secondColorCorrectedAndAltered = correctedNewSecondColor;
87void GradientImageParameters::updateSecondColor()
89 m_secondColorCorrectedAndAltered =
90 completlyNormalizedAndBounded(m_secondColorCorrectedAndAltered);
91 if (qAbs(m_firstColorCorrected.third - m_secondColorCorrectedAndAltered.third) > 180) {
92 if (m_firstColorCorrected.third > m_secondColorCorrectedAndAltered.third) {
93 m_secondColorCorrectedAndAltered.third += 360;
95 m_secondColorCorrectedAndAltered.third -= 360;
116void GradientImageParameters::render(
const QVariant &variantParameters, AsyncImageRenderCallback &callbackObject)
118 if (!variantParameters.
canConvert<GradientImageParameters>()) {
121 const GradientImageParameters parameters =
122 variantParameters.
value<GradientImageParameters>();
123 if (parameters.rgbColorSpace.isNull()) {
134 if (callbackObject.shouldAbort()) {
141 QImage onePixelLine(parameters.m_gradientLength,
146 GenericColor cielchD50;
148 for (
int i = 0; i < parameters.m_gradientLength; ++i) {
149 color = parameters.colorFromValue(
150 (i + 0.5) /
static_cast<qreal
>(parameters.m_gradientLength));
151 cielchD50.first = color.first;
152 cielchD50.second = color.second;
153 cielchD50.third = color.third;
154 temp = parameters.rgbColorSpace->fromCielchD50ToQRgbBound(cielchD50);
157 static_cast<QColorFloatType
>(color.fourth));
158 onePixelLine.setPixelColor(i, 0, temp);
160 if (callbackObject.shouldAbort()) {
166 parameters.m_gradientThickness,
178 (parameters.m_firstColorCorrected.fourth != 1)
179 || (parameters.m_secondColorCorrectedAndAltered.fourth != 1)
183 const auto background = transparencyBackground(
184 parameters.m_devicePixelRatioF);
187 parameters.m_gradientLength,
188 parameters.m_gradientThickness,
193 for (
int i = 0; i < parameters.m_gradientThickness; ++i) {
194 painter.drawImage(0, i, onePixelLine);
199 if (callbackObject.shouldAbort()) {
203 callbackObject.deliverInterlacingPass(
206 AsyncImageRenderCallback::InterlacingState::Final);
216GenericColor GradientImageParameters::colorFromValue(qreal value)
const
219 color.first = m_firstColorCorrected.first
220 + (m_secondColorCorrectedAndAltered.first - m_firstColorCorrected.first) * value;
221 color.second = m_firstColorCorrected.second +
222 (m_secondColorCorrectedAndAltered.second - m_firstColorCorrected.second) * value;
223 color.third = m_firstColorCorrected.third +
224 (m_secondColorCorrectedAndAltered.third - m_firstColorCorrected.third) * value;
225 color.fourth = m_firstColorCorrected.fourth +
226 (m_secondColorCorrectedAndAltered.fourth - m_firstColorCorrected.fourth) * value;
249void GradientImageParameters::setDevicePixelRatioF(
const qreal newDevicePixelRatioF)
251 const qreal tempDevicePixelRatioF = qMax<qreal>(1, newDevicePixelRatioF);
252 if (m_devicePixelRatioF != tempDevicePixelRatioF) {
253 m_devicePixelRatioF = tempDevicePixelRatioF;
263void GradientImageParameters::setGradientLength(
const int newGradientLength)
265 const int temp = qMax(0, newGradientLength);
266 if (m_gradientLength != temp) {
267 m_gradientLength = temp;
277void GradientImageParameters::setGradientThickness(
const int newGradientThickness)
279 const int temp = qMax(0, newGradientThickness);
280 if (m_gradientThickness != temp) {
281 m_gradientThickness = temp;
292bool GradientImageParameters::operator==(
const GradientImageParameters &other)
const
295 (m_devicePixelRatioF == other.m_devicePixelRatioF)
296 && (m_firstColorCorrected.first == other.m_firstColorCorrected.first)
297 && (m_firstColorCorrected.second == other.m_firstColorCorrected.second)
298 && (m_firstColorCorrected.third == other.m_firstColorCorrected.third)
299 && (m_firstColorCorrected.fourth == other.m_firstColorCorrected.fourth)
300 && (m_gradientLength == other.m_gradientLength)
301 && (m_gradientThickness == other.m_gradientThickness)
302 && (rgbColorSpace == other.rgbColorSpace)
303 && (m_secondColorCorrectedAndAltered.first == other.m_secondColorCorrectedAndAltered.first)
304 && (m_secondColorCorrectedAndAltered.second == other.m_secondColorCorrectedAndAltered.second)
305 && (m_secondColorCorrectedAndAltered.third == other.m_secondColorCorrectedAndAltered.third)
306 && (m_secondColorCorrectedAndAltered.fourth == other.m_secondColorCorrectedAndAltered.fourth)
315bool GradientImageParameters::operator!=(
const GradientImageParameters &other)
const
317 return !(*
this == other);
The namespace of this library.
void setAlphaF(float alpha)
bool isNull() const const
void setDevicePixelRatio(qreal scaleFactor)
bool canConvert() const const