FastLED 3.10.6
Loading...
Searching...
No Matches
ring_canvas.h
Go to the documentation of this file.
1// ring_canvas.h - ring geometry and additive drawing on a float ring.
2//
3// Positions are in turns: 0 is the first LED, 1 wraps back to it. Widths
4// are also in turns, so a lobe of width 0.05 covers 5% of the ring no
5// matter how many LEDs there are.
6//
7// Everything accumulates in float and is quantised to CRGB exactly once, in
8// MoodPainter. Scaling 8-bit values repeatedly is what produced the stair
9// steps in the old overlay: a dim pixel at 3 -> 2 -> 1 -> 0 is a visible
10// staircase, and a weight quantised to 1/255 steps between frames.
11#pragma once
12
13#include "FastLED.h"
14#include "fl/stl/span.h"
15#include "fl/stl/stdint.h"
16
17namespace mood_ring {
18
19inline float clampf(float v, float lo, float hi) {
20 return v < lo ? lo : (v > hi ? hi : v);
21}
22
24struct RGBf {
25 float r;
26 float g;
27 float b;
28 // Explicit ctors: C++11 forbids brace-init of a struct that has
29 // default member initializers.
30 RGBf() : r(0.0f), g(0.0f), b(0.0f) {}
31 RGBf(float r_, float g_, float b_) : r(r_), g(g_), b(b_) {}
32};
33
34RGBf toRGBf(CRGB c);
35CRGB toCRGB(const RGBf &p);
36inline RGBf rgbfWhite() { return RGBf{1.0f, 1.0f, 1.0f}; }
37
39float smoothstepf(float e0, float e1, float x);
40
42float wrapTurns(float t);
43
45float ringOffset(float a, float b);
46
48float ringDistance(float a, float b);
49
51float falloff(float distance, float sigma);
52
54class Xorshift {
55 public:
56 explicit Xorshift(fl::u32 seed = 0x9E3779B9u) : mState(seed ? seed : 1u) {}
57 fl::u32 next();
59 float unit();
60
61 private:
62 fl::u32 mState;
63};
64
67 public:
68 explicit RingCanvas(fl::span<RGBf> px) : mPx(px) {}
69
70 int size() const { return static_cast<int>(mPx.size()); }
71
72 void clear();
73
75 void lobe(float pos, float sigma, const RGBf &color, float gain);
76
79 void ring(float origin, float radius, float sigma, const RGBf &color,
80 float gain);
81
83 void fill(const RGBf &color, float gain);
84
86 void pixel(int i, const RGBf &color, float gain);
87
89 float posOf(int i) const;
90
91 private:
93};
94
95} // namespace mood_ring
int x
Definition simple.h:92
uint8_t pos
Definition Blur.ino:11
float posOf(int i) const
Position of pixel i in turns.
void pixel(int i, const RGBf &color, float gain)
Add to one pixel, index wrapped.
void lobe(float pos, float sigma, const RGBf &color, float gain)
Add a smooth lobe centred at pos (turns) with half-width sigma (turns).
void fill(const RGBf &color, float gain)
Add the same colour to every pixel.
fl::span< RGBf > mPx
Definition ring_canvas.h:92
void ring(float origin, float radius, float sigma, const RGBf &color, float gain)
Add a band at distance radius (turns, 0..0.5) from origin, on both sides of it at once: an expanding ...
RingCanvas(fl::span< RGBf > px)
Definition ring_canvas.h:68
float unit()
Uniform in [0, 1).
Xorshift(fl::u32 seed=0x9E3779B9u)
Definition ring_canvas.h:56
static uint32_t t
Definition Luminova.h:55
fl::CRGB CRGB
Definition crgb.h:25
float smoothstepf(float e0, float e1, float x)
0 below e0, 1 above e1, smooth in between.
RGBf toRGBf(CRGB c)
float ringDistance(float a, float b)
Shortest distance between two ring positions, in turns, in [0, 0.5].
CRGB toCRGB(const RGBf &p)
float clampf(float v, float lo, float hi)
Definition ring_canvas.h:19
float ringOffset(float a, float b)
Signed shortest offset from b to a on the ring, in (-0.5, 0.5].
float falloff(float distance, float sigma)
Smooth bump: 1 at distance 0, 0 at distance >= sigma.
RGBf rgbfWhite()
Definition ring_canvas.h:36
float wrapTurns(float t)
Wrap into [0, 1).
RGBf(float r_, float g_, float b_)
Definition ring_canvas.h:31
Linear-light float pixel, nominal range [0, 1], may exceed 1 before clip.
Definition ring_canvas.h:24