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
5
#
include
"qquickcolor_p.h"
6
7
#
include
<
private
/
qquickvaluetypes_p
.
h
>
8
#
include
<
private
/
qv4engine_p
.
h
>
9
10
QT_BEGIN_NAMESPACE
11
12
/*!
13
* \qmltype Color
14
* \nativetype QQuickColor
15
* \inqmlmodule QtQuick
16
* \brief Holds utility functions for colors.
17
* \since 6.12
18
*/
19
QQuickColor::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
*/
29
QColor 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
*/
40
QColor 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
*/
51
QColor 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
*/
62
QColor 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==()}.
82
*
83
* \note This function is a typed version of \l{Qt::colorEqual()}
84
*/
85
bool
QQuickColor::equal(
const
QColor &lhs,
const
QColor &rhs)
const
86
{
87
return
lhs == rhs;
88
}
89
90
/*!
91
* \qmlmethod color Color::transparent(color color, real opacity)
92
*
93
* Returns \a color with an alpha value of \a opacity, which ranges from 0
94
* (completely transparent) to 1 (completely opaque).
95
*/
96
QColor QQuickColor::transparent(
const
QColor &color, qreal opacity)
const
97
{
98
QColor newColor = color;
99
newColor.setAlphaF(opacity);
100
return
newColor;
101
}
102
103
/*!
104
* \qmlmethod color Color::blend(color a, color b, real factor)
105
*
106
* Returns a color that is a blend of \a a and \a b. The resulting color is constructed from the
107
* independent linear interpolations of the RGB color channels of \a a and \a b.
108
*
109
* If \a factor is smaller than 0.0, \a a is returned. If factor is larger than 1.0, \a b is
110
* returned.
111
*/
112
QColor QQuickColor::blend(
const
QColor &a,
const
QColor &b, qreal factor)
const
113
{
114
if
(std::isnan(factor)) {
115
qWarning(
"NaN given as argument to Color.blend()"
);
116
return
QColor();
117
}
118
119
if
(factor <= 0.0)
120
return
a;
121
if
(factor >= 1.0)
122
return
b;
123
124
const
auto
rgbA = a.toRgb();
125
const
auto
rgbB = b.toRgb();
126
QColor color;
127
color.setRedF(rgbA.redF() * (1.0 - factor) + rgbB.redF() * factor);
128
color.setGreenF(rgbA.greenF() * (1.0 - factor) + rgbB.greenF() * factor);
129
color.setBlueF(rgbA.blueF() * (1.0 - factor) + rgbB.blueF() * factor);
130
return
color;
131
}
132
133
/*!
134
* \qmlmethod color Color::darker(color baseColor, real factor)
135
*
136
* Returns a color darker than \a baseColor by the \a factor provided.
137
*
138
* The function converts the current RGB color to HSV, divides the value (V) component
139
* by factor and converts the color back to RGB.
140
*
141
* If the factor is greater than 1.0, this function returns a darker color. Setting factor to 3.0
142
* returns a color that has one-third the brightness. If the factor is less than 1.0, the return
143
* color is lighter, but we recommend using the \l{Color::lighter()} function for this purpose. If
144
* the factor is 0 or negative, the return value is unspecified.
145
*/
146
QColor QQuickColor::darker(
const
QColor &baseColor,
double
factor)
const
147
{
148
return
QQuickColorValueType(baseColor).darker(factor);
149
}
150
151
/*!
152
* \qmlmethod color Color::lighter(color baseColor, real factor)
153
*
154
* Returns a color lighter than \a baseColor by the \a factor provided.
155
*
156
* The function converts the current RGB color to HSV, multiplies the value (V) component
157
* by factor and converts the color back to RGB.
158
*
159
* If the factor is greater than 1.0, this functions returns a lighter color. Setting factor to 1.5
160
* returns a color that is 50% brighter. If the factor is less than 1.0, the return color is
161
* darker, but we recommend using the \l{Color::darker()} function for this purpose. If the factor
162
* is 0 or negative, the return value is unspecified.
163
*/
164
QColor QQuickColor::lighter(
const
QColor &baseColor,
double
factor)
const
165
{
166
return
QQuickColorValueType(baseColor).lighter(factor);
167
}
168
169
/*!
170
* \qmlmethod color Color::tint(color baseColor, color tintColor)
171
*
172
* This function allows tinting one color (\a baseColor) with another (\a tintColor).
173
*
174
* The tint color should usually be mostly transparent, or you will not be
175
* able to see the underlying color. The example below provides a slight red
176
* tint by having the tint color be pure red, which is only 1/16th opaque.
177
*
178
* \qml
179
* Item {
180
* Rectangle {
181
* x: 0
182
* width: 80
183
* height: 80
184
* color: "lightsteelblue"
185
* }
186
* Rectangle {
187
* x: 100; width: 80; height: 80
188
* color: Color.tint("lightsteelblue", "#10FF0000")
189
* }
190
* }
191
* \endqml
192
* \image declarative-rect_tint.png {Side-by-side representation of a light
193
* steel blue square and a light steel blue square with a tint applied}
194
*
195
* Tint is most useful when a subtle change is intended to be conveyed due to some event;
196
* you can then use tinting to more effectively tune the visible color.
197
*/
198
QColor QQuickColor::tint(
const
QColor &baseColor,
const
QColor &tintColor)
const
199
{
200
return
QQuickColorValueType(baseColor).tint(tintColor);
201
}
202
203
QT_END_NAMESPACE
204
205
#
include
"moc_qquickcolor_p.cpp"
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtdeclarative
src
quick
util
qquickcolor.cpp
Generated on
for Qt by
1.16.1