6#include "gradientimageparameters.h"
8#include "asyncimagerendercallback.h"
10#include "helperqttypes.h"
11#include "lchadouble.h"
13#include "rgbcolorspace.h"
18#include <qnamespace.h>
20#include <qsharedpointer.h>
25GradientImageParameters::GradientImageParameters()
27 setFirstColor(LchaDouble{0, 0, 0, 1});
28 setFirstColor(LchaDouble{1000, 0, 0, 1});
37LchaDouble GradientImageParameters::completlyNormalizedAndBounded(
const LchaDouble &color)
41 result.c = color.c * (-1);
42 result.h = fmod(color.h + 180, 360);
45 result.h = fmod(color.h, 360);
50 result.l = qBound<qreal>(0, color.l, 100);
51 result.a = qBound<qreal>(0, color.a, 1);
58void GradientImageParameters::setFirstColor(
const LchaDouble &newFirstColor)
60 LchaDouble correctedNewFirstColor =
61 completlyNormalizedAndBounded(newFirstColor);
63 m_firstColorCorrected = correctedNewFirstColor;
73void GradientImageParameters::setSecondColor(
const LchaDouble &newSecondColor)
75 LchaDouble correctedNewSecondColor =
76 completlyNormalizedAndBounded(newSecondColor);
78 m_secondColorCorrectedAndAltered = correctedNewSecondColor;
89void GradientImageParameters::updateSecondColor()
91 m_secondColorCorrectedAndAltered =
92 completlyNormalizedAndBounded(m_secondColorCorrectedAndAltered);
93 if (qAbs(m_firstColorCorrected.
h - m_secondColorCorrectedAndAltered.
h) > 180) {
94 if (m_firstColorCorrected.
h > m_secondColorCorrectedAndAltered.
h) {
95 m_secondColorCorrectedAndAltered.
h += 360;
97 m_secondColorCorrectedAndAltered.
h -= 360;
118void GradientImageParameters::render(
const QVariant &variantParameters, AsyncImageRenderCallback &callbackObject)
120 if (!variantParameters.
canConvert<GradientImageParameters>()) {
123 const GradientImageParameters parameters =
124 variantParameters.
value<GradientImageParameters>();
125 if (parameters.rgbColorSpace.isNull()) {
136 if (callbackObject.shouldAbort()) {
143 QImage onePixelLine(parameters.m_gradientLength,
150 for (
int i = 0; i < parameters.m_gradientLength; ++i) {
151 color = parameters.colorFromValue(
152 (i + 0.5) /
static_cast<qreal
>(parameters.m_gradientLength));
153 cielchD50.l = color.l;
154 cielchD50.c = color.c;
155 cielchD50.h = color.h;
156 temp = parameters.rgbColorSpace->fromCielchD50ToQRgbBound(cielchD50);
159 static_cast<QColorFloatType
>(color.a));
160 onePixelLine.setPixelColor(i, 0, temp);
162 if (callbackObject.shouldAbort()) {
168 parameters.m_gradientThickness,
180 (parameters.m_firstColorCorrected.a != 1)
181 || (parameters.m_secondColorCorrectedAndAltered.a != 1)
185 const auto background = transparencyBackground(
186 parameters.m_devicePixelRatioF);
189 parameters.m_gradientLength,
190 parameters.m_gradientThickness,
195 for (
int i = 0; i < parameters.m_gradientThickness; ++i) {
196 painter.drawImage(0, i, onePixelLine);
201 if (callbackObject.shouldAbort()) {
205 callbackObject.deliverInterlacingPass(
208 AsyncImageRenderCallback::InterlacingState::Final);
218LchaDouble GradientImageParameters::colorFromValue(qreal value)
const
221 color.l = m_firstColorCorrected.
l
222 + (m_secondColorCorrectedAndAltered.
l - m_firstColorCorrected.
l) * value;
223 color.c = m_firstColorCorrected.
c +
224 (m_secondColorCorrectedAndAltered.
c - m_firstColorCorrected.
c) * value;
225 color.h = m_firstColorCorrected.
h +
226 (m_secondColorCorrectedAndAltered.
h - m_firstColorCorrected.
h) * value;
227 color.a = m_firstColorCorrected.
a +
228 (m_secondColorCorrectedAndAltered.
a - m_firstColorCorrected.
a) * value;
251void GradientImageParameters::setDevicePixelRatioF(
const qreal newDevicePixelRatioF)
253 const qreal tempDevicePixelRatioF = qMax<qreal>(1, newDevicePixelRatioF);
254 if (m_devicePixelRatioF != tempDevicePixelRatioF) {
255 m_devicePixelRatioF = tempDevicePixelRatioF;
265void GradientImageParameters::setGradientLength(
const int newGradientLength)
267 const int temp = qMax(0, newGradientLength);
268 if (m_gradientLength != temp) {
269 m_gradientLength = temp;
279void GradientImageParameters::setGradientThickness(
const int newGradientThickness)
281 const int temp = qMax(0, newGradientThickness);
282 if (m_gradientThickness != temp) {
283 m_gradientThickness = temp;
294bool GradientImageParameters::operator==(
const GradientImageParameters &other)
const
297 (m_devicePixelRatioF == other.m_devicePixelRatioF)
298 && (m_firstColorCorrected.
l == other.m_firstColorCorrected.l)
299 && (m_firstColorCorrected.
c == other.m_firstColorCorrected.c)
300 && (m_firstColorCorrected.
h == other.m_firstColorCorrected.h)
301 && (m_firstColorCorrected.
a == other.m_firstColorCorrected.a)
302 && (m_gradientLength == other.m_gradientLength)
303 && (m_gradientThickness == other.m_gradientThickness)
304 && (rgbColorSpace == other.rgbColorSpace)
305 && (m_secondColorCorrectedAndAltered.
l == other.m_secondColorCorrectedAndAltered.l)
306 && (m_secondColorCorrectedAndAltered.
c == other.m_secondColorCorrectedAndAltered.c)
307 && (m_secondColorCorrectedAndAltered.
h == other.m_secondColorCorrectedAndAltered.h)
308 && (m_secondColorCorrectedAndAltered.
a == other.m_secondColorCorrectedAndAltered.a)
317bool GradientImageParameters::operator!=(
const GradientImageParameters &other)
const
319 return !(*
this == other);
The namespace of this library.
void setAlphaF(float alpha)
bool isNull() const const
void setDevicePixelRatio(qreal scaleFactor)
bool canConvert() const const
bool hasSameCoordinates(const LchaDouble &other) const
Compares coordinates with another object.
double a
Opacity (alpha channel)
double h
Hue, measured in degree.
double l
Lightness, mesured in percent.