feat(strands): add color() support for beginner-friendly color inputs#8822
feat(strands): add color() support for beginner-friendly color inputs#8822LalitNarayanYadav wants to merge 5 commits into
Conversation
| width: { typeInfo: DataType.float1, get: p => p.width }, | ||
| height: { typeInfo: DataType.float1, get: p => p.height }, | ||
| mouseX: { typeInfo: DataType.float1, get: p => p.mouseX }, | ||
| mouseY: { typeInfo: DataType.float1, get: p => p.mouseY }, | ||
| pmouseX: { typeInfo: DataType.float1, get: p => p.pmouseX }, | ||
| pmouseY: { typeInfo: DataType.float1, get: p => p.pmouseY }, | ||
| winMouseX: { typeInfo: DataType.float1, get: p => p.winMouseX }, | ||
| winMouseY: { typeInfo: DataType.float1, get: p => p.winMouseY }, | ||
| pwinMouseX: { typeInfo: DataType.float1, get: p => p.pwinMouseX }, | ||
| pwinMouseY: { typeInfo: DataType.float1, get: p => p.pwinMouseY }, | ||
| frameCount: { typeInfo: DataType.float1, get: p => p.frameCount }, |
There was a problem hiding this comment.
Do you know why we’re seeing so many diffs?
There was a problem hiding this comment.
The extra diffs are formatting-only changes (whitespace/trailing commas) applied automatically by npx eslint --fix. They don't affect functionality. Happy to squash these into a separate cleanup commit or revert them if you'd prefer the PR to only touch the color-related lines.
There was a problem hiding this comment.
Hi @LalitNarayanYadav ! Please only include changes in the PR related to the color-related updates, for easier review. Please revert the cleanup changes
There was a problem hiding this comment.
Hi @ksen0 ! I've reverted all the formatting changes, the PR now only contains the color-related additions. Sorry for the noise, and thanks for the patience!
2ea0678 to
6684e17
Compare
davepagurek
left a comment
There was a problem hiding this comment.
This is a good start, thanks for working on this! I left a few comments, and then additionally do you think we could add unit or visual tests that make use of all the new methods in strands?
| }); | ||
|
|
||
| // HSB/HSL accessors — inject conversion helpers and extract channel | ||
| const _hsbSnippet = `vec3 _p5_rgb2hsb(vec3 c) { |
There was a problem hiding this comment.
This is GLSL-specific. Is it possible to either build this out of existing functionality, similar to what you were doing with paletteLerp in #8817? Otherwise we'd need to move these snippets into the glsl and wgsl backends so that we can have a version for either, which is why it's a little less work for each backend if we can make them work without a snippet 🙂
| // Reuse p5's parser - handles hex strings, rgb(), CSS named colors, numerics | ||
| const c = originalColor.apply(this, args); | ||
| // _getRGBA() returns [r, g, b, a] normalized to 0-1 | ||
| const rgba = c._getRGBA(); |
| const c = originalColor.apply(this, args); | ||
| // _getRGBA() returns [r, g, b, a] normalized to 0-1 | ||
| const rgba = c._getRGBA(); | ||
| const { id, dimension } = build.primitiveConstructorNode( |
There was a problem hiding this comment.
Would return p5.strandsNode(rgba) be equivalent to this? if so it might be a little better at reusing existing functionality.
|
Thanks for the review Dave! I'll: Simplify Quick question on point 2 - should I build the RGB→HSL/HSB conversion purely from existing strands ops ( |
Part of the Open Source Software Microgrant: beginner-friendly p5.strands API for more p5-like color support.
Changes
Adds beginner-friendly p5.js-style color support inside the strands context, so users can work with familiar color APIs in shaders instead of raw
vec4literals.color()- parse any p5.js color input into avec4Previously, calling
color()inside amodify()block returned ap5.Colorobject, unusable in a GPU shader context. This PR overridescolor()instrands_api.jsto return avec4strands node instead.Supported input formats (all handled by p5's existing
colorjs.ioparser):color('#ff0000'),color('#f00')color('red'),color('cornflowerblue')rgb()/rgba()strings:color('rgb(255, 0, 0)')color(255, 0, 0),color(128)(grayscale)How it works:
p5.color()to parse the input, no new parsing logic._getRGBA()on the result to get[r, g, b, a]normalized to 0–1vec4strands node viaprimitiveConstructorNodelerpColor(c1, c2, amt)- interpolate between two colorsMaps directly to GLSL
mix()in the strands context, since colors arevec4s. Falls through to normal p5 behavior outside strands.red(),green(),blue(),alpha()- component accessorsExtract scalar RGBA channels from a
vec4color node via.x/.y/.z/.wswizzles.hue(),saturation(),brightness(),lightness()- HSL/HSB accessorsInject GLSL helper functions (
_p5_rgb2hsb,_p5_rgb2hsl) into the shader declarations on first use, then extract the relevant channel from the convertedvec3. Helpers are injected into vertex, fragment, and compute declarations.Outside a strands context, all functions fall through to normal p5 behavior.
Example
Screenshots
N/A - shader/transpiler change with no visual diff.
PR Checklist
npm run lintpasses