QskBoxBorderColors: Use gradients instead of colors

This commit is contained in:
Peter Hartmann 2021-12-13 09:42:21 +01:00
parent ab0fe2ac1c
commit 16df96700d
5 changed files with 66 additions and 91 deletions

View File

@ -19,19 +19,14 @@ static void qskRegisterBoxBorderColors()
Q_CONSTRUCTOR_FUNCTION( qskRegisterBoxBorderColors ) Q_CONSTRUCTOR_FUNCTION( qskRegisterBoxBorderColors )
static inline bool qskIsVisble( const QColor& c ) static inline void qskSetColors( const QColor& c, QskGradient* colors )
{
return c.isValid() && ( c.alpha() > 0 );
}
static inline void qskSetColors( const QColor& c, QColor* colors )
{ {
colors[ 0 ] = colors[ 1 ] = colors[ 2 ] = colors[ 3 ] = c.toRgb(); colors[ 0 ] = colors[ 1 ] = colors[ 2 ] = colors[ 3 ] = c.toRgb();
} }
static inline void qskSetColors( static inline void qskSetColors(
const QColor& left, const QColor& top, const QColor& left, const QColor& top,
const QColor& right, const QColor& bottom, QColor* colors ) const QColor& right, const QColor& bottom, QskGradient* colors )
{ {
colors[ Qsk::Left ] = left.toRgb(); colors[ Qsk::Left ] = left.toRgb();
colors[ Qsk::Top ] = top.toRgb(); colors[ Qsk::Top ] = top.toRgb();
@ -47,12 +42,12 @@ QskBoxBorderColors::QskBoxBorderColors(
const QColor& left, const QColor& top, const QColor& left, const QColor& top,
const QColor& right, const QColor& bottom ) const QColor& right, const QColor& bottom )
{ {
qskSetColors( left, top, right, bottom, m_colors ); qskSetColors( left, top, right, bottom, m_gradients );
} }
QskBoxBorderColors::QskBoxBorderColors( const QColor& color ) QskBoxBorderColors::QskBoxBorderColors( const QColor& color )
{ {
qskSetColors( color, m_colors ); qskSetColors( color, m_gradients );
} }
QskBoxBorderColors::~QskBoxBorderColors() QskBoxBorderColors::~QskBoxBorderColors()
@ -61,37 +56,37 @@ QskBoxBorderColors::~QskBoxBorderColors()
bool QskBoxBorderColors::operator==( const QskBoxBorderColors& other ) const bool QskBoxBorderColors::operator==( const QskBoxBorderColors& other ) const
{ {
return ( m_colors[ 0 ] == other.m_colors[ 0 ] ) && return ( m_gradients[ 0 ] == other.m_gradients[ 0 ] ) &&
( m_colors[ 1 ] == other.m_colors[ 1 ] ) && ( m_gradients[ 1 ] == other.m_gradients[ 1 ] ) &&
( m_colors[ 2 ] == other.m_colors[ 2 ] ) && ( m_gradients[ 2 ] == other.m_gradients[ 2 ] ) &&
( m_colors[ 3 ] == other.m_colors[ 3 ] ); ( m_gradients[ 3 ] == other.m_gradients[ 3 ] );
} }
void QskBoxBorderColors::setAlpha( int alpha ) void QskBoxBorderColors::setAlpha( int alpha )
{ {
for ( int i = 0; i < 4; i++ ) for ( int i = 0; i < 4; i++ )
{ {
if ( m_colors[ i ].isValid() && m_colors[ i ].alpha() ) if ( m_gradients[ i ].isValid() )
m_colors[ i ].setAlpha( alpha ); m_gradients[ i ].setAlpha( alpha );
} }
} }
void QskBoxBorderColors::setColors( const QColor& color ) void QskBoxBorderColors::setColors( const QColor& color )
{ {
qskSetColors( color, m_colors ); qskSetColors( color, m_gradients );
} }
void QskBoxBorderColors::setColors( void QskBoxBorderColors::setColors(
const QColor& left, const QColor& top, const QColor& left, const QColor& top,
const QColor& right, const QColor& bottom ) const QColor& right, const QColor& bottom )
{ {
qskSetColors( left, top, right, bottom, m_colors ); qskSetColors( left, top, right, bottom, m_gradients );
} }
void QskBoxBorderColors::setColor( void QskBoxBorderColors::setColor(
Qsk::Position position, const QColor& color ) Qsk::Position position, const QColor& color )
{ {
m_colors[ position ] = color.toRgb(); m_gradients[ position ] = color.toRgb();
} }
void QskBoxBorderColors::setColorsAt( Qt::Edges edges, const QColor& color ) void QskBoxBorderColors::setColorsAt( Qt::Edges edges, const QColor& color )
@ -99,33 +94,33 @@ void QskBoxBorderColors::setColorsAt( Qt::Edges edges, const QColor& color )
const QColor c = color.toRgb(); const QColor c = color.toRgb();
if ( edges & Qt::TopEdge ) if ( edges & Qt::TopEdge )
m_colors[ Qsk::Top ] = c; m_gradients[ Qsk::Top ] = c;
if ( edges & Qt::LeftEdge ) if ( edges & Qt::LeftEdge )
m_colors[ Qsk::Left ] = c; m_gradients[ Qsk::Left ] = c;
if ( edges & Qt::RightEdge ) if ( edges & Qt::RightEdge )
m_colors[ Qsk::Right ] = c; m_gradients[ Qsk::Right ] = c;
if ( edges & Qt::BottomEdge ) if ( edges & Qt::BottomEdge )
m_colors[ Qsk::Bottom ] = c; m_gradients[ Qsk::Bottom ] = c;
} }
QColor QskBoxBorderColors::colorAt( Qt::Edge edge ) const QskGradient QskBoxBorderColors::colorAt( Qt::Edge edge ) const
{ {
switch ( edge ) switch ( edge )
{ {
case Qt::TopEdge: case Qt::TopEdge:
return m_colors[ Qsk::Top ]; return m_gradients[ Qsk::Top ];
case Qt::LeftEdge: case Qt::LeftEdge:
return m_colors[ Qsk::Left ]; return m_gradients[ Qsk::Left ];
case Qt::RightEdge: case Qt::RightEdge:
return m_colors[ Qsk::Right ]; return m_gradients[ Qsk::Right ];
case Qt::BottomEdge: case Qt::BottomEdge:
return m_colors[ Qsk::Bottom ]; return m_gradients[ Qsk::Bottom ];
} }
return QColor(); return QColor();
@ -133,16 +128,16 @@ QColor QskBoxBorderColors::colorAt( Qt::Edge edge ) const
bool QskBoxBorderColors::isVisible() const bool QskBoxBorderColors::isVisible() const
{ {
if ( qskIsVisble( m_colors[ 0 ] ) ) if ( m_gradients[ 0 ].isVisible() )
return true; return true;
if ( qskIsVisble( m_colors[ 1 ] ) ) if ( m_gradients[ 1 ].isVisible() )
return true; return true;
if ( qskIsVisble( m_colors[ 2 ] ) ) if ( m_gradients[ 2 ].isVisible() )
return true; return true;
if ( qskIsVisble( m_colors[ 3 ] ) ) if ( m_gradients[ 3 ].isVisible() )
return true; return true;
return false; return false;
@ -150,16 +145,19 @@ bool QskBoxBorderColors::isVisible() const
bool QskBoxBorderColors::isMonochrome() const bool QskBoxBorderColors::isMonochrome() const
{ {
if ( m_colors[ 1 ] != m_colors[ 0 ] ) if ( m_gradients[ 1 ] != m_gradients[ 0 ] )
return false; return false;
if ( m_colors[ 2 ] != m_colors[ 1 ] ) if ( m_gradients[ 2 ] != m_gradients[ 1 ] )
return false; return false;
if ( m_colors[ 3 ] != m_colors[ 2 ] ) if ( m_gradients[ 3 ] != m_gradients[ 2 ] )
return false; return false;
return true; return m_gradients[ 0 ].isMonochrome()
&& m_gradients[ 1 ].isMonochrome()
&& m_gradients[ 2 ].isMonochrome()
&& m_gradients[ 3 ].isMonochrome();
} }
QskBoxBorderColors QskBoxBorderColors::interpolated( QskBoxBorderColors QskBoxBorderColors::interpolated(
@ -169,8 +167,8 @@ QskBoxBorderColors QskBoxBorderColors::interpolated(
for ( size_t i = 0; i < 4; i++ ) for ( size_t i = 0; i < 4; i++ )
{ {
colors.m_colors[ i ] = QskRgb::interpolated( colors.m_gradients[ i ] = colors.m_gradients[ i ].interpolated(
m_colors[ i ], to.m_colors[ i ], ratio ); to.m_gradients[ i ], ratio );
} }
return colors; return colors;
@ -184,30 +182,18 @@ QVariant QskBoxBorderColors::interpolate(
uint QskBoxBorderColors::hash( uint seed ) const uint QskBoxBorderColors::hash( uint seed ) const
{ {
const QRgb rgb[] = uint h = m_gradients[ 0 ].hash( seed );
{ h = m_gradients[ 1 ].hash( h );
m_colors[ 0 ].rgba(), h = m_gradients[ 2 ].hash( h );
m_colors[ 1 ].rgba(), h = m_gradients[ 3 ].hash( h );
m_colors[ 2 ].rgba(),
m_colors[ 3 ].rgba(),
};
return qHashBits( rgb, sizeof( rgb ), seed ); return h;
} }
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM
#include <qdebug.h> #include <qdebug.h>
static inline void qskDebugColor( QDebug debug, const QColor& c )
{
debug << '('
<< c.red() << ','
<< c.green() << ','
<< c.blue() << ','
<< c.alpha() << ')';
}
QDebug operator<<( QDebug debug, const QskBoxBorderColors& colors ) QDebug operator<<( QDebug debug, const QskBoxBorderColors& colors )
{ {
QDebugStateSaver saver( debug ); QDebugStateSaver saver( debug );
@ -215,17 +201,13 @@ QDebug operator<<( QDebug debug, const QskBoxBorderColors& colors )
debug << "BoxBorderColors" << '('; debug << "BoxBorderColors" << '(';
debug << " L"; debug << " L" << colors.color( Qsk::Left );
qskDebugColor( debug, colors.color( Qsk::Left ) );
debug << ", T"; debug << ", T" << colors.color( Qsk::Top );
qskDebugColor( debug, colors.color( Qsk::Top ) );
debug << ", R"; debug << ", R" << colors.color( Qsk::Right );
qskDebugColor( debug, colors.color( Qsk::Right ) );
debug << ", B"; debug << ", B" << colors.color( Qsk::Bottom );
qskDebugColor( debug, colors.color( Qsk::Bottom ) );
debug << " )"; debug << " )";

View File

@ -6,6 +6,7 @@
#ifndef QSK_BOX_BORDER_COLORS_H #ifndef QSK_BOX_BORDER_COLORS_H
#define QSK_BOX_BORDER_COLORS_H #define QSK_BOX_BORDER_COLORS_H
#include "QskGradient.h"
#include "QskNamespace.h" #include "QskNamespace.h"
#include <qcolor.h> #include <qcolor.h>
@ -37,12 +38,10 @@ class QSK_EXPORT QskBoxBorderColors
const QColor& right, const QColor& bottom ); const QColor& right, const QColor& bottom );
void setColor( Qsk::Position, const QColor& ); void setColor( Qsk::Position, const QColor& );
QColor color( Qsk::Position ) const; QskGradient color( Qsk::Position ) const;
void setColorsAt( Qt::Edges, const QColor& ); void setColorsAt( Qt::Edges, const QColor& );
QColor colorAt( Qt::Edge ) const; QskGradient colorAt( Qt::Edge ) const;
QRgb rgb( Qsk::Position ) const;
QskBoxBorderColors interpolated( const QskBoxBorderColors&, qreal value ) const; QskBoxBorderColors interpolated( const QskBoxBorderColors&, qreal value ) const;
@ -55,8 +54,7 @@ class QSK_EXPORT QskBoxBorderColors
bool isVisible() const; bool isVisible() const;
private: private:
// should be stored as QRgb QskGradient m_gradients[ 4 ];
QColor m_colors[ 4 ];
}; };
inline QskBoxBorderColors::QskBoxBorderColors( Qt::GlobalColor color ) inline QskBoxBorderColors::QskBoxBorderColors( Qt::GlobalColor color )
@ -74,14 +72,9 @@ inline bool QskBoxBorderColors::operator!=( const QskBoxBorderColors& other ) co
return !( *this == other ); return !( *this == other );
} }
inline QColor QskBoxBorderColors::color( Qsk::Position position ) const inline QskGradient QskBoxBorderColors::color( Qsk::Position position ) const
{ {
return m_colors[ position ]; return m_gradients[ position ];
}
inline QRgb QskBoxBorderColors::rgb( Qsk::Position position ) const
{
return m_colors[ position ].rgba();
} }
#ifndef QT_NO_DEBUG_STREAM #ifndef QT_NO_DEBUG_STREAM

View File

@ -171,7 +171,7 @@ void QskBoxNode::setBoxData( const QRectF& rect,
} }
else else
{ {
flatMaterial->setColor( borderColors.color( Qsk::Left ).rgba() ); flatMaterial->setColor( borderColors.color( Qsk::Left ).startColor().rgba() ); // ###
renderer.renderBorder( m_rect, shape, borderMetrics, *geometry() ); renderer.renderBorder( m_rect, shape, borderMetrics, *geometry() );
} }
} }

View File

@ -859,17 +859,17 @@ static inline void qskRenderBorder( const QskBoxRenderer::Metrics& metrics,
if ( colors.isMonochrome() ) if ( colors.isMonochrome() )
{ {
qskRenderBorderLines( metrics, orientation, line, BorderMapSolid( c.rgb( Qsk::Left ) ) ); qskRenderBorderLines( metrics, orientation, line, BorderMapSolid( c.color( Qsk::Left ).startColor().rgb() ) ); // ###
} }
else else
{ {
const int stepCount = metrics.corner[ 0 ].stepCount; const int stepCount = metrics.corner[ 0 ].stepCount;
qskRenderBorderLines( metrics, orientation, line, qskRenderBorderLines( metrics, orientation, line,
BorderMapGradient( stepCount, c.rgb( Qsk::Top ), c.rgb( Qsk::Left ) ), BorderMapGradient( stepCount, c.color( Qsk::Top ).startColor().rgb(), c.color( Qsk::Left ).startColor().rgb() ), // ###
BorderMapGradient( stepCount, c.rgb( Qsk::Right ), c.rgb( Qsk::Top ) ), BorderMapGradient( stepCount, c.color( Qsk::Right ).startColor().rgb(), c.color( Qsk::Top ).startColor().rgb() ), // ###
BorderMapGradient( stepCount, c.rgb( Qsk::Left ), c.rgb( Qsk::Bottom ) ), BorderMapGradient( stepCount, c.color( Qsk::Left ).startColor().rgb(), c.color( Qsk::Bottom ).startColor().rgb() ), // ###
BorderMapGradient( stepCount, c.rgb( Qsk::Bottom ), c.rgb( Qsk::Right ) ) ); BorderMapGradient( stepCount, c.color( Qsk::Bottom ).startColor().rgb(), c.color( Qsk::Right ).startColor().rgb() ) ); // ###
} }
} }
@ -899,7 +899,7 @@ static inline void qskRenderBoxRandom(
if ( bc.isMonochrome() ) if ( bc.isMonochrome() )
{ {
const BorderMapSolid borderMap( bc.rgb( Qsk::Left ) ); const BorderMapSolid borderMap( bc.color( Qsk::Left ).startColor().rgb() ); // ###
if ( gradient.isMonochrome() ) if ( gradient.isMonochrome() )
{ {
@ -918,10 +918,10 @@ static inline void qskRenderBoxRandom(
{ {
const int n = metrics.corner[ 0 ].stepCount; const int n = metrics.corner[ 0 ].stepCount;
const BorderMapGradient tl( n, bc.rgb( Qsk::Top ), bc.rgb( Qsk::Left ) ); const BorderMapGradient tl( n, bc.color( Qsk::Top ).startColor().rgb(), bc.color( Qsk::Left ).startColor().rgb() );
const BorderMapGradient tr( n, bc.rgb( Qsk::Right ), bc.rgb( Qsk::Top ) ); const BorderMapGradient tr( n, bc.color( Qsk::Right ).startColor().rgb(), bc.color( Qsk::Top ).startColor().rgb() );
const BorderMapGradient bl( n, bc.rgb( Qsk::Left ), bc.rgb( Qsk::Bottom ) ); const BorderMapGradient bl( n, bc.color( Qsk::Left ).startColor().rgb(), bc.color( Qsk::Bottom ).startColor().rgb() );
const BorderMapGradient br( n, bc.rgb( Qsk::Bottom ), bc.rgb( Qsk::Right ) ); const BorderMapGradient br( n, bc.color( Qsk::Bottom ).startColor().rgb(), bc.color( Qsk::Right ).startColor().rgb() );
if ( gradient.isMonochrome() ) if ( gradient.isMonochrome() )
{ {

View File

@ -422,10 +422,10 @@ static inline void qskCreateBorder(
const QskBoxRenderer::Quad& out, const QskBoxRenderer::Quad& in, const QskBoxRenderer::Quad& out, const QskBoxRenderer::Quad& in,
const QskBoxBorderColors& colors, Line* line ) const QskBoxBorderColors& colors, Line* line )
{ {
const Color colorLeft = colors.rgb( Qsk::Left ); const Color colorLeft = colors.color( Qsk::Left ).startColor().rgb(); // ###
const Color colorRight = colors.rgb( Qsk::Right ); const Color colorRight = colors.color( Qsk::Right ).startColor().rgb(); // ###
const Color colorTop = colors.rgb( Qsk::Top ); const Color colorTop = colors.color( Qsk::Top ).startColor().rgb(); // ###
const Color colorBottom = colors.rgb( Qsk::Bottom ); const Color colorBottom = colors.color( Qsk::Bottom ).startColor().rgb(); // ###
( line++ )->setLine( in.right, in.bottom, out.right, out.bottom, colorBottom ); ( line++ )->setLine( in.right, in.bottom, out.right, out.bottom, colorBottom );
( line++ )->setLine( in.left, in.bottom, out.left, out.bottom, colorBottom ); ( line++ )->setLine( in.left, in.bottom, out.left, out.bottom, colorBottom );
@ -587,7 +587,7 @@ void QskBoxRenderer::renderRect(
auto fillLines = line + fillLineCount; auto fillLines = line + fillLineCount;
if ( bc.isMonochrome() ) if ( bc.isMonochrome() )
qskCreateBorderMonochrome( rect, in, bc.rgb( Qsk::Left ), fillLines ); qskCreateBorderMonochrome( rect, in, bc.color( Qsk::Left ).startColor().rgb(), fillLines );
else else
qskCreateBorder( rect, in, bc, fillLines ); qskCreateBorder( rect, in, bc, fillLines );
} }