From b511ed16c36468fe429c36939378771d556449f0 Mon Sep 17 00:00:00 2001 From: "Vogel, Rick" Date: Mon, 29 Jan 2024 14:43:25 +0100 Subject: [PATCH] change gradient to two colors --- playground/gradient_fix/main.cpp | 125 +++++++++++++++++-------------- 1 file changed, 70 insertions(+), 55 deletions(-) diff --git a/playground/gradient_fix/main.cpp b/playground/gradient_fix/main.cpp index 88ce832d..199eca8b 100644 --- a/playground/gradient_fix/main.cpp +++ b/playground/gradient_fix/main.cpp @@ -3,21 +3,21 @@ * SPDX-License-Identifier: BSD-3-Clause *****************************************************************************/ -#include -#include -#include -#include -#include -#include -#include -#include -#include #include #include -#include +#include +#include #include +#include #include +#include #include +#include +#include +#include +#include +#include +#include #include #include @@ -31,17 +31,17 @@ double qskMapValueRange( double value, double srcMin, double srcMax, double dstM 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 ratio = qBound< qreal >( 0.0, ratio, 1.0 ); // Extract RGB components of the start and end colors int startRed, startGreen, startBlue, startAlpha; - gradient.startColor().getRgb( &startRed, &startGreen, &startBlue, &startAlpha ); + from.getRgb( &startRed, &startGreen, &startBlue, &startAlpha ); int endRed, endGreen, endBlue, endAlpha; - gradient.endColor().getRgb( &endRed, &endGreen, &endBlue, &endAlpha ); + to.getRgb( &endRed, &endGreen, &endBlue, &endAlpha ); // Linearly interpolate each color component 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(); if ( stops.count() == 0 ) + { return {}; + } + if ( stops.count() == 1 ) + { return stops[ 0 ].color(); + } from = qBound( 0.0, from, 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 p = qskMapValueRange( from, stops[ fromIndex.first ].position(), stops[ fromIndex.second ].position(), 0.0, 1.0 ); - return extracted( - { stops[ fromIndex.first ].color(), stops[ fromIndex.second ].color() }, p ); + return extracted( stops[ fromIndex.first ].color(), stops[ fromIndex.second ].color(), p ); }(); const auto toColor = [ & ]() { const auto p = qskMapValueRange( 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; @@ -241,63 +245,74 @@ int main( int argc, char* argv[] ) SkinnyShortcut::enable( SkinnyShortcut::AllShortcuts ); - auto* const layout = new QskLinearBox(Qt::Vertical); - auto* const row = new QskLinearBox(Qt::Vertical, layout); - auto* const control = new Control(layout); + auto* const layout = new QskLinearBox( Qt::Vertical ); + auto* const row = new QskLinearBox( Qt::Vertical, layout ); + auto* const control = new Control( layout ); auto* const skinlet = new Skinlet; control->setSkinlet( skinlet ); skinlet->setOwnedBySkinnable( true ); - auto qskGradient = [](Qt::Orientation orientation , QskGradient gradient ){ - gradient.setLinearDirection(orientation); + auto qskGradient = []( Qt::Orientation orientation, QskGradient gradient ) { + gradient.setLinearDirection( orientation ); return gradient; }; { - auto* const button = new QskPushButton( "Click",row ); - button->setGradientHint(QskPushButton::Panel, {}); - QObject::connect(button, &QskPushButton::clicked, control, [control, button](){ - control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); - }); + auto* const button = new QskPushButton( "Click", row ); + button->setGradientHint( QskPushButton::Panel, {} ); + QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { + control->setGradientHint( + Control::Gradient, button->gradientHint( QskPushButton::Panel ) ); + } ); } { - auto* const button = new QskPushButton( "Click",row ); - button->setGradientHint(QskPushButton::Panel, Qt::red); - QObject::connect(button, &QskPushButton::clicked, control, [control, button](){ - control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); - }); + auto* const button = new QskPushButton( "Click", row ); + button->setGradientHint( QskPushButton::Panel, Qt::red ); + QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { + control->setGradientHint( + Control::Gradient, button->gradientHint( QskPushButton::Panel ) ); + } ); } { - auto* const button = new QskPushButton("Click", row ); - button->setGradientHint(QskPushButton::Panel, qskGradient(Qt::Horizontal, {Qt::red, Qt::green})); - QObject::connect(button, &QskPushButton::clicked, control, [control, button](){ - control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); - }); + auto* const button = new QskPushButton( "Click", row ); + button->setGradientHint( + QskPushButton::Panel, qskGradient( Qt::Horizontal, { Qt::red, Qt::green } ) ); + QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { + control->setGradientHint( + Control::Gradient, button->gradientHint( QskPushButton::Panel ) ); + } ); } { - 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}}})); - QObject::connect(button, &QskPushButton::clicked, control, [control, button](){ - control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); - }); + 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 } } } ) ); + QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { + control->setGradientHint( + Control::Gradient, button->gradientHint( QskPushButton::Panel ) ); + } ); } { - 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}}})); - QObject::connect(button, &QskPushButton::clicked, control, [control, button](){ - control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); - }); + 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 } } } ) ); + QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { + control->setGradientHint( + Control::Gradient, button->gradientHint( QskPushButton::Panel ) ); + } ); } { - auto* const button = new QskPushButton( "Click",row ); - button->setGradientHint(QskPushButton::Panel, qskGradient(Qt::Horizontal,{{ - {0.0, Qt::red}, {0.25, Qt::red}, - {0.25, Qt::green}, {0.5, Qt::green}, - {0.5, Qt::blue}, {0.75, Qt::blue}, - {0.75, Qt::yellow}, {1.0, Qt::yellow}}})); - QObject::connect(button, &QskPushButton::clicked, control, [control, button](){ - control->setGradientHint(Control::Gradient, button->gradientHint(QskPushButton::Panel)); - }); + auto* const button = new QskPushButton( "Click", row ); + button->setGradientHint( + QskPushButton::Panel, qskGradient( Qt::Horizontal, + { { { 0.0, Qt::red }, { 0.25, Qt::red }, { 0.25, Qt::green }, + { 0.5, Qt::green }, { 0.5, Qt::blue }, { 0.75, Qt::blue }, + { 0.75, Qt::yellow }, { 1.0, Qt::yellow } } } ) ); + QObject::connect( button, &QskPushButton::clicked, control, [ control, button ]() { + control->setGradientHint( + Control::Gradient, button->gradientHint( QskPushButton::Panel ) ); + } ); } QskWindow window;