Skip to content

Conversation

AlexeyMal
Copy link

@AlexeyMal AlexeyMal commented Oct 11, 2025

Resolves my feature fequest
#4996

Dear Developers,

this pull request adds the option to set Random colors via JSON API in Segment object like col=["r","r","r"].
As an alternative to setting the colors to the fix values.
Similar to the already impleneted "r" for palette pal and effect fx.

Why? Many people scoll through all effects and palettes using a playlist with one random effect and palette. But some effects use the 1 to 3 colors instead of the palette, so for them the colors can not be set randomly.

Best regards

Summary by CodeRabbit

  • New Features
    • Per-segment color settings via the JSON API now accept the single-character string "r" to assign a random RGBW color. This complements existing color formats (Kelvin, hex, object/array) and enables quick, dynamic color selection without specifying exact values.

Copy link
Contributor

coderabbitai bot commented Oct 11, 2025

Walkthrough

DeserializeSegment in wled00/json.cpp gained handling for a color value of the single-character string "r", which triggers setRandomColor(brgbw) and applies that RGBW to the segment color; other color parsing paths remain unchanged.

Changes

Cohort / File(s) Summary of changes
Segment color deserialization
wled00/json.cpp
In deserializeSegment, added handling for a single-character string "r" to call setRandomColor(brgbw), mark the color valid, and apply the resulting RGBW to the segment; existing Kelvin, hex, object, and array parsing branches are unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title explicitly describes the addition of random color support via the JSON API in segment objects and directly reflects the PR’s main objective. Including the example usage and issue number does not obscure clarity and the phrase remains a single, coherent sentence summarizing the primary change. It is fully related to the changeset and provides enough context for maintainers to understand the purpose at a glance.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7f1f986 and 4e9d956.

📒 Files selected for processing (1)
  • wled00/colors.cpp (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
wled00/**/*.cpp

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Use 2-space indentation for C++ source files (.cpp)

Files:

  • wled00/colors.cpp

Comment on lines 495 to 498
if (in[0] == 'r') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
setRandomColor(rgb);
return true;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

🧩 Analysis chain

Tighten the string comparison to match exactly "r".

The current check if (in[0] == 'r') is overly permissive—it treats any string starting with 'r' (e.g., "random", "red", "r12345") as a request for random colors. This could cause unintended behavior if a user passes a malformed hex string or expects different handling.

Apply this diff to ensure only the exact string "r" triggers random color generation:

- if (in[0] == 'r') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
+ if (in[0] == 'r' && in[1] == '\0') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
    setRandomColor(rgb);
    return true;
  }

Additionally, verify that this pattern is consistent with how "r" is handled for palette and effect settings elsewhere in the codebase.

Run the following script to check how "r" shorthand is implemented for palette (pal) and effect (fx) settings:


🏁 Script executed:

#!/bin/bash
# Description: Find how "r" shorthand is handled for palette and effect settings
# to ensure consistency across the codebase.

# Search for palette randomization logic
echo "=== Palette randomization patterns ==="
rg -n -C3 'pal.*==.*["\']r["\']' --type=cpp

# Search for effect randomization logic  
echo -e "\n=== Effect randomization patterns ==="
rg -n -C3 'fx.*==.*["\']r["\']' --type=cpp

# Search for other "r" string comparisons that might set random values
echo -e "\n=== Other 'r' comparisons for randomization ==="
rg -n -C3 '\[0\]\s*==\s*["\']r["\']' --type=cpp

Length of output: 108


🏁 Script executed:

#!/bin/bash
# Search for palette randomization logic
echo "=== Palette randomization patterns ==="
rg -n -C3 "pal.*==.*['\"]r['\"]" --type=cpp

# Search for effect randomization logic  
echo -e "\n=== Effect randomization patterns ==="
rg -n -C3 "fx.*==.*['\"]r['\"]" --type=cpp

# Search for other "[0] == 'r'" comparisons
echo -e "\n=== Other 'r' comparisons for randomization ==="
rg -n -C3 "\[0\]\s*==\s*['\"]r['\"]" --type=cpp

Length of output: 1086


