QskRgbPalette::colorStops added

This commit is contained in:
Uwe Rathmann 2020-07-31 13:26:22 +02:00
parent 39f4d80153
commit 9c55e03859
3 changed files with 43 additions and 19 deletions

View File

@ -8,6 +8,7 @@
#include <QskBoxBorderColors.h> #include <QskBoxBorderColors.h>
#include <QskBoxBorderMetrics.h> #include <QskBoxBorderMetrics.h>
#include <QskBoxShapeMetrics.h> #include <QskBoxShapeMetrics.h>
#include <QskRgbPalette.h>
Box::Box( QQuickItem* parentItem ) Box::Box( QQuickItem* parentItem )
: QskBox( parentItem ) : QskBox( parentItem )
@ -180,23 +181,5 @@ void Box::setGradient(
const QskGradient::Orientation orientation, QskRgbPalette::Theme theme ) const QskGradient::Orientation orientation, QskRgbPalette::Theme theme )
{ {
const auto pal = QskRgbPalette::palette( theme ); const auto pal = QskRgbPalette::palette( theme );
setGradient( QskGradient( orientation, pal.colorStops( true ) ) );
QVector< QskGradientStop > stops;
stops += QskGradientStop( 0.0, pal.color( static_cast< QskRgbPalette::Weight >( 0 ) ) );
const int count = QskRgbPalette::NumWeights - 1;
for ( int i = 1; i < count; i++ )
{
const qreal pos = qreal( i ) / count;
const auto weight = static_cast< QskRgbPalette::Weight >( i );
stops += QskGradientStop( pos, stops.last().color() );
stops += QskGradientStop( pos, pal.color( weight ) );
}
stops += QskGradientStop( 1.0,
pal.color( static_cast< QskRgbPalette::Weight >( QskRgbPalette::NumWeights - 1 ) ) );
setGradient( QskGradient( orientation, stops ) );
} }

View File

@ -5,6 +5,7 @@
#include "QskRgbPalette.h" #include "QskRgbPalette.h"
#include "QskRgbValue.h" #include "QskRgbValue.h"
#include "QskGradient.h"
#define RGB( color, weight ) color ## weight #define RGB( color, weight ) color ## weight
@ -56,3 +57,38 @@ QskRgbPalette QskRgbPalette::palette( Theme theme )
{ {
return Palette( static_cast< int >( theme ) ); return Palette( static_cast< int >( theme ) );
} }
QVector< QskGradientStop > QskRgbPalette::colorStops( bool discrete ) const
{
QVector< QskGradientStop > stops;
if ( discrete )
stops.reserve( 2 * QskRgbPalette::NumWeights - 2 );
else
stops.reserve( QskRgbPalette::NumWeights );
stops += QskGradientStop( 0.0, m_rgb[0] );
if ( discrete )
{
constexpr auto step = 1.0 / NumWeights;
for ( int i = 1; i < NumWeights; i++ )
{
const qreal pos = i * step;
stops += QskGradientStop( pos, m_rgb[i-1] );
stops += QskGradientStop( pos, m_rgb[i] );
}
}
else
{
constexpr auto step = 1.0 / ( NumWeights - 1 );
for ( int i = 1; i < NumWeights - 1; i++ )
stops += QskGradientStop( i * step, m_rgb[i] );
}
stops += QskGradientStop( 1.0, m_rgb[NumWeights - 1] );
return stops;
}

View File

@ -10,6 +10,9 @@
#include <qmetaobject.h> #include <qmetaobject.h>
#include <qcolor.h> #include <qcolor.h>
class QskGradientStop;
template< typename T > class QVector;
class QSK_EXPORT QskRgbPalette class QSK_EXPORT QskRgbPalette
{ {
Q_GADGET Q_GADGET
@ -71,6 +74,8 @@ class QSK_EXPORT QskRgbPalette
return QColor::fromRgba( rgb( weight ) ); return QColor::fromRgba( rgb( weight ) );
} }
QVector< QskGradientStop > colorStops( bool discrete = false ) const;
static QskRgbPalette palette( Theme ); static QskRgbPalette palette( Theme );
protected: protected: