Skip to content

Smooth fade for HMTL Sparkle effect - #1

Merged
aphelps-claude-bot merged 1 commit into
mainfrom
sparkle-smooth-fade
Apr 20, 2026
Merged

aphelps-claude-bot merged 1 commit into
mainfrom
sparkle-smooth-fade

Conversation

@aphelps-claude-bot

Copy link
Copy Markdown
Collaborator

Summary

Pixels now blend toward their target color each frame instead of snapping
instantly. A new Fade Speed slider (c2, default 32) controls blend step per
frame: low = slow dissolve, high = snap.

Internally allocates a uint32_t targets[] array in SEGENV.data; dice rolls
set targets each cycle tick, blendPixelColor() advances each pixel every
FRAMETIME. Existing Speed / Intensity / BG Reset / Color 2 / Palette
parameters unchanged.

Parent repo PR

Part of aphelps/WLED_dev#3

🤖 Generated with Claude Code

Comment thread usermods/ampworks/ampworks.cpp Resolved
aphelps added a commit to aphelps/WLED_dev that referenced this pull request Apr 18, 2026
Points to 9ddfccc7 which adds smooth per-pixel fade to the HMTL
Sparkle effect via SEGENV.data target array + blendPixelColor.

Submodule PR: aphelps/WLED#1

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fade semantics inverted — c2=0 now gives instant snap (blend step=255), higher values slow the fade (fade_step = 255 - c2, floored at 1 at c2=255). Default updated from 32 to 200 for a visible fade out of the box.

Re the ampworks CI failure: pre-existing issue — the build references the MPR121 library at an absolute local path (/Users/amp/Dropbox/Arduino/ArduinoLibs/MPR121) that CI runners can't resolve. Unrelated to this change.

@aphelps aphelps left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the comment, the range of the slider isn't very useful. The middle of the values seem almost instant. It would be good to expand the upper range to provide a better sliding value for slow fades, while compressing the lower range which seem mostly instant to human eye. I think this could be solved with some sort of logarythmic function.

Comment thread usermods/ampworks/ampworks.cpp Resolved

@aphelps-claude-bot aphelps-claude-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re 'values of 254 or 255 result in pixels fading to red or black': with the quadratic curve now in place, c2=254-255 gives fade_step=1 (maximum slowness, ~4 seconds to fully blend). The behaviour seen was likely pixels slowly converging toward their assigned targets — red from the palette, black/background from Color 2. Could you confirm whether this is still a concern with the new curve, or if it was the intended very-slow-fade working correctly?

@aphelps aphelps left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the changes fade values starting at 233 fade to black, at least when he background color includes no red. When I set the background color to pure white it fades to red, as it does for background colors with some red in them

@aphelps aphelps left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After the changes fade values starting at 233 fade to black, at least when he background color includes no red. When I set the background color to pure white it fades to red, as it does for background colors with some red in them

@aphelps
aphelps force-pushed the sparkle-smooth-fade branch from 8c6cee7 to c0c579c Compare April 19, 2026 18:13
@aphelps-claude-bot

Copy link
Copy Markdown
Collaborator Author

Fixed in c0c579c. Root cause: color_blend uses >> 8 (divide by 256) with a 0-255 blend value, so blend=1 contributes target * 1 / 256 per channel — integer-truncates to 0 for all practical channel values. Pixels at or near black never gain enough to advance, sticking permanently at black.

Fix: replaced blendPixelColor with explicit per-channel delta stepping. Each channel moves at most fade_step steps per frame toward its target, tracked in a SparklePixel { target, current } struct in SEGENV.data. This guarantees monotonic convergence at all fade speeds including the slowest (fade_step=1 = ~8.5s full range). The quadratic Fade Speed curve is unchanged.

Comment thread usermods/ampworks/ampworks.cpp Outdated
*/
struct SparklePixel {
uint32_t target;
uint32_t current;

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason for storing "current" here rather than using SEGMENT.setPixelColor()? If its not much of a performance hit its often better to use less allocated memory since the number of pixels could be quite large.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point on memory. Dropped the SparklePixel::current field — getPixelColor returns exactly the raw value last written by setPixelColor with no brightness scaling or transformation applied, so reading it back loses no precision. Back to 4 bytes/pixel.

Comment thread usermods/ampworks/ampworks.cpp Outdated
Comment on lines +240 to +244
// Quadratic curve: c2=0 → instant snap (255), c2=255 → very slow (1).
// (255-c2)²/255 compresses the fast end and expands the slow end so the
// full slider range is perceptually useful. Integer-only, no powf.
uint8_t inv = 255 - SEGMENT.custom2;
uint8_t fade_step = (uint8_t)max(1u, (uint32_t)inv * inv / 255u);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even with this the slow end is still overly compressed, can you further stretch out that end of the value? I'd never need to use the fastest 1/3 of the values as they're close enough to instant to not matter.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Switched to cubic curve (inv³/255²). Comparison at key points:

c2 quadratic fade_step cubic fade_step
85 113 (~2 frames) 75 (~3 frames)
128 63 (~4 frames) 31 (~8 frames)
170 28 (~9 frames) 9 (~28 frames)
200 11 (~23 frames) 2 (~128 frames)
212 5 (~51 frames) 1 (~8.5 s)
233 1 (~8.5 s) 1

Slow-end resolution improves meaningfully: fade_step=1 now starts at c2≈212 rather than c2≈233, giving ~43 more slider positions in the interesting slow range. Let me know if you want it stretched further.

Add per-pixel target tracking to the HMTL Sparkle effect. Each pixel
independently rolls each cycle to become a palette sparkle or reset to
the background color (Color 2). Pixels then fade toward their target
using per-channel delta stepping.

Fade Speed (c2) uses a cubic curve (inv³/255²) so the full slider range
is perceptually useful: 0=instant snap, 255=very slow dissolve (~8.5s
full range at 30fps, fade_step=1 from c2≈212). The cubic gives more
resolution in the slow end than the earlier quadratic.

Channel-delta stepping guarantees monotonic convergence at all fade
speeds. Current color is read back via getPixelColor (returns the raw
set value) eliminating the need for a separate per-pixel current-state
buffer and keeping data at 4 bytes/pixel.
@aphelps
aphelps force-pushed the sparkle-smooth-fade branch from c0c579c to 21e95d8 Compare April 20, 2026 05:22
@aphelps-claude-bot
aphelps-claude-bot merged commit 8a77edd into main Apr 20, 2026
6 of 8 checks passed
@aphelps-claude-bot
aphelps-claude-bot deleted the sparkle-smooth-fade branch April 20, 2026 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants