-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat(strands): add color() support for beginner-friendly color inputs #8822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
LalitNarayanYadav
wants to merge
9
commits into
processing:dev-2.0
Choose a base branch
from
LalitNarayanYadav:color-support-strands
base: dev-2.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+246
−1
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ad2ffcd
feat(strands): add color() support for beginner-friendly color inputs
LalitNarayanYadav 3df6b4a
feat(strands): add lerpColor() support mapping to GLSL mix()
LalitNarayanYadav 4c63088
feat(strands): add red(), green(), blue(), alpha() component accessors
LalitNarayanYadav ddc668a
feat(strands): add hue(), saturation(), brightness(), lightness() acc…
LalitNarayanYadav 6684e17
feat(strands): add beginner-friendly color support in p5.strands
LalitNarayanYadav 6b641dc
feat(strands): rewrite HSB/HSL accessors using backend-agnostic stran…
LalitNarayanYadav ebc7fd5
feat(strands): fix HSB/HSL helpers to use instance methods and add fo…
LalitNarayanYadav b5955f1
docs(color): add p5.strands behavior note to color() JSDoc
LalitNarayanYadav 2004c69
add test file
LalitNarayanYadav File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -289,7 +289,7 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) { | |
| const originalLerp = fn.lerp; | ||
| augmentFn(fn, p5, 'lerp', function (...args) { | ||
| if (strandsContext.active) { | ||
| return fn.mix(...args); | ||
| return this.mix(...args); | ||
| } else { | ||
| return originalLerp.apply(this, args); | ||
| } | ||
|
|
@@ -312,6 +312,139 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) { | |
| return result; | ||
| }); | ||
|
|
||
| const originalColor = fn.color; | ||
| augmentFn(fn, p5, 'color', function (...args) { | ||
| if (!strandsContext.active) { | ||
| return originalColor.apply(this, args); | ||
| } | ||
| // 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 { id, dimension } = build.primitiveConstructorNode( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would |
||
| strandsContext, | ||
| { baseType: BaseType.FLOAT, dimension: null }, | ||
| rgba | ||
| ); | ||
| return createStrandsNode(id, dimension, strandsContext); | ||
| }); | ||
| const originalLerpColor = fn.lerpColor; | ||
| augmentFn(fn, p5, 'lerpColor', function (...args) { | ||
| if (!strandsContext.active) { | ||
| return originalLerpColor.apply(this, args); | ||
| } | ||
| // In strands, colors are vec4s - lerpColor maps directly to GLSL mix() | ||
| return this.mix(...args); | ||
| }); | ||
| // Component accessors: extract scalar channels from a vec4 color | ||
| const originalRed = fn.red; | ||
| augmentFn(fn, p5, 'red', function (...args) { | ||
| if (!strandsContext.active) { | ||
| return originalRed.apply(this, args); | ||
| } | ||
| return p5.strandsNode(args[0]).x; | ||
| }); | ||
|
|
||
| const originalGreen = fn.green; | ||
| augmentFn(fn, p5, 'green', function (...args) { | ||
| if (!strandsContext.active) { | ||
| return originalGreen.apply(this, args); | ||
| } | ||
| return p5.strandsNode(args[0]).y; | ||
| }); | ||
|
|
||
| const originalBlue = fn.blue; | ||
| augmentFn(fn, p5, 'blue', function (...args) { | ||
| if (!strandsContext.active) { | ||
| return originalBlue.apply(this, args); | ||
| } | ||
| return p5.strandsNode(args[0]).z; | ||
| }); | ||
|
|
||
| const originalAlpha = fn.alpha; | ||
| augmentFn(fn, p5, 'alpha', function (...args) { | ||
| if (!strandsContext.active) { | ||
| return originalAlpha.apply(this, args); | ||
| } | ||
| return p5.strandsNode(args[0]).w; | ||
| }); | ||
|
|
||
| // RGB to HSB conversion based on: | ||
| // https://en.wikipedia.org/wiki/HSL_and_HSV#From_RGB | ||
| // Using mix/step to avoid branching, via the compact form from: | ||
| // http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl | ||
| const _rgb2hsb = (instance, colorNode) => { | ||
| const r = colorNode.x; | ||
| const g = colorNode.y; | ||
| const b = colorNode.z; | ||
| const K = instance.vec4(0, -1/3, 2/3, -1); | ||
| const p = instance.mix( | ||
| instance.vec4(b, g, K.w, K.z), | ||
| instance.vec4(g, b, K.x, K.y), | ||
| instance.step(b, g) | ||
| ); | ||
| const q = instance.mix( | ||
| instance.vec4(p.x, p.y, p.w, r), | ||
| instance.vec4(r, p.y, p.z, p.x), | ||
| instance.step(p.x, r) | ||
| ); | ||
| const d = q.x.sub(instance.min(q.w, q.y)); | ||
| const e = p5.strandsNode(1e-10); | ||
| const h = instance.abs(q.z.add(q.w.sub(q.y).div(d.mult(6).add(e)))); | ||
| const s = d.div(q.x.add(e)); | ||
| return instance.vec3(h, s, q.x); | ||
| }; | ||
|
|
||
| const _rgb2hsl = (instance, colorNode) => { | ||
| const r = colorNode.x; | ||
| const g = colorNode.y; | ||
| const b = colorNode.z; | ||
| const maxC = instance.max(r, instance.max(g, b)); | ||
| const minC = instance.min(r, instance.min(g, b)); | ||
| const l = maxC.add(minC).div(2); | ||
| const d = maxC.sub(minC); | ||
| const e = p5.strandsNode(1e-10); | ||
| const s = instance.mix( | ||
| p5.strandsNode(0), | ||
| d.div(p5.strandsNode(1).sub(instance.abs(l.mult(2).sub(1)))), | ||
| instance.step(e, d) | ||
| ); | ||
| const h_rg = instance.mod(g.sub(b).div(d.add(e)), p5.strandsNode(6)).div(6); | ||
| const h_gb = b.sub(r).div(d.add(e)).add(2).div(6); | ||
| const h_br = r.sub(g).div(d.add(e)).add(4).div(6); | ||
| const isR = instance.step(maxC.sub(e), r).mult(instance.step(r.sub(e), maxC)); | ||
| const isG = instance.step(maxC.sub(e), g).mult(instance.step(g.sub(e), maxC)); | ||
| const h = instance.mix(instance.mix(h_br, h_gb, isG), h_rg, isR); | ||
| return instance.vec3(h, s, l); | ||
| }; | ||
| const originalHue = fn.hue; | ||
| augmentFn(fn, p5, 'hue', function (...args) { | ||
| if (!strandsContext.active) return originalHue.apply(this, args); | ||
| const colorNode = p5.strandsNode(args[0]); | ||
| return _rgb2hsl(this, this.vec3(colorNode.x, colorNode.y, colorNode.z)).x; | ||
| }); | ||
|
|
||
| const originalSaturation = fn.saturation; | ||
| augmentFn(fn, p5, 'saturation', function (...args) { | ||
| if (!strandsContext.active) return originalSaturation.apply(this, args); | ||
| const colorNode = p5.strandsNode(args[0]); | ||
| return _rgb2hsl(this, this.vec3(colorNode.x, colorNode.y, colorNode.z)).y; | ||
| }); | ||
|
|
||
| const originalBrightness = fn.brightness; | ||
| augmentFn(fn, p5, 'brightness', function (...args) { | ||
| if (!strandsContext.active) return originalBrightness.apply(this, args); | ||
| const colorNode = p5.strandsNode(args[0]); | ||
| return _rgb2hsb(this, this.vec3(colorNode.x, colorNode.y, colorNode.z)).z; | ||
| }); | ||
|
|
||
| const originalLightness = fn.lightness; | ||
| augmentFn(fn, p5, 'lightness', function (...args) { | ||
| if (!strandsContext.active) return originalLightness.apply(this, args); | ||
| const colorNode = p5.strandsNode(args[0]); | ||
| return _rgb2hsl(this, this.vec3(colorNode.x, colorNode.y, colorNode.z)).z; | ||
| }); | ||
|
|
||
| augmentFn(fn, p5, 'getTexture', function (...rawArgs) { | ||
| if (strandsContext.active) { | ||
| const { id, dimension } = strandsContext.backend.createGetTextureCall(strandsContext, rawArgs); | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice!