Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qrgbafloat.h
Go to the documentation of this file.
1// Copyright (C) 2021 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
4#ifndef QRGBAFLOAT_H
5#define QRGBAFLOAT_H
6
7#include <QtGui/qtguiglobal.h>
8#include <QtCore/qfloat16.h>
9
10#include <algorithm>
11#include <cmath>
12#include <type_traits>
13
15
16template<typename F>
17class alignas(sizeof(F) * 4) QRgbaFloat
18{
19 static_assert(std::is_same<F, qfloat16>::value || std::is_same<F, float>::value);
20public:
21 using Type = F;
22#if defined(__AVX512FP16__) && QFLOAT16_IS_NATIVE
23 // AVX512FP16 has multiplication instructions
24 using FastType = F;
25#else
26 // use FP32 for multiplications
27 using FastType = float;
28#endif
29 F r;
30 F g;
31 F b;
32 F a;
33
34 static constexpr
36 {
37 constexpr FastType scale = FastType(1.0f / 65535.0f);
38 return QRgbaFloat{
39 F(red * scale),
40 F(green * scale),
41 F(blue * scale),
42 F(alpha * scale) };
43 }
44
45 static constexpr
47 {
48 constexpr FastType scale = FastType(1.0f / 255.0f);
49 return QRgbaFloat{
50 F(red * scale),
51 F(green * scale),
52 F(blue * scale),
53 F(alpha * scale) };
54 }
55 static constexpr
57 {
58 return fromRgba(quint8(rgb >> 16), quint8(rgb >> 8), quint8(rgb), quint8(rgb >> 24));
59 }
60
61 constexpr bool isOpaque() const { return a >= FastType(1.0f); }
62 constexpr bool isTransparent() const { return a <= FastType(0.0f); }
63
64 constexpr float red() const { return r; }
65 constexpr float green() const { return g; }
66 constexpr float blue() const { return b; }
67 constexpr float alpha() const { return a; }
68 void setRed(float _red) { r = F(_red); }
69 void setGreen(float _green) { g = F(_green); }
70 void setBlue(float _blue) { b = F(_blue); }
71 void setAlpha(float _alpha) { a = F(_alpha); }
72
73 constexpr float redNormalized() const { return clamp01(r); }
74 constexpr float greenNormalized() const { return clamp01(g); }
75 constexpr float blueNormalized() const { return clamp01(b); }
76 constexpr float alphaNormalized() const { return clamp01(a); }
77
78 constexpr quint8 red8() const { return qRound(redNormalized() * FastType(255.0f)); }
79 constexpr quint8 green8() const { return qRound(greenNormalized() * FastType(255.0f)); }
80 constexpr quint8 blue8() const { return qRound(blueNormalized() * FastType(255.0f)); }
81 constexpr quint8 alpha8() const { return qRound(alphaNormalized() * FastType(255.0f)); }
82 constexpr uint toArgb32() const
83 {
84 return uint((alpha8() << 24) | (red8() << 16) | (green8() << 8) | blue8());
85 }
86
87 constexpr quint16 red16() const { return qRound(redNormalized() * FastType(65535.0f)); }
88 constexpr quint16 green16() const { return qRound(greenNormalized() * FastType(65535.0f)); }
89 constexpr quint16 blue16() const { return qRound(blueNormalized() * FastType(65535.0f)); }
90 constexpr quint16 alpha16() const { return qRound(alphaNormalized() * FastType(65535.0f)); }
91
93 {
94 return QRgbaFloat{r * a, g * a, b * a, a};
95 }
97 {
98 if (a <= F{0.0f})
99 return QRgbaFloat{}; // default-initialization: zeroes
100 if (a >= F{1.0f})
101 return *this;
102 const FastType ia = 1.0f / a;
103 return QRgbaFloat{F(r * ia), F(g * ia), F(b * ia), F(a)};
104 }
105 constexpr bool operator==(QRgbaFloat f) const
106 {
107 return r == f.r && g == f.g && b == f.b && a == f.a;
108 }
109 constexpr bool operator!=(QRgbaFloat f) const
110 {
111 return !(*this == f);
112 }
113
114private:
115 constexpr static FastType clamp01(Type f)
116 {
117 return std::clamp(FastType(f), FastType(0.0f), FastType(1.0f));
118 }
119};
120
121typedef QRgbaFloat<qfloat16> QRgbaFloat16;
122typedef QRgbaFloat<float> QRgbaFloat32;
123
125
126#endif // QRGBAFLOAT_H
constexpr quint8 green8() const
Definition qrgbafloat.h:79
constexpr bool operator!=(QRgbaFloat f) const
Definition qrgbafloat.h:109
constexpr quint16 blue16() const
Definition qrgbafloat.h:89
constexpr float redNormalized() const
Definition qrgbafloat.h:73
constexpr float blueNormalized() const
Definition qrgbafloat.h:75
constexpr float alphaNormalized() const
Definition qrgbafloat.h:76
constexpr float green() const
Definition qrgbafloat.h:65
constexpr quint8 alpha8() const
Definition qrgbafloat.h:81
void setBlue(float _blue)
Definition qrgbafloat.h:70
constexpr quint8 red8() const
Definition qrgbafloat.h:78
constexpr float red() const
Definition qrgbafloat.h:64
void setRed(float _red)
Definition qrgbafloat.h:68
static constexpr QRgbaFloat fromArgb32(uint rgb)
Definition qrgbafloat.h:56
constexpr Q_ALWAYS_INLINE QRgbaFloat premultiplied() const
Definition qrgbafloat.h:92
constexpr quint16 red16() const
Definition qrgbafloat.h:87
constexpr bool isTransparent() const
Definition qrgbafloat.h:62
constexpr quint16 alpha16() const
Definition qrgbafloat.h:90
static constexpr QRgbaFloat fromRgba(quint8 red, quint8 green, quint8 blue, quint8 alpha)
Definition qrgbafloat.h:46
void setAlpha(float _alpha)
Definition qrgbafloat.h:71
static constexpr QRgbaFloat fromRgba64(quint16 red, quint16 green, quint16 blue, quint16 alpha)
Definition qrgbafloat.h:35
constexpr uint toArgb32() const
Definition qrgbafloat.h:82
float FastType
Definition qrgbafloat.h:27
constexpr float alpha() const
Definition qrgbafloat.h:67
constexpr quint16 green16() const
Definition qrgbafloat.h:88
constexpr bool isOpaque() const
Definition qrgbafloat.h:61
constexpr float greenNormalized() const
Definition qrgbafloat.h:74
void setGreen(float _green)
Definition qrgbafloat.h:69
constexpr bool operator==(QRgbaFloat f) const
Definition qrgbafloat.h:105
constexpr quint8 blue8() const
Definition qrgbafloat.h:80
constexpr Q_ALWAYS_INLINE QRgbaFloat unpremultiplied() const
Definition qrgbafloat.h:96
constexpr float blue() const
Definition qrgbafloat.h:66
Combined button and popup list for selecting options.
#define rgb(r, g, b)
Definition qcolor.cpp:124
#define Q_ALWAYS_INLINE
int qRound(qfloat16 d) noexcept
Definition qfloat16.h:327
GLboolean GLboolean GLboolean b
GLboolean GLboolean GLboolean GLboolean a
[7]
GLboolean r
[2]
GLfloat GLfloat f
GLboolean GLboolean g
GLbyte GLbyte blue
Definition qopenglext.h:385
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
GLbyte green
Definition qopenglext.h:385
GLenum GLenum GLenum GLenum GLenum scale
QRgbaFloat< float > QRgbaFloat32
Definition qrgbafloat.h:122
QRgbaFloat< qfloat16 > QRgbaFloat16
Definition qrgbafloat.h:121
unsigned short quint16
Definition qtypes.h:48
unsigned int uint
Definition qtypes.h:34
unsigned char quint8
Definition qtypes.h:46
Definition moc.h:23