QSkinny 0.8.0
C++/Qt UI toolkit
Loading...
Searching...
No Matches
QskVertex.h
1/******************************************************************************
2 * QSkinny - Copyright (C) The authors
3 * SPDX-License-Identifier: BSD-3-Clause
4 *****************************************************************************/
5
6#ifndef QSK_VERTEX_H
7#define QSK_VERTEX_H
8
9#include "QskGlobal.h"
10
11#include <qcolor.h>
12#include <qsggeometry.h>
13#include <qline.h>
14
15namespace QskVertex
16{
17 class Color
18 {
19 public:
20 constexpr Color() noexcept;
21
22 constexpr Color( unsigned char red, unsigned char green,
23 unsigned char blue, unsigned char alpha ) noexcept;
24
25 Color( QRgb ) noexcept;
26 Color( const QColor& );
27
28 Color interpolatedTo( Color, qreal ratio ) const noexcept;
29
30 constexpr bool operator==( const Color& ) const noexcept;
31 constexpr bool operator!=( const Color& ) const noexcept;
32
33 unsigned char r, g, b, a;
34 };
35
36 inline constexpr Color::Color() noexcept
37 : r( 0 )
38 , g( 0 )
39 , b( 0 )
40 , a( 255 )
41 {
42 }
43
44 inline constexpr Color::Color( unsigned char red, unsigned char green,
45 unsigned char blue, unsigned char alpha ) noexcept
46 : r( red )
47 , g( green )
48 , b( blue )
49 , a( alpha )
50 {
51 }
52
53 inline Color::Color( QRgb rgb ) noexcept
54 {
55 r = qRed( rgb );
56 g = qGreen( rgb );
57 b = qBlue( rgb );
58 a = qAlpha( rgb );
59
60 if ( a < 255 )
61 {
62 const auto af = a / 255.0;
63
64 r *= af;
65 g *= af;
66 b *= af;
67 }
68 }
69
70 inline Color::Color( const QColor& color )
71 : Color( color.rgba() )
72 {
73 }
74
75 inline Color Color::interpolatedTo( Color colorTo, qreal ratio ) const noexcept
76 {
77 if ( ratio <= 0.0 )
78 return *this;
79
80 if ( ratio >= 1.0 )
81 return colorTo;
82
83 const auto t = ratio;
84 const auto rt = 1.0 - ratio;
85
86 return Color(
87 static_cast< unsigned char >( rt * r + t * colorTo.r ),
88 static_cast< unsigned char >( rt * g + t * colorTo.g ),
89 static_cast< unsigned char >( rt * b + t * colorTo.b ),
90 static_cast< unsigned char >( rt * a + t * colorTo.a )
91 );
92 }
93
94 inline constexpr bool Color::operator==( const Color& other ) const noexcept
95 {
96 return ( r == other.r ) && ( g == other.g )
97 && ( b == other.b ) && ( a == other.a );
98 }
99
100 inline constexpr bool Color::operator!=( const Color& other ) const noexcept
101 {
102 return !( *this == other );
103 }
104}
105
106namespace QskVertex
107{
108 class Line
109 {
110 public:
111 inline void setLine( float x1, float y1, float x2, float y2 ) noexcept
112 {
113 p1.set( x1, y1 );
114 p2.set( x2, y2 );
115 }
116
117 inline void setLine( const QPointF& p1, const QPointF& p2 ) noexcept
118 {
119 setLine( p1.x(), p1.y(), p2.x(), p2.y() );
120 }
121
122 inline void setHLine( float x1, float x2, float y ) noexcept
123 {
124 setLine( x1, y, x2, y );
125 }
126
127 inline void setVLine( float x, float y1, float y2 ) noexcept
128 {
129 setLine( x, y1, x, y2 );
130 }
131
132 inline void setLine( const QPointF& p1, const QPointF& p2, Color ) noexcept
133 {
134 /* The color parameter makes no sense, but is useful
135 when being used from templated code
136 */
137
138 setLine( p1.x(), p1.y(), p2.x(), p2.y() );
139 }
140
141 inline void setLine( float x1, float y1, float x2, float y2, Color ) noexcept
142 {
143 /* The color parameter makes no sense, but is useful
144 when being used from templated code
145 */
146 setLine( x1, y1, x2, y2 );
147 }
148
149 inline float x1() const noexcept { return p1.x; }
150 inline float y1() const noexcept { return p1.y; }
151
152 inline float x2() const noexcept { return p2.x; }
153 inline float y2() const noexcept { return p2.y; }
154
155 inline float dx() const noexcept { return p2.x - p1.x; }
156 inline float dy() const noexcept { return p2.y - p1.y; }
157
158 QSGGeometry::Point2D p1;
159 QSGGeometry::Point2D p2;
160 };
161
163 {
164 public:
165 inline void setLine( float x1, float y1, Color c1,
166 float x2, float y2, Color c2 ) noexcept
167 {
168 p1.set( x1, y1, c1.r, c1.g, c1.b, c1.a );
169 p2.set( x2, y2, c2.r, c2.g, c2.b, c2.a );
170 }
171
172 inline void setLine( float x1, float y1, float x2, float y2, Color color ) noexcept
173 {
174 setLine( x1, y1, color, x2, y2, color );
175 }
176
177 inline void setLine( const QPointF& p1, const QPointF& p2, Color color ) noexcept
178 {
179 setLine( p1.x(), p1.y(), color, p2.x(), p2.y(), color );
180 }
181
182 inline void setLine( const QLineF& line, Color color ) noexcept
183 {
184 setLine( line.x1(), line.y1(), color, line.x2(), line.y2(), color );
185 }
186
187 inline void setHLine( qreal x1, qreal x2, qreal y, Color color ) noexcept
188 {
189 setLine( x1, y, color, x2, y, color );
190 }
191
192 inline void setVLine( qreal x, qreal y1, qreal y2, Color color ) noexcept
193 {
194 setLine( x, y1, color, x, y2, color );
195 }
196
197 inline float x1() const noexcept { return p1.x; }
198 inline float y1() const noexcept { return p1.y; }
199
200 inline float x2() const noexcept { return p2.x; }
201 inline float y2() const noexcept { return p2.y; }
202
203 inline float dx() const noexcept { return p2.x - p1.x; }
204 inline float dy() const noexcept { return p2.y - p1.y; }
205
206 QSGGeometry::ColoredPoint2D p1;
207 QSGGeometry::ColoredPoint2D p2;
208 };
209
210 template< class Line >
211 static inline Line* allocateLines( QSGGeometry& geometry, int lineCount )
212 {
213 geometry.allocate( 2 * lineCount ); // 2 points per line
214 return reinterpret_cast< Line* >( geometry.vertexData() );
215 }
216}
217
218namespace QskVertex
219{
220 void debugGeometry( const QSGGeometry& );
221}
222
223#ifndef QT_NO_DEBUG_STREAM
224
225 class QDebug;
226
227 QDebug operator<<( QDebug debug, QskVertex::Color );
228 QDebug operator<<( QDebug debug, const QskVertex::ColoredLine& );
229 QDebug operator<<( QDebug debug, const QskVertex::Line& );
230
231#endif
232
233#endif