change gradient to two colors

This commit is contained in:
Vogel, Rick 2024-01-29 14:43:25 +01:00
parent e92458419f
commit b511ed16c3
1 changed files with 70 additions and 55 deletions

View File

@ -3,21 +3,21 @@
* SPDX-License-Identifier: BSD-3-Clause * SPDX-License-Identifier: BSD-3-Clause
*****************************************************************************/ *****************************************************************************/
#include <QskObjectCounter.h>
#include <QskTabBar.h>
#include <QskTabView.h>
#include <QskWindow.h>
#include <QskSkinlet.h>
#include <QskBoxNode.h>
#include <QskTextNode.h>
#include <QskSGNode.h>
#include <QskBoxShadowNode.h>
#include <QskBoxBorderColors.h> #include <QskBoxBorderColors.h>
#include <QskBoxBorderMetrics.h> #include <QskBoxBorderMetrics.h>
#include <QskGradientDirection.h> #include <QskBoxNode.h>
#include <QskBoxShadowNode.h>
#include <QskBoxShapeMetrics.h> #include <QskBoxShapeMetrics.h>
#include <QskGradientDirection.h>
#include <QskLinearBox.h> #include <QskLinearBox.h>
#include <QskObjectCounter.h>
#include <QskPushButton.h> #include <QskPushButton.h>
#include <QskSGNode.h>
#include <QskSkinlet.h>
#include <QskTabBar.h>
#include <QskTabView.h>
#include <QskTextNode.h>
#include <QskWindow.h>
#include <QGuiApplication> #include <QGuiApplication>
#include <SkinnyShortcut.h> #include <SkinnyShortcut.h>
@ -31,17 +31,17 @@ double qskMapValueRange( double value, double srcMin, double srcMax, double dstM
return mappedValue; return mappedValue;
} }
QColor extracted( const QskGradient& gradient, qreal ratio ) QColor extracted( const QColor& from, const QColor& to, qreal ratio )
{ {
// Ensure the factor is within the [0, 1] range // Ensure the factor is within the [0, 1] range
ratio = qBound< qreal >( 0.0, ratio, 1.0 ); ratio = qBound< qreal >( 0.0, ratio, 1.0 );
// Extract RGB components of the start and end colors // Extract RGB components of the start and end colors
int startRed, startGreen, startBlue, startAlpha; int startRed, startGreen, startBlue, startAlpha;
gradient.startColor().getRgb( &startRed, &startGreen, &startBlue, &startAlpha ); from.getRgb( &startRed, &startGreen, &startBlue, &startAlpha );
int endRed, endGreen, endBlue, endAlpha; int endRed, endGreen, endBlue, endAlpha;
gradient.endColor().getRgb( &endRed, &endGreen, &endBlue, &endAlpha ); to.getRgb( &endRed, &endGreen, &endBlue, &endAlpha );
// Linearly interpolate each color component // Linearly interpolate each color component
int interpolatedRed = static_cast< int >( startRed + ratio * ( endRed - startRed ) ); int interpolatedRed = static_cast< int >( startRed + ratio * ( endRed - startRed ) );
@ -58,9 +58,14 @@ QskGradient extracted( const QskGradient& gradient, qreal from, qreal to )
const auto stops = gradient.stops(); const auto stops = gradient.stops();
if ( stops.count() == 0 ) if ( stops.count() == 0 )
{
return {}; return {};
}
if ( stops.count() == 1 ) if ( stops.count() == 1 )
{
return stops[ 0 ].color(); return stops[ 0 ].color();
}
from = qBound( 0.0, from, 1.0 ); from = qBound( 0.0, from, 1.0 );
to = qBound( 0.0, to, 1.0 ); to = qBound( 0.0, to, 1.0 );
@ -94,14 +99,13 @@ QskGradient extracted( const QskGradient& gradient, qreal from, qreal to )
const auto fromColor = [ & ]() { const auto fromColor = [ & ]() {
const auto p = qskMapValueRange( from, stops[ fromIndex.first ].position(), const auto p = qskMapValueRange( from, stops[ fromIndex.first ].position(),
stops[ fromIndex.second ].position(), 0.0, 1.0 ); stops[ fromIndex.second ].position(), 0.0, 1.0 );
return extracted( return extracted( stops[ fromIndex.first ].color(), stops[ fromIndex.second ].color(), p );
{ stops[ fromIndex.first ].color(), stops[ fromIndex.second ].color() }, p );
}(); }();
const auto toColor = [ & ]() { const auto toColor = [ & ]() {
const auto p = qskMapValueRange( const auto p = qskMapValueRange(
to, stops[ toIndex.first ].position(), stops[ toIndex.second ].position(), 0.0, 1.0 ); to, stops[ toIndex.first ].position(), stops[ toIndex.second ].position(), 0.0, 1.0 );
return extracted( { stops[ toIndex.first ].color(), stops[ toIndex.second ].color() }, p ); return extracted( stops[ toIndex.first ].color(), stops[ toIndex.second ].color(), p );
}(); }();
QskGradient newGradient; QskGradient newGradient;
@ -257,46 +261,57 @@ int main( int argc, char* argv[] )
auto* const button = new QskPushButton( "Click", row ); auto* const button = new QskPushButton( "Click", row );
button->setGradientHint( QskPushButton::Panel, {} ); button->setGradientHint( QskPushButton::Panel, {} );
QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() {
control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); control->setGradientHint(
Control::Gradient, button->gradientHint( QskPushButton::Panel ) );
} ); } );
} }
{ {
auto* const button = new QskPushButton( "Click", row ); auto* const button = new QskPushButton( "Click", row );
button->setGradientHint( QskPushButton::Panel, Qt::red ); button->setGradientHint( QskPushButton::Panel, Qt::red );
QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() {
control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); control->setGradientHint(
Control::Gradient, button->gradientHint( QskPushButton::Panel ) );
} ); } );
} }
{ {
auto* const button = new QskPushButton( "Click", row ); auto* const button = new QskPushButton( "Click", row );
button->setGradientHint(QskPushButton::Panel, qskGradient(Qt::Horizontal, {Qt::red, Qt::green})); button->setGradientHint(
QskPushButton::Panel, qskGradient( Qt::Horizontal, { Qt::red, Qt::green } ) );
QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() {
control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); control->setGradientHint(
Control::Gradient, button->gradientHint( QskPushButton::Panel ) );
} ); } );
} }
{ {
auto* const button = new QskPushButton( "Click", row ); auto* const button = new QskPushButton( "Click", row );
button->setGradientHint(QskPushButton::Panel, qskGradient(Qt::Horizontal,{{{0.0, Qt::red}, {0.5, Qt::green}, {1.0, Qt::blue}}})); button->setGradientHint( QskPushButton::Panel,
qskGradient(
Qt::Horizontal, { { { 0.0, Qt::red }, { 0.5, Qt::green }, { 1.0, Qt::blue } } } ) );
QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() {
control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); control->setGradientHint(
Control::Gradient, button->gradientHint( QskPushButton::Panel ) );
} ); } );
} }
{ {
auto* const button = new QskPushButton( "Click", row ); auto* const button = new QskPushButton( "Click", row );
button->setGradientHint(QskPushButton::Panel, qskGradient(Qt::Horizontal,{{{0.0, Qt::red}, {0.5, Qt::red},{0.5, Qt::blue}, {1.0, Qt::blue}}})); button->setGradientHint( QskPushButton::Panel,
qskGradient( Qt::Horizontal, { { { 0.0, Qt::red }, { 0.5, Qt::red }, { 0.5, Qt::blue },
{ 1.0, Qt::blue } } } ) );
QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() {
control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); control->setGradientHint(
Control::Gradient, button->gradientHint( QskPushButton::Panel ) );
} ); } );
} }
{ {
auto* const button = new QskPushButton( "Click", row ); auto* const button = new QskPushButton( "Click", row );
button->setGradientHint(QskPushButton::Panel, qskGradient(Qt::Horizontal,{{ button->setGradientHint(
{0.0, Qt::red}, {0.25, Qt::red}, QskPushButton::Panel, qskGradient( Qt::Horizontal,
{0.25, Qt::green}, {0.5, Qt::green}, { { { 0.0, Qt::red }, { 0.25, Qt::red }, { 0.25, Qt::green },
{0.5, Qt::blue}, {0.75, Qt::blue}, { 0.5, Qt::green }, { 0.5, Qt::blue }, { 0.75, Qt::blue },
{ 0.75, Qt::yellow }, { 1.0, Qt::yellow } } } ) ); { 0.75, Qt::yellow }, { 1.0, Qt::yellow } } } ) );
QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() {
control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); control->setGradientHint(
Control::Gradient, button->gradientHint( QskPushButton::Panel ) );
} ); } );
} }