6#include "QskRgbValue.h"
8#include <qeasingcurve.h>
12#include <private/qdrawhelper_p.h>
17 inline int value(
int from,
int to, qreal ratio )
19 return int( from + ( to - from ) * ratio );
22 inline qreal valueF( qreal from, qreal to, qreal ratio )
24 return int( from + ( to - from ) * ratio );
28static inline QColor qskInterpolatedColor(
29 const QColor& c1,
const QColor& c2, qreal ratio )
35 const int r = value( c1.red(), c2.red(), ratio );
36 const int g = value( c1.green(), c2.green(), ratio );
37 const int b = value( c1.blue(), c2.blue(), ratio );
38 const int a = value( c1.alpha(), c2.alpha(), ratio );
40 return QColor::fromRgb( r, g, b, a );
44 const int h = value( c1.hue(), c2.hue(), ratio );
45 const int s = value( c1.saturation(), c2.saturation(), ratio );
46 const int v = value( c1.value(), c2.value(), ratio );
47 const int a = value( c1.alpha(), c2.alpha(), ratio );
49 return QColor::fromHsv( h, s, v, a );
53 const int c = value( c1.cyan(), c2.cyan(), ratio );
54 const int m = value( c1.magenta(), c2.magenta(), ratio );
55 const int y = value( c1.yellow(), c2.yellow(), ratio );
56 const int k = value( c1.black(), c2.black(), ratio );
57 const int a = value( c1.alpha(), c2.alpha(), ratio );
59 return QColor::fromCmykF( c, m, y, k, a );
63 const int h = value( c1.hue(), c2.hue(), ratio );
64 const int s = value( c1.saturation(), c2.saturation(), ratio );
65 const int l = value( c1.lightness(), c2.lightness(), ratio );
66 const int a = value( c1.alpha(), c2.alpha(), ratio );
68 return QColor::fromHsl( h, s, l, a );
70 case QColor::ExtendedRgb:
72 const qreal r = valueF( c1.redF(), c2.redF(), ratio );
73 const qreal g = valueF( c1.greenF(), c2.greenF(), ratio );
74 const qreal b = valueF( c1.blueF(), c2.blueF(), ratio );
75 const qreal a = valueF( c1.alphaF(), c2.alphaF(), ratio );
77 return QColor::fromRgbF( r, g, b, a );
86QRgb QskRgb::interpolated( QRgb rgb1, QRgb rgb2, qreal ratio )
93 const int r = value( qRed( rgb1 ), qRed( rgb2 ), ratio );
94 const int g = value( qGreen( rgb1 ), qGreen( rgb2 ), ratio );
95 const int b = value( qBlue( rgb1 ), qBlue( rgb2 ), ratio );
96 const int a = value( qAlpha( rgb1 ), qAlpha( rgb2 ), ratio );
98 return qRgba( r, g, b, a );
101QColor QskRgb::interpolated(
const QColor& c1,
const QColor& c2, qreal ratio )
114 c.setAlpha( ratio * c2.alpha() );
121 c.setAlpha( ( 1.0 - ratio ) * c1.alpha() );
125 if ( c1.spec() == c2.spec() )
126 return qskInterpolatedColor( c1, c2, ratio );
128 return qskInterpolatedColor( c1.convertTo( c2.spec() ), c2, ratio );
131QRgb QskRgb::rgb( Qt::GlobalColor color )
133 using namespace QskRgb;
135 static constexpr QRgb rgbValues[] =
142 qRgb( 160, 160, 164 ),
159 return rgbValues[ color ];
162QRgb QskRgb::lighter( QRgb rgb,
int factor )
noexcept
168 return QColor::fromRgba( rgb ).lighter( factor ).rgba();
171QRgb QskRgb::darker( QRgb rgb,
int factor )
noexcept
177 return QColor::fromRgba( rgb ).darker( factor ).rgba();
180#ifndef QT_NO_DEBUG_STREAM
184void QskRgb::debugColor( QDebug debug,
const QColor& color )
186 debugColor( debug, color.rgba() );
189void QskRgb::debugColor( QDebug debug, QRgb rgb )
191 QDebugStateSaver saver( debug );
196 debug << qRed( rgb ) <<
"r,"
197 << qGreen( rgb ) <<
"g," << qBlue( rgb ) <<
'b';
199 if ( qAlpha( rgb ) != 255 )
200 debug <<
',' << qAlpha( rgb ) <<
'a';
207QImage QskRgb::colorTable(
int size,
const QskGradientStops& stops )
209 if ( size == 0 || stops.isEmpty() )
212 QImage image( size, 1, QImage::Format_RGBA8888_Premultiplied );
214 if ( stops.size() == 1 )
216 const auto rgb = ARGB2RGBA( qPremultiply( stops[0].rgb() ) );
222 auto values =
reinterpret_cast< uint*
>( image.bits() );
227 index1 = index2 = qRound( stops[0].position() * size );
228 rgb1 = rgb2 = qPremultiply( stops[0].rgb() );
232 const auto v = ARGB2RGBA( rgb1 );
234 for (
int i = 0; i < index1; i++ )
238 for (
int i = 1; i < stops.count(); i++ )
240 const auto& stop = stops[i];
242 index2 = qRound( stop.position() * size );
243 rgb2 = qPremultiply( stop.rgb() );
245 const auto n = index2 - index1;
247 values[ index1 ] = ARGB2RGBA( rgb1 );
248 for (
int j = 1; j < n; j++ )
250 const auto rgb = QskRgb::interpolated( rgb1, rgb2, qreal( j ) / ( n - 1 ) );
251 values[ index1 + j] = ARGB2RGBA( rgb );
258 if ( index1 < size - 1 )
260 const auto v = ARGB2RGBA( rgb1 );
262 for (
int i = index1; i < size ; i++ )
269QImage QskRgb::colorTable(
const int size,
270 QRgb rgb1, QRgb rgb2,
const QEasingCurve& curve )
275 rgb1 = qPremultiply( rgb1 );
276 rgb2 = qPremultiply( rgb2 );
278 QImage image( size, 1, QImage::Format_RGBA8888_Premultiplied );
282 image.fill( ARGB2RGBA( rgb1 ) );
286 auto values =
reinterpret_cast< uint*
>( image.bits() );
288 for (
int i = 0; i < size; i++ )
290 qreal progress = curve.valueForProgress( qreal( i ) / ( size - 1 ) );
291 progress = qBound( 0.0, progress, 1.0 );
293 auto rgb = QskRgb::interpolated( rgb1, rgb2, progress );
294 values[i] = ARGB2RGBA( rgb );