aboutsummaryrefslogtreecommitdiffstats
path: root/src/quickcontrols2impl
diff options
context:
space:
mode:
authorJiDe Zhang <zhangjide@uniontech.com>2021-10-28 11:45:52 +0800
committerJiDe Zhang <zhangjide@uniontech.com>2021-10-29 18:59:48 +0800
commit85b5413dc43e20951a3f27b2940961b1c998df95 (patch)
treeb131e0f53937f0db19f1d4a06e86d7810d5ade5c /src/quickcontrols2impl
parent004e90b15569ab204e8302df853ef66eba918131 (diff)
Avoid unnecessary color format conversion
If a color is not the rgb format, when QColor::red() QColor::blue() QColor::green() is used continuously to obtain the values of different channels, three times color conversions will occur. Therefore, use QColor::toRgb() before that to ensure that only one conversion is performed at most. Not only rgb, the conversion of other formats is the same. Change-Id: Ia969e1ca6f1524ad5d7e8dec915bcbc407875c66 Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Diffstat (limited to 'src/quickcontrols2impl')
-rw-r--r--src/quickcontrols2impl/qquickcolor.cpp11
1 files changed, 7 insertions, 4 deletions
diff --git a/src/quickcontrols2impl/qquickcolor.cpp b/src/quickcontrols2impl/qquickcolor.cpp
index 8ae568fc1c..9d67b5c032 100644
--- a/src/quickcontrols2impl/qquickcolor.cpp
+++ b/src/quickcontrols2impl/qquickcolor.cpp
@@ -45,7 +45,8 @@ QQuickColor::QQuickColor(QObject *parent) :
QColor QQuickColor::transparent(const QColor &color, qreal opacity) const
{
- return QColor(color.red(), color.green(), color.blue(),
+ const auto rgbColor = color.toRgb();
+ return QColor(rgbColor.red(), rgbColor.green(), rgbColor.blue(),
int(qreal(255) * qBound(qreal(0), opacity, qreal(1))));
}
@@ -56,10 +57,12 @@ QColor QQuickColor::blend(const QColor &a, const QColor &b, qreal factor) const
if (factor >= 1.0)
return b;
+ const auto rgbA = a.toRgb();
+ const auto rgbB = b.toRgb();
QColor color;
- color.setRedF(a.redF() * (1.0 - factor) + b.redF() * factor);
- color.setGreenF(a.greenF() * (1.0 - factor) + b.greenF() * factor);
- color.setBlueF(a.blueF() * (1.0 - factor) + b.blueF() * factor);
+ color.setRedF(rgbA.redF() * (1.0 - factor) + rgbB.redF() * factor);
+ color.setGreenF(rgbA.greenF() * (1.0 - factor) + rgbB.greenF() * factor);
+ color.setBlueF(rgbA.blueF() * (1.0 - factor) + rgbB.blueF() * factor);
return color;
}