inverted gradient vectors for horizontal/vertical gradients supported

This commit is contained in:
Uwe Rathmann 2023-01-10 12:17:56 +01:00
parent d2c5413daa
commit 4038f52cdf
3 changed files with 41 additions and 4 deletions

View File

@ -312,6 +312,17 @@ QskGradientStops qskExtractedGradientStops(
return extracted; return extracted;
} }
QskGradientStops qskRevertedGradientStops( const QskGradientStops& stops )
{
QVector< QskGradientStop > s;
s.reserve( stops.count() );
for ( auto it = stops.crbegin(); it != stops.crend(); ++it )
s += QskGradientStop( 1.0 - it->position(), it->color() );
return s;
}
QVector< QskGradientStop > qskBuildGradientStops( const QGradientStops& qtStops ) QVector< QskGradientStop > qskBuildGradientStops( const QGradientStops& qtStops )
{ {
QVector< QskGradientStop > stops; QVector< QskGradientStop > stops;

View File

@ -145,6 +145,8 @@ QSK_EXPORT QskGradientStops qskBuildGradientStops(
QSK_EXPORT QskGradientStops qskBuildGradientStops( QSK_EXPORT QskGradientStops qskBuildGradientStops(
const QVector< QColor >&, bool discrete = false ); const QVector< QColor >&, bool discrete = false );
QSK_EXPORT QskGradientStops qskRevertedGradientStops( const QskGradientStops& );
QSK_EXPORT QskGradientStops qskBuildGradientStops( const QVector< QGradientStop >& ); QSK_EXPORT QskGradientStops qskBuildGradientStops( const QVector< QGradientStop >& );
QSK_EXPORT QVector< QGradientStop > qskToQGradientStops( const QVector< QskGradientStop >& ); QSK_EXPORT QVector< QGradientStop > qskToQGradientStops( const QVector< QskGradientStop >& );

View File

@ -11,6 +11,30 @@
#include "QskGradient.h" #include "QskGradient.h"
#include "QskGradientDirection.h" #include "QskGradientDirection.h"
static inline QskGradient qskNormalizedGradient( const QskGradient gradient )
{
const auto dir = gradient.linearDirection();
if ( !dir.isTilted() )
{
/*
Dealing with inverted gradient vectors makes the code even
more unreadable. So we simply invert stops/vector instead.
*/
if ( ( dir.x1() > dir.x2() ) || ( dir.y1() > dir.y2() ) )
{
QskGradient g = gradient;
g.setLinearDirection( dir.x2(), dir.y2(), dir.x1(), dir.y1() );
if ( !g.isMonochrome() )
g.setStops( qskRevertedGradientStops( gradient.stops() ) );
return g;
}
}
return gradient;
}
void QskBoxRenderer::renderBorder( void QskBoxRenderer::renderBorder(
const QRectF& rect, const QskBoxShapeMetrics& shape, const QRectF& rect, const QskBoxShapeMetrics& shape,
const QskBoxBorderMetrics& border, QSGGeometry& geometry ) const QskBoxBorderMetrics& border, QSGGeometry& geometry )
@ -36,15 +60,17 @@ void QskBoxRenderer::renderBox( const QRectF& rect,
const QskBoxBorderColors& borderColors, const QskGradient& gradient, const QskBoxBorderColors& borderColors, const QskGradient& gradient,
QSGGeometry& geometry ) QSGGeometry& geometry )
{ {
const auto gradientN = qskNormalizedGradient( gradient );
if ( shape.isRectangle() ) if ( shape.isRectangle() )
{ {
QskRectRenderer::renderRect( QskRectRenderer::renderRect(
rect, border, borderColors, gradient, geometry ); rect, border, borderColors, gradientN, geometry );
} }
else else
{ {
QskRoundedRectRenderer::renderRectellipse( QskRoundedRectRenderer::renderRectellipse(
rect, shape, border, borderColors, gradient, geometry ); rect, shape, border, borderColors, gradientN, geometry );
} }
} }
@ -70,8 +96,6 @@ bool QskBoxRenderer::isGradientSupported(
case QskGradient::Linear: case QskGradient::Linear:
{ {
const auto dir = gradient.linearDirection(); const auto dir = gradient.linearDirection();
if ( dir.x1() > dir.x2() || dir.y1() > dir.y2() )
return false;
if ( shape.isRectangle() ) if ( shape.isRectangle() )
{ {