1.10
pattern.h
1/*
2 * Copyright (C) 2018 Microchip Technology Inc. All rights reserved.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6#ifndef EGT_PATTERN_H
7#define EGT_PATTERN_H
8
14#include <cairo.h>
15#include <cassert>
16#include <egt/color.h>
17#include <egt/detail/meta.h>
18#include <egt/geometry.h>
19#include <egt/types.h>
20#include <vector>
21
22namespace egt
23{
24inline namespace v1
25{
26
27/*
28 * gcc < 7.2 with c++14 support contains a bug dealing with constexpr on some
29 * member functions. clang seems to handle this correctly.
30 *
31 * pattern.h:88:27: error: enclosing class of constexpr non-static member
32 * function ‘egt::v1::Pattern::operator egt::v1::Color() const’ is not a literal type
33 *
34 * This is further explained here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66297
35 *
36 * Conditionally use constexpr in this case.
37 */
38#if defined(__clang__)
39# define EGT_PATTERN_CONSTEXPR constexpr
40#elif defined(__GNUC__)
41# if (__GNUC__ < 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 2))
42# define EGT_PATTERN_CONSTEXPR
43# else
44# define EGT_PATTERN_CONSTEXPR constexpr
45# endif
46#else
47# define EGT_PATTERN_CONSTEXPR constexpr
48#endif
49
54class EGT_API Pattern
55{
56public:
57
59 enum class Type
60 {
61 solid,
62 linear,
63 linear_vertical,
64 radial
65 };
66
68 using StepScaler = float;
69
71 using StepArray = std::vector<std::pair<float, Color>>;
72
73 Pattern() noexcept = default;
74
75 Pattern(const Pattern& rhs);
76 Pattern(Pattern&& rhs) noexcept;
77 Pattern& operator=(const Pattern& rhs);
78 Pattern& operator=(Pattern&& rhs) noexcept;
79
80 ~Pattern() noexcept;
81
82 // cppcheck-suppress noExplicitConstructor
83 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
84 constexpr Pattern(const Color& color) noexcept
85 : m_color(color)
86 {}
87
91 explicit Pattern(Type type, const StepArray& steps = {});
92
102 Pattern(const StepArray& steps,
103 const Point& start,
104 const Point& end);
105
117 Pattern(const StepArray& steps,
118 const Point& start,
119 float start_radius,
120 const Point& end,
121 float end_radius);
122
126 bool operator==(const Pattern& rhs) const;
127 bool operator!=(const Pattern& rhs) const
128 {
129 return !operator==(rhs);
130 }
131
135 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
136 EGT_PATTERN_CONSTEXPR operator Color() const
137 {
138 return solid();
139 }
140
146 EGT_PATTERN_CONSTEXPR Color solid() const
147 {
148 if (m_type != Type::solid)
149 throw std::runtime_error("requested solid color in pattern that is not a solid color");
150 return m_color;
151 }
152
157 EGT_DEPRECATED EGT_NODISCARD Color color() const
158 {
159 return first();
160 }
161
165 EGT_NODISCARD Color first() const;
166
170 Pattern& step(StepScaler offset, const Color& color);
171
175 EGT_NODISCARD Type type() const { return m_type; }
176
180 void linear(const Point& start, const Point& end);
181
185 void radial(const Point& start, float start_radius,
186 const Point& end, float end_radius);
187
189 EGT_NODISCARD Point starting() const;
191 EGT_NODISCARD float starting_radius() const;
193 EGT_NODISCARD Point ending() const;
195 EGT_NODISCARD float ending_radius() const;
196
198 EGT_NODISCARD const StepArray& steps() const;
199
201 EGT_NODISCARD cairo_pattern_t* pattern() const
202 {
203 if (!m_pattern)
204 create_pattern();
205 assert(m_pattern.get());
206 return m_pattern.get();
207 }
208
209protected:
210
212 void create_pattern() const;
213
215 static bool sort_by_first(const std::pair<float, Color>& a,
216 const std::pair<float, Color>& b)
217 {
218 return (a.first < b.first);
219 }
220
222 Type m_type{Type::solid};
223
226
227 struct PatternImpl;
229 PatternImpl* m_impl{nullptr};
230
233};
234
235static_assert(detail::rule_of_5<Pattern>(), "must fulfill rule of 5");
236
237}
238}
239
240#endif
32 bit RGBA color.
Definition color.h:41
A Pattern which can store one or more colors at different offsets (steps) which can be used to create...
Definition pattern.h:55
EGT_NODISCARD Color first() const
Get the first color of the pattern.
float StepScaler
Scalar type for pattern steps.
Definition pattern.h:68
Type
Type of pattern.
Definition pattern.h:60
Pattern() noexcept=default
void radial(const Point &start, float start_radius, const Point &end, float end_radius)
Create a radial gradient from start to end.
std::vector< std::pair< float, Color > > StepArray
Step array type.
Definition pattern.h:71
void linear(const Point &start, const Point &end)
Create a linear gradient from start to end.
bool operator!=(const Pattern &rhs) const
Definition pattern.h:127
Pattern(const StepArray &steps, const Point &start, const Point &end)
Construct a linear pattern with the specified steps.
Color m_color
Solid color of the pattern.
Definition pattern.h:225
EGT_NODISCARD cairo_pattern_t * pattern() const
Get internal pattern representation.
Definition pattern.h:201
Pattern(Type type, const StepArray &steps={})
Construct a pattern with the specified type and steps.
EGT_DEPRECATED EGT_NODISCARD Color color() const
Get the first color of the pattern.
Definition pattern.h:157
EGT_NODISCARD Point ending() const
Get the ending point of the pattern.
EGT_PATTERN_CONSTEXPR Color solid() const
Get the solid color.
Definition pattern.h:146
Pattern & step(StepScaler offset, const Color &color)
Add a step to the gradient.
EGT_NODISCARD const StepArray & steps() const
Get all of the steps of the pattern.
EGT_NODISCARD Type type() const
Get the type of pattern.
Definition pattern.h:175
EGT_NODISCARD float ending_radius() const
Get the ending radius of the pattern.
bool operator==(const Pattern &rhs) const
Compare two patterns.
shared_cairo_pattern_t m_pattern
Internal pattern representation.
Definition pattern.h:232
Pattern(const StepArray &steps, const Point &start, float start_radius, const Point &end, float end_radius)
Construct a radial pattern with the specified steps.
EGT_NODISCARD Point starting() const
Get the starting point of the pattern.
EGT_NODISCARD float starting_radius() const
Get the starting radius of the pattern.
constexpr bool operator==(const Color &lhs, const Color &rhs)
Color operator.
Definition color.h:500
std::shared_ptr< cairo_pattern_t > shared_cairo_pattern_t
Shared pointer for a cairo pattern.
Definition types.h:41
EGT framework namespace.
Definition animation.h:24