FastLED 3.10.8
Loading...
Searching...
No Matches
tile2x2.cpp.hpp
Go to the documentation of this file.
1#include "fl/gfx/tile2x2.h"
2#include "crgb.h"
4#include "fl/math/math.h"
5#include "fl/gfx/raster.h"
8#include "fl/log/log.h"
9#include "fl/math/xymap.h"
10#include "fl/stl/vector.h"
11#include "fl/stl/noexcept.h"
12
13namespace fl {
14
15namespace {
16static vec2<u16> wrap(const vec2<u16> &v, const vec2<u16> &size) FL_NO_EXCEPT {
17 // Wrap the vector v around the size
18 return vec2<u16>(v.x % size.x, v.y % size.y);
19}
20
21static vec2<u16> wrap_x(const vec2<u16> &v, const u16 width) FL_NO_EXCEPT {
22 // Wrap the x component of the vector v around the size
23 return vec2<u16>(v.x % width, v.y);
24}
25} // namespace
26
28 mData[0][0] = {vec2<u16>(0, 0), 0};
29 mData[0][1] = {vec2<u16>(0, 1), 0};
30 mData[1][0] = {vec2<u16>(1, 0), 0};
31 mData[1][1] = {vec2<u16>(1, 1), 0};
32}
33
35 XYRasterU8Sparse *out_raster) FL_NO_EXCEPT {
36 out_raster->rasterize(tiles);
37}
38
40 XYDrawComposited visitor(color, xymap, out);
41 draw(xymap, visitor);
42}
43
45 // scale the tile
46 if (scale == 255) {
47 return;
48 }
49 for (int x = 0; x < 2; ++x) {
50 for (int y = 0; y < 2; ++y) {
51 u16 value = at(x, y);
52 at(x, y) = (value * scale) >> 8;
53 }
54 }
55}
56
58 const vec2<u16> origin = from.origin();
59 at(0, 0) = {wrap_x(vec2<u16>(origin.x, origin.y), width), from.at(0, 0)};
60 at(0, 1) = {wrap_x(vec2<u16>(origin.x, origin.y + 1), width), from.at(0, 1)};
61 at(1, 0) = {wrap_x(vec2<u16>(origin.x + 1, origin.y), width), from.at(1, 0)};
62 at(1, 1) = {wrap_x(vec2<u16>(origin.x + 1, origin.y + 1), width),
63 from.at(1, 1)};
64}
65
67 // Wrap around the edges
68 x = (x + 2) % 2;
69 y = (y + 2) % 2;
70 return mData[y][x];
71}
72
73
75 mData[0][0] = data[0][0];
76 mData[0][1] = data[0][1];
77 mData[1][0] = data[1][0];
78 mData[1][1] = data[1][1];
79}
80
81
83 // Wrap around the edges
84 x = (x + 2) % 2;
85 y = (y + 2) % 2;
86 return mData[y][x];
87}
88
90 u16 height) FL_NO_EXCEPT {
91 const vec2<u16> origin = from.origin();
92 at(0, 0) = {wrap(vec2<u16>(origin.x, origin.y), vec2<u16>(width, height)),
93 from.at(0, 0)};
94 at(0, 1) = {wrap(vec2<u16>(origin.x, origin.y + 1), vec2<u16>(width, height)),
95 from.at(0, 1)};
96 at(1, 0) = {wrap(vec2<u16>(origin.x + 1, origin.y), vec2<u16>(width, height)),
97 from.at(1, 0)};
98 at(1,
99 1) = {wrap(vec2<u16>(origin.x + 1, origin.y + 1), vec2<u16>(width, height)),
100 from.at(1, 1)};
101}
102
104 u8 max = 0;
105 max = fl::max(max, at(0, 0));
106 max = fl::max(max, at(0, 1));
107 max = fl::max(max, at(1, 0));
108 max = fl::max(max, at(1, 1));
109 return max;
110}
111
114 for (int x = 0; x < 2; ++x) {
115 for (int y = 0; y < 2; ++y) {
116 result.at(x, y) = fl::max(a.at(x, y), b.at(x, y));
117 }
118 }
119 return result;
120}
121
127
130
131 // Clamp t to [0, 1]
132 if (t <= 0.0f) {
133 result.push_back(a);
134 return result;
135 }
136 if (t >= 1.0f) {
137 result.push_back(b);
138 return result;
139 }
140
141 // Create interpolated tile
142 Tile2x2_u8_wrap interpolated;
143
144 // Interpolate each of the 4 positions
145 for (u16 x = 0; x < 2; ++x) {
146 for (u16 y = 0; y < 2; ++y) {
147 const auto& data_a = a.at(x, y);
148 const auto& data_b = b.at(x, y);
149
150 // For now, assume positions are the same or close enough
151 // Use position from 'a' as the base
152 vec2<u16> pos = data_a.first;
153
154 // Simple linear interpolation for alpha values
155 u8 alpha_a = data_a.second;
156 u8 alpha_b = data_b.second;
157
158 // Linear interpolation: a + t * (b - a)
159 float alpha_float = alpha_a + t * (alpha_b - alpha_a);
160 u8 interpolated_alpha = static_cast<u8>(alpha_float + 0.5f); // Round to nearest
161
162 interpolated.mData[y][x] = {pos, interpolated_alpha};
163 }
164 }
165
166 result.push_back(interpolated);
167 return result;
168}
169
170} // namespace fl
XYMap xymap
uint8_t pos
Definition Blur.ino:11
Entry & at(u16 x, u16 y) FL_NO_EXCEPT
fl::pair< vec2< u16 >, u8 > Entry
Definition tile2x2.h:103
Entry[2][2] Data
Definition tile2x2.h:104
Tile2x2_u8_wrap() FL_NO_EXCEPT
static vector_fixed< Tile2x2_u8_wrap, 2 > Interpolate(const Tile2x2_u8_wrap &a, const Tile2x2_u8_wrap &b, float t) FL_NO_EXCEPT
u8 & at(int x, int y) FL_NO_EXCEPT
Definition tile2x2.h:34
u8 maxValue() const FL_NO_EXCEPT
Tile2x2_u8() FL_NO_EXCEPT=default
void draw(const CRGB &color, const XYMap &xymap, fl::span< CRGB > out) const FL_NO_EXCEPT
rect< u16 > bounds() const FL_NO_EXCEPT
bounds => [begin_x, end_x) (where end_x is exclusive)
static void Rasterize(const span< const Tile2x2_u8 > &tiles, XYRasterU8Sparse *output) FL_NO_EXCEPT
void scale(u8 scale) FL_NO_EXCEPT
static Tile2x2_u8 MaxTile(const Tile2x2_u8 &a, const Tile2x2_u8 &b) FL_NO_EXCEPT
vec2< u16 > mOrigin
Definition tile2x2.h:94
void draw(float pos)
Definition curr.h:526
Defines the 8-bit red, green, and blue (RGB) pixel type in the fl namespace.
Centralized logging categories for FastLED hardware interfaces and subsystems.
static vec2< u16 > wrap_x(const vec2< u16 > &v, const u16 width) FL_NO_EXCEPT
static vec2< u16 > wrap(const vec2< u16 > &v, const vec2< u16 > &size) FL_NO_EXCEPT
unsigned char u8
Definition stdint.h:131
constexpr common_type_t< T, U > max(T a, U b) FL_NO_EXCEPT
Definition math.h:75
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
constexpr int type_rank< T >::value
u8 u8 height
Definition blur.h:186
FL_DISABLE_WARNING_PUSH U constexpr common_type_t< T, U > min(T a, U b) FL_NO_EXCEPT
Definition math.h:71
u8 width
Definition blur.h:186
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
FixedVector< T, INLINED_SIZE > vector_fixed
Definition vector.h:1134
Base definition for an LED controller.
Definition crgb.hpp:179
Representation of an 8-bit RGB pixel (Red, Green, Blue)
Definition crgb.h:38
value_type y
Definition geometry.h:191
value_type x
Definition geometry.h:190