Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickcolor.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
6
7#include <private/qqmlcontextdata_p.h>
8#include <private/qquickvaluetypes_p.h>
9#include <private/qv4engine_p.h>
10
12
13/*!
14 * \qmltype Color
15 * \nativetype QQuickColor
16 * \inqmlmodule QtQuick
17 * \brief Holds utility functions for colors.
18 * \since 6.12
19 */
20QQuickColor::QQuickColor(QObject *parent) : QObject(parent)
21{
22}
23
24/*!
25 * \qmlmethod color Color::rgba(real red, real green, real blue, real alpha)
26 *
27 * Returns a color with the specified \a red, \a green, \a blue, and \a alpha
28 * components. All components should be in the range 0-1 (inclusive).
29 */
30QColor QQuickColor::rgba(double r, double g, double b, double a) const
31{
32 return QColor::fromRgbF(r, g, b, a);
33}
34
35/*!
36 * \qmlmethod color Color::hsla(real hue, real saturation, real lightness, real alpha)
37 *
38 * Returns a color with the specified \a hue, \a saturation, \a lightness, and \a alpha
39 * components. All components should be in the range 0-1 (inclusive).
40 */
41QColor QQuickColor::hsla(double h, double s, double l, double a) const
42{
43 return QColor::fromHslF(h, s, l, a);
44}
45
46/*!
47 * \qmlmethod color Color::hsva(real hue, real saturation, real value, real alpha)
48 *
49 * Returns a color with the specified \a hue, \a saturation, \a value and \a alpha
50 * components. All components should be in the range 0-1 (inclusive).
51 */
52QColor QQuickColor::hsva(double h, double s, double v, double a) const
53{
54 return QColor::fromHsvF(h, s, v, a);
55}
56
57/*!
58 * \qmlmethod color Color::fromString(string name)
59 *
60 * Returns the color corresponding to the given \a name (i.e. red or #ff0000).
61 * If there is no such color, an exception is thrown.
62 */
63QColor QQuickColor::fromString(const QString &name) const
64{
65 QColor c = QColor::fromString(name);
66 if (!c.isValid()) {
67 const auto *qmlContext = QQmlData::get(this)->outerContext;
68 Q_ASSERT(qmlContext);
69 if (qmlContext)
70 qmlContext->engine()->throwError(
71 QStringLiteral("\"%1\" is not a valid color name").arg(name));
72 return {};
73 }
74
75 return c;
76}
77
78/*!
79 * \qmlmethod bool Color::equal(color lhs, color rhs)
80 *
81 * Returns true if \a lhs and \a rhs are equal.
82 * See \l{QColor::}{operator==()}.
83 *
84 * \note This function is a typed version of \l{Qt::colorEqual()}
85 */
86bool QQuickColor::equal(const QColor &lhs, const QColor &rhs) const
87{
88 return lhs == rhs;
89}
90
91/*!
92 * \qmlmethod color Color::transparent(color color, real opacity)
93 *
94 * Returns \a color with an alpha value of \a opacity, which ranges from 0
95 * (completely transparent) to 1 (completely opaque).
96 */
97QColor QQuickColor::transparent(const QColor &color, qreal opacity) const
98{
99 QColor newColor = color;
100 newColor.setAlphaF(opacity);
101 return newColor;
102}
103
104/*!
105 * \qmlmethod color Color::blend(color a, color b, real factor)
106 *
107 * Returns a color that is a blend of \a a and \a b. The resulting color is constructed from the
108 * independent linear interpolations of the RGB color channels of \a a and \a b.
109 *
110 * If \a factor is smaller than 0.0, \a a is returned. If factor is larger than 1.0, \a b is
111 * returned.
112 */
113QColor QQuickColor::blend(const QColor &a, const QColor &b, qreal factor) const
114{
115 if (std::isnan(factor)) {
116 qWarning("NaN given as argument to Color.blend()");
117 return QColor();
118 }
119
120 if (factor <= 0.0)
121 return a;
122 if (factor >= 1.0)
123 return b;
124
125 const auto rgbA = a.toRgb();
126 const auto rgbB = b.toRgb();
127 QColor color;
128 color.setRedF(rgbA.redF() * (1.0 - factor) + rgbB.redF() * factor);
129 color.setGreenF(rgbA.greenF() * (1.0 - factor) + rgbB.greenF() * factor);
130 color.setBlueF(rgbA.blueF() * (1.0 - factor) + rgbB.blueF() * factor);
131 return color;
132}
133
134/*!
135 * \qmlmethod color Color::darker(color baseColor, real factor)
136 *
137 * Returns a color darker than \a baseColor by the \a factor provided.
138 *
139 * The function converts the current RGB color to HSV, divides the value (V) component
140 * by factor and converts the color back to RGB.
141 *
142 * If the factor is greater than 1.0, this function returns a darker color. Setting factor to 3.0
143 * returns a color that has one-third the brightness. If the factor is less than 1.0, the return
144 * color is lighter, but we recommend using the \l{Color::lighter()} function for this purpose. If
145 * the factor is 0 or negative, the return value is unspecified.
146 */
147QColor QQuickColor::darker(const QColor &baseColor, double factor) const
148{
149 return QQuickColorValueType(baseColor).darker(factor);
150}
151
152/*!
153 * \qmlmethod color Color::lighter(color baseColor, real factor)
154 *
155 * Returns a color lighter than \a baseColor by the \a factor provided.
156 *
157 * The function converts the current RGB color to HSV, multiplies the value (V) component
158 * by factor and converts the color back to RGB.
159 *
160 * If the factor is greater than 1.0, this functions returns a lighter color. Setting factor to 1.5
161 * returns a color that is 50% brighter. If the factor is less than 1.0, the return color is
162 * darker, but we recommend using the \l{Color::darker()} function for this purpose. If the factor
163 * is 0 or negative, the return value is unspecified.
164 */
165QColor QQuickColor::lighter(const QColor &baseColor, double factor) const
166{
167 return QQuickColorValueType(baseColor).lighter(factor);
168}
169
170/*!
171 * \qmlmethod color Color::tint(color baseColor, color tintColor)
172 *
173 * This function allows tinting one color (\a baseColor) with another (\a tintColor).
174 *
175 * The tint color should usually be mostly transparent, or you will not be
176 * able to see the underlying color. The example below provides a slight red
177 * tint by having the tint color be pure red, which is only 1/16th opaque.
178 *
179 * \qml
180 * Item {
181 * Rectangle {
182 * x: 0
183 * width: 80
184 * height: 80
185 * color: "lightsteelblue"
186 * }
187 * Rectangle {
188 * x: 100; width: 80; height: 80
189 * color: Color.tint("lightsteelblue", "#10FF0000")
190 * }
191 * }
192 * \endqml
193 * \image declarative-rect_tint.png {Side-by-side representation of a light
194 * steel blue square and a light steel blue square with a tint applied}
195 *
196 * Tint is most useful when a subtle change is intended to be conveyed due to some event;
197 * you can then use tinting to more effectively tune the visible color.
198 */
199QColor QQuickColor::tint(const QColor &baseColor, const QColor &tintColor) const
200{
201 return QQuickColorValueType(baseColor).tint(tintColor);
202}
203
204QT_END_NAMESPACE
205
206#include "moc_qquickcolor_p.cpp"
Combined button and popup list for selecting options.