Skip to content

Commit b7bfd6f

Browse files
authored
Copy Segment FX (#4124)
* Added FX to copy a segment in 1D or 2D - copies the source segment - brightness of segment is relative to source segment - optionally shifts the color hue, saturation and brightness - invert, transpose, mirror work - if source or targets do not match in size, smallest size is copied - unused pixels fade to black (allows overlapping segments) - if invalid source ID is set, segment just fades to black - added a rgb2hsv conversion function as the fastled variant is inaccurate and buggy - 1D to 2D and vice versa are supported
1 parent 8bcd455 commit b7bfd6f

File tree

4 files changed

+71
-0
lines changed

4 files changed

+71
-0
lines changed

wled00/FX.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ static um_data_t* getAudioData() {
114114
return um_data;
115115
}
116116

117+
117118
// effect functions
118119

119120
/*
@@ -125,6 +126,56 @@ uint16_t mode_static(void) {
125126
}
126127
static const char _data_FX_MODE_STATIC[] PROGMEM = "Solid";
127128

129+
/*
130+
* Copy a segment and perform (optional) color adjustments
131+
*/
132+
uint16_t mode_copy_segment(void) {
133+
uint32_t sourceid = SEGMENT.custom3;
134+
if (sourceid >= strip.getSegmentsNum() || sourceid == strip.getCurrSegmentId()) { // invalid source
135+
SEGMENT.fadeToBlackBy(5); // fade out
136+
return FRAMETIME;
137+
}
138+
Segment sourcesegment = strip.getSegment(sourceid);
139+
if (sourcesegment.isActive()) {
140+
uint32_t sourcecolor;
141+
uint32_t destcolor;
142+
if(sourcesegment.is2D()) { // 2D source, note: 2D to 1D just copies the first row (or first column if 'Switch axis' is checked in FX)
143+
for (unsigned y = 0; y < SEGMENT.vHeight(); y++) {
144+
for (unsigned x = 0; x < SEGMENT.vWidth(); x++) {
145+
unsigned sx = x; // source coordinates
146+
unsigned sy = y;
147+
if(SEGMENT.check1) std::swap(sx, sy); // flip axis
148+
if(SEGMENT.check2) {
149+
sourcecolor = strip.getPixelColorXY(sx + sourcesegment.start, sy + sourcesegment.startY); // read from global buffer (reads the last rendered frame)
150+
}
151+
else {
152+
sourcesegment.setDrawDimensions(); // set to source segment dimensions
153+
sourcecolor = sourcesegment.getPixelColorXY(sx, sy); // read from segment buffer
154+
}
155+
destcolor = adjust_color(sourcecolor, SEGMENT.intensity, SEGMENT.custom1, SEGMENT.custom2);
156+
SEGMENT.setDrawDimensions(); // reset to current segment dimensions
157+
SEGMENT.setPixelColorXY(x, y, destcolor);
158+
}
159+
}
160+
} else { // 1D source, source can be expanded into 2D
161+
for (unsigned i = 0; i < SEGMENT.vLength(); i++) {
162+
if(SEGMENT.check2) {
163+
sourcecolor = strip.getPixelColor(i + sourcesegment.start); // read from global buffer (reads the last rendered frame)
164+
}
165+
else {
166+
sourcesegment.setDrawDimensions(); // set to source segment dimensions
167+
sourcecolor = sourcesegment.getPixelColor(i);
168+
}
169+
destcolor = adjust_color(sourcecolor, SEGMENT.intensity, SEGMENT.custom1, SEGMENT.custom2);
170+
SEGMENT.setDrawDimensions(); // reset to current segment dimensions
171+
SEGMENT.setPixelColor(i, destcolor);
172+
}
173+
}
174+
}
175+
return FRAMETIME;
176+
}
177+
static const char _data_FX_MODE_COPY[] PROGMEM = "Copy Segment@,Color shift,Lighten,Brighten,ID,Axis(2D),FullStack(last frame);;;12;ix=0,c1=0,c2=0,c3=0";
178+
128179

129180
/*
130181
* Blink/strobe function
@@ -10617,6 +10668,7 @@ void WS2812FX::setupEffectData() {
1061710668
_modeData.push_back(_data_RESERVED);
1061810669
}
1061910670
// now replace all pre-allocated effects
10671+
addEffect(FX_MODE_COPY, &mode_copy_segment, _data_FX_MODE_COPY);
1062010672
// --- 1D non-audio effects ---
1062110673
addEffect(FX_MODE_BLINK, &mode_blink, _data_FX_MODE_BLINK);
1062210674
addEffect(FX_MODE_BREATH, &mode_breath, _data_FX_MODE_BREATH);

wled00/FX.h

100755100644
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ extern byte realtimeMode; // used in getMappedPixelIndex()
228228
#define FX_MODE_LAKE 75
229229
#define FX_MODE_METEOR 76
230230
//#define FX_MODE_METEOR_SMOOTH 77 // replaced by Meteor
231+
#define FX_MODE_COPY 77
231232
#define FX_MODE_RAILWAY 78
232233
#define FX_MODE_RIPPLE 79
233234
#define FX_MODE_TWINKLEFOX 80

wled00/colors.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ uint32_t color_fade(uint32_t c1, uint8_t amount, bool video)
8686
return scaledcolor;
8787
}
8888

89+
/*
90+
* color adjustment in HSV color space (converts RGB to HSV and back), color conversions are not 100% accurate!
91+
shifts hue, increase brightness, decreases saturation (if not black)
92+
note: inputs are 32bit to speed up the function, useful input value ranges are 0-255
93+
*/
94+
uint32_t adjust_color(uint32_t rgb, uint32_t hueShift, uint32_t lighten, uint32_t brighten) {
95+
if(rgb == 0 | hueShift + lighten + brighten == 0) return rgb; // black or no change
96+
CHSV32 hsv;
97+
rgb2hsv(rgb, hsv); //convert to HSV
98+
hsv.h += (hueShift << 8); // shift hue (hue is 16 bits)
99+
hsv.s = max((int32_t)0, (int32_t)hsv.s - (int32_t)lighten); // desaturate
100+
hsv.v = min((uint32_t)255, (uint32_t)hsv.v + brighten); // increase brightness
101+
uint32_t rgb_adjusted;
102+
hsv2rgb(hsv, rgb_adjusted); // convert back to RGB TODO: make this into 16 bit conversion
103+
return rgb_adjusted;
104+
}
105+
89106
// 1:1 replacement of fastled function optimized for ESP, slightly faster, more accurate and uses less flash (~ -200bytes)
90107
uint32_t ColorFromPaletteWLED(const CRGBPalette16& pal, unsigned index, uint8_t brightness, TBlendType blendType)
91108
{

wled00/fcn_declare.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ class NeoGammaWLEDMethod {
174174
inline uint32_t color_blend16(uint32_t c1, uint32_t c2, uint16_t b) { return color_blend(c1, c2, b >> 8); };
175175
[[gnu::hot, gnu::pure]] uint32_t color_add(uint32_t, uint32_t, bool preserveCR = false);
176176
[[gnu::hot, gnu::pure]] uint32_t color_fade(uint32_t c1, uint8_t amount, bool video=false);
177+
[[gnu::hot, gnu::pure]] uint32_t adjust_color(uint32_t rgb, uint32_t hueShift, uint32_t lighten, uint32_t brighten);
177178
[[gnu::hot, gnu::pure]] uint32_t ColorFromPaletteWLED(const CRGBPalette16 &pal, unsigned index, uint8_t brightness = (uint8_t)255U, TBlendType blendType = LINEARBLEND);
178179
CRGBPalette16 generateHarmonicRandomPalette(const CRGBPalette16 &basepalette);
179180
CRGBPalette16 generateRandomPalette();

0 commit comments

Comments
 (0)