Restrict random‐color shorthand to exact "r".
The current if (in[0] == 'r') matches any string starting with 'r' (e.g., "red", "random"). Only the lone "r" should trigger random colors. Apply:

- if (in[0] == 'r') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
+ if (in[0] == 'r' && in[1] == '\0') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
    setRandomColor(rgb);
    return true;
  }

No other "r" shorthand cases were found in palette or effect code.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
if (in[0] == 'r') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
setRandomColor(rgb);
return true;
}
if (in[0] == 'r' && in[1] == '\0') { // Random colors via JSON API in Segment object like col=["r","r","r"] · Issue #4996
setRandomColor(rgb);
return true;
}
🤖 Prompt for AI Agents
In wled00/colors.cpp around lines 495 to 498, the condition uses a prefix check
(in[0] == 'r') which matches strings like "red" or "random"; change it to
require the input be exactly the single-character string "r" (e.g., check that
in[0] == 'r' and the next character is the string terminator or that strlen(in)
== 1, or use strcmp/interned-string equality) so only the lone "r" triggers
setRandomColor and returns true.

Copy link
Collaborator

@blazoncek blazoncek left a comment

Choose a reason for hiding this comment

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

Not the correct place for this behaviour. Do not hijack colorFromHexString().
Please add random handling into json.cpp where it belongs..

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4e9d956 and 3e2e3c9.

📒 Files selected for processing (1)
  • wled00/json.cpp (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
wled00/**/*.cpp

📄 CodeRabbit inference engine (.github/copilot-instructions.md)

Use 2-space indentation for C++ source files (.cpp)

Files:

  • wled00/json.cpp
🧬 Code graph analysis (1)
wled00/json.cpp (1)
wled00/colors.cpp (2)
  • setRandomColor (134-138)
  • setRandomColor (134-134)

@AlexeyMal AlexeyMal requested a review from blazoncek October 11, 2025 20:49
@DedeHai
Copy link
Collaborator

DedeHai commented Oct 11, 2025

Thanks for contributing. What happens if I want [255, 0, r]?

@AlexeyMal
Copy link
Author

AlexeyMal commented Oct 11, 2025

Thanks for contributing. What happens if I want [255, 0, r]?

Hello DedeHai,
thank you too for your efforts developing WLED!
Every color in col array is processed independently, so it is possible to use different formats for different colors (Primary, Secondary and Tetrary). E. g. col:["r",[0,0,0],"r"] or col:[[255,255,255],[0,0,0],"r"] will work, keeping the Secondary color black. Setting just single components (R/G/B) of one color randomly will not work, this was not my intention.
I think the random functionality makes sense with that behaviour.
Have a nice weekend,
best regards, Alexey

@AlexeyMal AlexeyMal changed the title Random colors via JSON API in Segment object like col=["r","r","r"] #4996 Random colors via JSON API in Segment object like col:["r","r","r"] #4996 Oct 12, 2025
Copy link
Collaborator

@blazoncek blazoncek left a comment

Choose a reason for hiding this comment

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

I've implemented the idea slightly differently.

See: d4afe5b

@AlexeyMal
Copy link
Author

Hello Blaz,
thanks for your implementation and for taking care of this feature request!
Your commit is really fine! It provides the desired functionality.
Can you push it to wled master branch?
Then I will withdraw my pull request and close the issue.
Best regards, Alexey

@AlexeyMal AlexeyMal changed the title Random colors via JSON API in Segment object like col:["r","r","r"] #4996 Random colors via JSON API in Segment object like "col":["r","r","r"] #4996 Oct 12, 2025
@blazoncek
Copy link
Collaborator

Can you push it to wled master branch?

I would not like to steal credit from you.
This PR's code is ok as far as functionality goes (though you could add a few comments) and it is a matter of personal preference for my implementation.

@AlexeyMal
Copy link
Author

Hello Blaz,
it is no problem for me if you push your implementation instead of pulling mine.
Otherwise you would get a merge conflict later. And your implementation is a bit more comprehensive.
My credit will be the desired functionality in the next release 0.16.0. I hope you’ll manage to release it before Christmas :)
The head (web UI) was not responsive on my esp32 today, connection errors all the time. So I think you have bigger issues to care about.
Best regards, Alexey

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.

3 participants