diff --git a/examples/boxes/Box.cpp b/examples/boxes/Box.cpp index 19334090..f3cbd8fd 100644 --- a/examples/boxes/Box.cpp +++ b/examples/boxes/Box.cpp @@ -8,6 +8,7 @@ #include #include #include +#include Box::Box( QQuickItem* parentItem ) : QskBox( parentItem ) @@ -180,23 +181,5 @@ void Box::setGradient( const QskGradient::Orientation orientation, QskRgbPalette::Theme theme ) { const auto pal = QskRgbPalette::palette( theme ); - - 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 ) ); + setGradient( QskGradient( orientation, pal.colorStops( true ) ) ); } diff --git a/src/common/QskRgbPalette.cpp b/src/common/QskRgbPalette.cpp index 1cad4d03..13fc2a8b 100644 --- a/src/common/QskRgbPalette.cpp +++ b/src/common/QskRgbPalette.cpp @@ -5,6 +5,7 @@ #include "QskRgbPalette.h" #include "QskRgbValue.h" +#include "QskGradient.h" #define RGB( color, weight ) color ## weight @@ -56,3 +57,38 @@ QskRgbPalette QskRgbPalette::palette( Theme 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; +} diff --git a/src/common/QskRgbPalette.h b/src/common/QskRgbPalette.h index bcf6910a..46ff83f1 100644 --- a/src/common/QskRgbPalette.h +++ b/src/common/QskRgbPalette.h @@ -10,6 +10,9 @@ #include #include +class QskGradientStop; +template< typename T > class QVector; + class QSK_EXPORT QskRgbPalette { Q_GADGET @@ -71,6 +74,8 @@ class QSK_EXPORT QskRgbPalette return QColor::fromRgba( rgb( weight ) ); } + QVector< QskGradientStop > colorStops( bool discrete = false ) const; + static QskRgbPalette palette( Theme ); protected: