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