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
(factor <= 0.0)
115
return
a;
116
if
(factor >= 1.0)
117
return
b;
118
119
const
auto
rgbA = a.toRgb();
120
const
auto
rgbB = b.toRgb();
121
QColor color;
122
color.setRedF(rgbA.redF() * (1.0 - factor) + rgbB.redF() * factor);
123
color.setGreenF(rgbA.greenF() * (1.0 - factor) + rgbB.greenF() * factor);
124
color.setBlueF(rgbA.blueF() * (1.0 - factor) + rgbB.blueF() * factor);
125
return
color;
126
}
127
128
/*!
129
* \qmlmethod color Color::darker(color baseColor, real factor)
130
*
131
* Returns a color darker than \a baseColor by the \a factor provided.
132
*
133
* The function converts the current RGB color to HSV, divides the value (V) component
134
* by factor and converts the color back to RGB.
135
*
136
* If the factor is greater than 1.0, this function returns a darker color. Setting factor to 3.0
137
* returns a color that has one-third the brightness. If the factor is less than 1.0, the return
138
* color is lighter, but we recommend using the \l{Color::lighter()} function for this purpose. If
139
* the factor is 0 or negative, the return value is unspecified.
140
*/
141
QColor QQuickColor::darker(
const
QColor &baseColor,
double
factor)
const
142
{
143
return
QQuickColorValueType(baseColor).darker(factor);
144
}
145
146
/*!
147
* \qmlmethod color Color::lighter(color baseColor, real factor)
148
*
149
* Returns a color lighter than \a baseColor by the \a factor provided.
150
*
151
* The function converts the current RGB color to HSV, multiplies the value (V) component
152
* by factor and converts the color back to RGB.
153
*
154
* If the factor is greater than 1.0, this functions returns a lighter color. Setting factor to 1.5
155
* returns a color that is 50% brighter. If the factor is less than 1.0, the return color is
156
* darker, but we recommend using the \l{Color::darker()} function for this purpose. If the factor
157
* is 0 or negative, the return value is unspecified.
158
*/
159
QColor QQuickColor::lighter(
const
QColor &baseColor,
double
factor)
const
160
{
161
return
QQuickColorValueType(baseColor).lighter(factor);
162
}
163
164
/*!
165
* \qmlmethod color Color::tint(color baseColor, color tintColor)
166
*
167
* This function allows tinting one color (\a baseColor) with another (\a tintColor).
168
*
169
* The tint color should usually be mostly transparent, or you will not be
170
* able to see the underlying color. The example below provides a slight red
171
* tint by having the tint color be pure red, which is only 1/16th opaque.
172
*
173
* \qml
174
* Item {
175
* Rectangle {
176
* x: 0
177
* width: 80
178
* height: 80
179
* color: "lightsteelblue"
180
* }
181
* Rectangle {
182
* x: 100; width: 80; height: 80
183
* color: Color.tint("lightsteelblue", "#10FF0000")
184
* }
185
* }
186
* \endqml
187
* \image declarative-rect_tint.png {Side-by-side representation of a light
188
* steel blue square and a light steel blue square with a tint applied}
189
*
190
* Tint is most useful when a subtle change is intended to be conveyed due to some event;
191
* you can then use tinting to more effectively tune the visible color.
192
*/
193
QColor QQuickColor::tint(
const
QColor &baseColor,
const
QColor &tintColor)
const
194
{
195
return
QQuickColorValueType(baseColor).tint(tintColor);
196
}
197
198
QT_END_NAMESPACE
199
200
#
include
"moc_qquickcolor_p.cpp"
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qrandomaccessasyncfile_darwin.mm:17
qtdeclarative
src
quick
util
qquickcolor.cpp
Generated on
for Qt by
1.16.1