p5.js icon indicating copy to clipboard operation
p5.js copied to clipboard

Add p5.strands documentation and examples for color functions

Open LalitNarayanYadav opened this issue 5 months ago • 5 comments

Increasing access

Beginners using p5.strands need to know that color functions behave differently inside shader callbacks. Without strands-specific examples in the docs, it's not obvious how to use these functions in a modify() block.

Most appropriate sub-area of p5.js?

  • [ ] Accessibility
  • [x] Color
  • [ ] Core/Environment/Rendering
  • [ ] Data
  • [ ] DOM
  • [ ] Events
  • [ ] Image
  • [ ] IO
  • [ ] Math
  • [ ] Typography
  • [ ] Utilities
  • [ ] WebGL
  • [ ] Build process
  • [ ] Unit testing
  • [ ] Internationalization
  • [ ] Friendly errors
  • [x] Other (specify if possible)

Feature enhancement details

Now that color(), lerpColor(), red(), green(), blue(), alpha(), hue(), saturation(), brightness(), and lightness() work inside p5.strands shader callbacks (added in #8822), the documentation should include strands-specific examples showing how to use these functions in a modify() callback.

Key things to document:

  • In strands, color() returns a vec4 instead of a p5.Color object
  • All color values are represented as normalized RGBA values in the 0–1 range
  • colorMode() has no effect inside strands callbacks
  • Common usage patterns for lerpColor(), color channel accessors, and HSL/HSB accessors in shader code

Potential follow-up documentation work:

  • Update existing p5.strands examples that currently use raw color vectors where the new color() API would improve readability
  • Add example sketches demonstrating hex strings, CSS color names, and rgb()/rgba() inputs inside strands
  • Review existing strands examples and helper functions for opportunities to showcase the newly added color APIs

Related PR: #8822

LalitNarayanYadav avatar Jun 12 '26 19:06 LalitNarayanYadav

Hi @LalitNarayanYadav,

I'd like to work on this documentation enhancement.

I can help add p5.strands-specific examples for color(), lerpColor(), color channel accessors, and HSL/HSB functions inside modify() callbacks, along with notes about normalized RGBA values and colorMode() behavior.

Please let me know if there are any specific documentation files or examples you would like updated.

Thank you!

Yashisinghal285 avatar Jun 12 '26 19:06 Yashisinghal285

This seems especially valuable for beginners because p5.strands looks like normal p5 code until shader-callback constraints show up. The docs could use one side-by-side example: regular color()/lerpColor() use in sketch code, then the equivalent pattern inside modify() with any normalization or type restrictions called out. That would make the difference visible without requiring users to infer it from shader errors.

hiSandog avatar Jun 27 '26 16:06 hiSandog

Thanks for the interest! @Yashisinghal285 @hiSandog . Feel free to collaborate on this. The main file to update is src/color/creating_reading.js where the JSDoc for each color function lives. The strands note I added to color() in PR #8822 can serve as a template for the other functions. The side-by-side example idea is great too! @davepagurek and @ksen0 what do you think?

LalitNarayanYadav avatar Jul 01 '26 09:07 LalitNarayanYadav

Hi @Yashisinghal285 thanks for volunteering! I'll assign to you as you volunteered first, so it's yours if you're still interested. @LalitNarayanYadav if you have capacity to help review, that would be really helpful. Please review the style guide caefully: examples should be minimal, focused, and well-explained.

If @Yashisinghal285 doesn't respond by end of July 9th, then @hiSandog you can take it, but we'll go in order, thanks for understanding!

ksen0 avatar Jul 02 '26 07:07 ksen0

Thanks @ksen0! Happy to help review when @Yashisinghal285 opens a PR.

@Yashisinghal285, a few pointers to get started:

File to edit

  • src/color/creating_reading.js - each color function has a JSDoc block above it.

Template to follow

  • Look at the strands note I added to color() in PR #8822.
  • It's a short paragraph added before @method.
  • The same pattern applies to:
    • lerpColor()
    • red()
    • green()
    • blue()
    • alpha()
    • hue()
    • saturation()
    • brightness()
    • lightness()

Key things to mention for each function

  • In strands, the function operates on vec4 values with components in the 0–1 range.
  • colorMode() has no effect inside strands callbacks.

Examples

  • Follow the style guide- keep examples minimal and focused.
  • A simple sketch showing the function used inside a modify() callback with a baseMaterialShader() is enough.
  • The side-by-side idea from @hiSandog is great if it stays concise.

Feel free to tag me when you open a draft PR, and I can give feedback on whether the strands behavior description is accurate.

LalitNarayanYadav avatar Jul 02 '26 09:07 LalitNarayanYadav