1.11
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 <cassert>
15#include <egt/color.h>
16#include <egt/detail/meta.h>
17#include <egt/geometry.h>
18#include <egt/types.h>
19#include <vector>
20
21namespace egt
22{
23inline namespace v1
24{
25
26namespace detail
27{
28class InternalPattern;
29}
30
31/*
32 * gcc < 7.2 with c++14 support contains a bug dealing with constexpr on some
33 * member functions. clang seems to handle this correctly.
34 *
35 * pattern.h:88:27: error: enclosing class of constexpr non-static member
36 * function ‘egt::v1::Pattern::operator egt::v1::Color() const’ is not a literal type
37 *
38 * This is further explained here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66297
39 *
40 * Conditionally use constexpr in this case.
41 */
42#if defined(__clang__)
43# define EGT_PATTERN_CONSTEXPR constexpr
44#elif defined(__GNUC__)
45# if (__GNUC__ < 7 || (__GNUC__ == 7 && __GNUC_MINOR__ < 2))
46# define EGT_PATTERN_CONSTEXPR
47# else
48# define EGT_PATTERN_CONSTEXPR constexpr
49# endif
50#else
51# define EGT_PATTERN_CONSTEXPR constexpr
52#endif
53
58class EGT_API Pattern
59{
60public:
61
63 enum class Type
64 {
65 solid,
66 linear,
67 linear_vertical,
68 radial
69 };
70
72 using StepScaler = float;
73
75 using StepArray = std::vector<std::pair<float, Color>>;
76
77 Pattern() noexcept = default;
78
79 Pattern(const Pattern& rhs);
80 Pattern(Pattern&& rhs) noexcept;
81 Pattern& operator=(const Pattern& rhs);
82 Pattern& operator=(Pattern&& rhs) noexcept;
83
84 ~Pattern() noexcept;
85
86 // cppcheck-suppress noExplicitConstructor
87 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
88 constexpr Pattern(const Color& color) noexcept
89 : m_color(color)
90 {}
91
95 explicit Pattern(Type type, const StepArray& steps = {});
96
106 Pattern(const StepArray& steps,
107 const Point& start,
108 const Point& end);
109
121 Pattern(const StepArray& steps,
122 const Point& start,
123 float start_radius,
124 const Point& end,
125 float end_radius);
126
130 bool operator==(const Pattern& rhs) const;
131 bool operator!=(const Pattern& rhs) const
132 {
133 return !operator==(rhs);
134 }
135
139 // NOLINTNEXTLINE(hicpp-explicit-conversions, google-explicit-constructor)
140 EGT_PATTERN_CONSTEXPR operator Color() const
141 {
142 return solid();
143 }
144
150 EGT_PATTERN_CONSTEXPR Color solid() const
151 {
152 if (m_type != Type::solid)
153 throw std::runtime_error("requested solid color in pattern that is not a solid color");
154 return m_color;
155 }
156
161 EGT_DEPRECATED EGT_NODISCARD Color color() const
162 {
163 return first();
164 }
165
169 EGT_NODISCARD Color first() const;
170
174 Pattern& step(StepScaler offset, const Color& color);
175
179 EGT_NODISCARD Type type() const { return m_type; }
180
184 void linear(const Point& start, const Point& end);
185
189 void radial(const Point& start, float start_radius,
190 const Point& end, float end_radius);
191
193 EGT_NODISCARD Point starting() const;
195 EGT_NODISCARD float starting_radius() const;
197 EGT_NODISCARD Point ending() const;
199 EGT_NODISCARD float ending_radius() const;
200
202 EGT_NODISCARD const StepArray& steps() const;
203
205 EGT_NODISCARD const detail::InternalPattern& pattern() const
206 {
207 if (!m_pattern)
208 create_pattern();
209 assert(m_pattern.get());
210 return *m_pattern;
211 }
212
213protected:
214
216 void create_pattern() const;
217
219 static bool sort_by_first(const std::pair<float, Color>& a,
220 const std::pair<float, Color>& b)
221 {
222 return (a.first < b.first);
223 }
224
226 Type m_type{Type::solid};
227
230
231 struct PatternImpl;
233 PatternImpl* m_impl{nullptr};
234
236 mutable std::shared_ptr<detail::InternalPattern> m_pattern;
237};
238
239static_assert(detail::rule_of_5<Pattern>(), "must fulfill rule of 5");
240
241}
242}
243
244#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:59
EGT_NODISCARD Color first() const
Get the first color of the pattern.
float StepScaler
Scalar type for pattern steps.
Definition pattern.h:72
Type
Type of pattern.
Definition pattern.h:64
EGT_NODISCARD const detail::InternalPattern & pattern() const
Get internal pattern representation.
Definition pattern.h:205
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:75
std::shared_ptr< detail::InternalPattern > m_pattern
Internal pattern representation.
Definition pattern.h:236
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:131
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:229
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:161
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:150
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:179
EGT_NODISCARD float ending_radius() const
Get the ending radius of the pattern.
bool operator==(const Pattern &rhs) const
Compare two patterns.
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
EGT framework namespace.
Definition animation.h:24