From c6f6c3f54bb41481c4e39d4f54a264607265afd4 Mon Sep 17 00:00:00 2001 From: Akshat Date: Tue, 1 Dec 2026 01:15:41 +0530 Subject: [PATCH 1/3] Make colorMode() part of the p5.strands state Calling colorMode() inside a shader now only affects that shader, not the whole sketch. Part of #9432. --- src/strands/p5.strands.js | 6 ++ src/strands/strands_api.js | 64 ++++++++++++++++++++- test/unit/webgl/p5.Shader.js | 106 +++++++++++++++++++++++++++++++++++ 3 files changed, 175 insertions(+), 1 deletion(-) diff --git a/src/strands/p5.strands.js b/src/strands/p5.strands.js index a4c47bba53..ef2bdafad4 100644 --- a/src/strands/p5.strands.js +++ b/src/strands/p5.strands.js @@ -60,6 +60,8 @@ function strands(p5, fn) { * @property {Array} tempProps Temporary property descriptors, restored newest first. * @property {Number} _noiseOctaves Noise octave override captured by `noiseDetail()`. * @property {Number} _noiseAmpFalloff Noise falloff override captured by `noiseDetail()`. + * @property {String} _colorMode Color mode override captured by `colorMode()`. + * @property {Object} _colorMaxes Color range overrides captured by `colorMode()`. * @property {Number} _randomSeed Random seed override captured by `randomSeed()`. * @property {Object} _builtinGlobals Cache of builtin-global uniform nodes for the current DAG. * @property {Map} sharedVariables Shared variable metadata that tracks vertex/fragment usage to decide whether each variable becomes a local declaration or a varying. @@ -131,6 +133,8 @@ function strands(p5, fn) { ctx.tempProps = []; ctx._noiseOctaves = null; ctx._noiseAmpFalloff = null; + ctx._colorMode = null; + ctx._colorMaxes = null; ctx._randomSeed = null; ctx._builtinGlobals = createBuiltinGlobalsCache(ctx.dag); ctx.sharedVariables = new Map(); @@ -174,6 +178,8 @@ function strands(p5, fn) { ctx.tempProps = []; ctx._noiseOctaves = null; ctx._noiseAmpFalloff = null; + ctx._colorMode = null; + ctx._colorMaxes = null; ctx._randomSeed = null; ctx._builtinGlobals = createBuiltinGlobalsCache(ctx.dag); ctx.sharedVariables = new Map(); diff --git a/src/strands/strands_api.js b/src/strands/strands_api.js index 6b26b54ebf..30b9bd62d7 100644 --- a/src/strands/strands_api.js +++ b/src/strands/strands_api.js @@ -27,6 +27,18 @@ import { isReservedStrandsName, STRANDS_INTERNAL_NAME_PREFIX } from './strands_names'; +import { Color } from '../color/p5.Color'; +import { + RGB, + RGBP3, + HSB, + HSL, + HWB, + LAB, + LCH, + OKLAB, + OKLCH +} from '../color/color_constants'; const BUILTIN_GLOBAL_SPECS = { width: { typeInfo: DataType.float1, get: p => p.width }, @@ -956,13 +968,63 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) { return result; }); + // Like noiseDetail(), colorMode() in strands only applies to this shader + const getStrandsColorMode = instance => ({ + mode: strandsContext._colorMode ?? instance._renderer.states.colorMode, + maxes: strandsContext._colorMaxes ?? instance._renderer.states.colorMaxes + }); + + const originalColorMode = fn.colorMode; + augmentFn(fn, p5, 'colorMode', function (mode, max1, max2, max3, maxA) { + if (!strandsContext.active) { + return originalColorMode.apply(this, arguments); + } + if ([RGB, RGBP3, HSB, HSL, HWB, LAB, LCH, OKLAB, OKLCH].includes(mode)) { + if ([max1, max2, max3, maxA].some(max => max?.isStrandsNode)) { + FES.userError( + 'type error', + 'colorMode() in p5.strands needs plain numbers for its ranges, not shader values.' + ); + } + // Same as core colorMode(), but saved on the strands context + const colorMaxes = getStrandsColorMode(this).maxes.clone(); + const maxes = colorMaxes[mode]; + if (arguments.length === 2) { + maxes[0] = max1; + maxes[1] = max1; + maxes[2] = max1; + maxes[3] = max1; + } else if (arguments.length === 4) { + maxes[0] = max1; + maxes[1] = max2; + maxes[2] = max3; + } else if (arguments.length === 5) { + maxes[0] = max1; + maxes[1] = max2; + maxes[2] = max3; + maxes[3] = maxA; + } + strandsContext._colorMode = mode; + strandsContext._colorMaxes = colorMaxes; + } + return getStrandsColorMode(this).mode; + }); + 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); + const { mode, maxes } = getStrandsColorMode(this); + const c = args[0] instanceof Color + ? args[0] + : new Color( + Array.isArray(args[0]) ? args[0] : args, + mode, + maxes[mode], + { clamp: true } + ); // _getRGBA() returns [r, g, b, a] normalized to 0-1 const rgba = c._getRGBA(); const { id, dimension } = build.primitiveConstructorNode( diff --git a/test/unit/webgl/p5.Shader.js b/test/unit/webgl/p5.Shader.js index f4dc92b0d5..06030c4bfb 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -1092,6 +1092,112 @@ suite('p5.Shader', function () { assert.approximately(pixelColor[0], 0, 5); }); + suite('colorMode()', () => { + const getCenterPixel = testShader => { + myp5.noStroke(); + myp5.shader(testShader); + myp5.plane(myp5.width, myp5.height); + return myp5.get(25, 25); + }; + + test('color() uses the sketch colorMode when colorMode() is not called in strands', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + myp5.colorMode(myp5.HSB); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.getPixelInputs(inputs => { + inputs.color = myp5.color(240, 100, 100); + return inputs; + }); + }, + { myp5 } + ); + myp5.colorMode(myp5.RGB); + + const pixelColor = getCenterPixel(testShader); + assert.approximately(pixelColor[0], 0, 5); + assert.approximately(pixelColor[1], 0, 5); + assert.approximately(pixelColor[2], 255, 5); + }); + + test('colorMode() in strands changes how color() reads its values', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.colorMode(myp5.HSB, 1); + myp5.getPixelInputs(inputs => { + inputs.color = myp5.color(0.5, 1, 1); + return inputs; + }); + }, + { myp5 } + ); + + const pixelColor = getCenterPixel(testShader); + assert.approximately(pixelColor[0], 0, 5); + assert.approximately(pixelColor[1], 255, 5); + assert.approximately(pixelColor[2], 255, 5); + }); + + test('colorMode() in strands does not change the sketch colorMode', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + myp5.baseMaterialShader().modify( + () => { + myp5.colorMode(myp5.HSB, 1); + myp5.getPixelInputs(inputs => inputs); + }, + { myp5 } + ); + + assert.equal(myp5.colorMode(), myp5.RGB); + assert.deepEqual( + myp5._renderer.states.colorMaxes[myp5.HSB], + [360, 100, 100, 1] + ); + assert.equal(myp5.red(myp5.color(255, 0, 0)), 255); + }); + + test('colorMode() state does not leak across modify calls', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + myp5.baseMaterialShader().modify( + () => { + myp5.colorMode(myp5.HSB); + myp5.getPixelInputs(inputs => inputs); + }, + { myp5 } + ); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.getPixelInputs(inputs => { + // Would be black if this were read as HSB + inputs.color = myp5.color(255, 0, 0); + return inputs; + }); + }, + { myp5 } + ); + + const pixelColor = getCenterPixel(testShader); + assert.approximately(pixelColor[0], 255, 5); + assert.approximately(pixelColor[1], 0, 5); + assert.approximately(pixelColor[2], 0, 5); + }); + + test('colorMode() in strands rejects shader values as ranges', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + expect(() => { + myp5.baseMaterialShader().modify( + () => { + const range = myp5.uniformFloat(() => 1); + myp5.colorMode(myp5.HSB, range); + myp5.getPixelInputs(inputs => inputs); + }, + { myp5 } + ); + }).toThrowError(/colorMode\(\) in p5\.strands needs plain numbers/); + }); + }); + test('handle custom uniform names with automatic values', () => { myp5.createCanvas(50, 50, myp5.WEBGL); const testShader = myp5.baseMaterialShader().modify( From 1ebb5d90979e89fd32d5923c3fbfed13f693e336 Mon Sep 17 00:00:00 2001 From: Akshat Date: Tue, 1 Dec 2026 21:42:50 +0530 Subject: [PATCH 2/3] Make saturation() in p5.strands follow colorMode() In HSB mode it now returns HSB saturation, so it pairs with brightness(). Part of #9432. --- src/strands/strands_api.js | 7 ++++- test/unit/webgl/p5.Shader.js | 55 ++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/src/strands/strands_api.js b/src/strands/strands_api.js index 30b9bd62d7..630344f03e 100644 --- a/src/strands/strands_api.js +++ b/src/strands/strands_api.js @@ -1138,7 +1138,12 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) { 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 rgb = this.vec3(colorNode.x, colorNode.y, colorNode.z); + // HSB saturation in HSB mode so it pairs with brightness(), else HSL + if (getStrandsColorMode(this).mode === HSB) { + return _rgb2hsb(this, rgb).y; + } + return _rgb2hsl(this, rgb).y; }); const originalBrightness = fn.brightness; diff --git a/test/unit/webgl/p5.Shader.js b/test/unit/webgl/p5.Shader.js index 06030c4bfb..79928761c0 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -1196,6 +1196,61 @@ suite('p5.Shader', function () { ); }).toThrowError(/colorMode\(\) in p5\.strands needs plain numbers/); }); + + // #ff8080 has an HSB saturation of 0.5 and an HSL saturation of 1 + test('saturation() returns HSB saturation in HSB mode', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.colorMode(myp5.HSB, 1); + myp5.getPixelInputs(inputs => { + const s = myp5.saturation(myp5.color('#ff8080')); + inputs.color = [s, s, s, 1]; + return inputs; + }); + }, + { myp5 } + ); + + const pixelColor = getCenterPixel(testShader); + assert.approximately(pixelColor[0], 127, 5); + }); + + test('saturation() uses the sketch colorMode when colorMode() is not called in strands', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + myp5.colorMode(myp5.HSB); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.getPixelInputs(inputs => { + const s = myp5.saturation(myp5.color('#ff8080')); + inputs.color = [s, s, s, 1]; + return inputs; + }); + }, + { myp5 } + ); + myp5.colorMode(myp5.RGB); + + const pixelColor = getCenterPixel(testShader); + assert.approximately(pixelColor[0], 127, 5); + }); + + test('saturation() returns HSL saturation outside of HSB mode', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.getPixelInputs(inputs => { + const s = myp5.saturation(myp5.color('#ff8080')); + inputs.color = [s, s, s, 1]; + return inputs; + }); + }, + { myp5 } + ); + + const pixelColor = getCenterPixel(testShader); + assert.approximately(pixelColor[0], 255, 5); + }); }); test('handle custom uniform names with automatic values', () => { From 6d4ec659c111633781e587a4981a22e21aa8abcf Mon Sep 17 00:00:00 2001 From: Akshat Date: Thu, 3 Dec 2026 16:33:26 +0530 Subject: [PATCH 3/3] Let color() in p5.strands build colors from shader values Things like color(hue(c), 1, 1) now work in RGB, HSB and HSL instead of compiling to NaN. Also updates the docs. Closes #9432. --- src/color/creating_reading.js | 62 +++++++++++---- src/color/setting.js | 5 ++ src/strands/strands_api.js | 86 ++++++++++++++++++-- test/unit/webgl/p5.Shader.js | 145 ++++++++++++++++++++++++++++++++++ test/unit/webgpu/p5.Shader.js | 55 +++++++++++++ 5 files changed, 333 insertions(+), 20 deletions(-) diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index f85b6e742a..6551de76fc 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -291,11 +291,14 @@ function creatingReading(p5, fn) { * p5.strands is an experimental mode for writing shader code with p5.js-like * syntax instead of GLSL. In p5.strands shader callbacks, `color()` accepts * the same input formats but returns a `vec4` instead of a `p5.Color` - * object, with RGBA components normalized to the 0–1 range. All colors in - * strands are RGB-based; `colorMode()` has no effect inside shader - * callbacks. Color utility functions such as `red()`, `green()`, `blue()`, - * `alpha()`, `hue()`, `saturation()`, `brightness()`, and `lightness()` - * also return values in the 0–1 range when used in strands. + * object, with RGBA components normalized to the 0–1 range. It follows + * `colorMode()`, and calling `colorMode()` inside a shader callback only + * affects that shader. `color()` also accepts shader values, such as + * `hue(c)` or a uniform, in the `RGB`, `HSB` and `HSL` color modes. Color + * utility functions such as `red()`, `green()`, `blue()`, `alpha()`, + * `hue()`, `saturation()`, `brightness()`, and `lightness()` return values + * in the 0–1 range when used in strands, so `colorMode(HSB, 1)` lets you + * pass them back into `color()`. * * ```js example * let myShader; @@ -322,6 +325,35 @@ function creatingReading(p5, fn) { * } * ``` * + * ```js example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * myShader = buildMaterialShader(myShaderBuilder); + * } + * + * function myShaderBuilder() { + * // Use HSB values from 0 to 1, like hue() returns. + * colorMode(HSB, 1); + * finalColor.begin(); + * let c = finalColor.color; + * // Shift the hue over time. Hues wrap around past 1. + * let h = hue(c) + millis() * 0.0002; + * finalColor.set(color(h, saturation(c), brightness(c), alpha(c))); + * finalColor.end(); + * } + * + * function draw() { + * background(200); + * shader(myShader); + * noStroke(); + * fill(255, 0, 0); + * sphere(40); + * + * describe('A sphere slowly changing through the colors of the rainbow.'); + * } + * ``` + * * @method color * @param {Number} gray number specifying value between white and black. * @param {Number} [alpha] alpha value relative to current color range @@ -491,7 +523,7 @@ function creatingReading(p5, fn) { * * In p5.strands shader callbacks, `red()` operates on `vec4` values and * returns the red channel as a normalized value in the 0–1 range. - * `colorMode()` has no effect inside shader callbacks. + * The range doesn't change with `colorMode()`. * * ```js example * let myShader; @@ -644,7 +676,7 @@ function creatingReading(p5, fn) { * * In p5.strands shader callbacks, `green()` operates on `vec4` values and * returns the green channel as a normalized value in the 0–1 range. - * `colorMode()` has no effect inside shader callbacks. + * The range doesn't change with `colorMode()`. * * ```js example * let myShader; @@ -797,7 +829,7 @@ function creatingReading(p5, fn) { * * In p5.strands shader callbacks, `blue()` operates on `vec4` values and * returns the blue channel as a normalized value in the 0–1 range. - * `colorMode()` has no effect inside shader callbacks. + * The range doesn't change with `colorMode()`. * * ```js example * let myShader; @@ -918,7 +950,7 @@ function creatingReading(p5, fn) { * * In p5.strands shader callbacks, `alpha()` operates on `vec4` values and * returns the alpha channel as a normalized value in the 0–1 range. - * `colorMode()` has no effect inside shader callbacks. + * The range doesn't change with `colorMode()`. * * ```js example * let myShader; @@ -1059,7 +1091,7 @@ function creatingReading(p5, fn) { * * In p5.strands shader callbacks, `hue()` operates on `vec4` values and * returns the hue as a normalized value in the 0–1 range. - * `colorMode()` has no effect inside shader callbacks. + * The range doesn't change with `colorMode()`. * * ```js example * let myShader; @@ -1266,7 +1298,8 @@ function creatingReading(p5, fn) { * * In p5.strands shader callbacks, `saturation()` operates on `vec4` values * and returns the saturation as a normalized value in the 0–1 range. - * `colorMode()` has no effect inside shader callbacks. + * It returns HSB saturation in `HSB` mode and HSL saturation otherwise. + * The range doesn't change with `colorMode()`. * * ```js example * let myShader; @@ -1429,7 +1462,7 @@ function creatingReading(p5, fn) { * * In p5.strands shader callbacks, `brightness()` operates on `vec4` values * and returns the brightness as a normalized value in the 0–1 range. - * `colorMode()` has no effect inside shader callbacks. + * The range doesn't change with `colorMode()`. * * ```js example * let myShader; @@ -1591,7 +1624,7 @@ function creatingReading(p5, fn) { * * In p5.strands shader callbacks, `lightness()` operates on `vec4` values * and returns the lightness as a normalized value in the 0–1 range. - * `colorMode()` has no effect inside shader callbacks. + * The range doesn't change with `colorMode()`. * * ```js example * let myShader; @@ -1735,7 +1768,8 @@ function creatingReading(p5, fn) { * * In p5.strands shader callbacks, `lerpColor()` interpolates between * `vec4` colors and returns a normalized `vec4` with RGBA components in - * the 0–1 range. `colorMode()` has no effect inside shader callbacks. + * the 0–1 range. Colors are always mixed in RGB, whatever the + * `colorMode()`. * * ```js example * let myShader; diff --git a/src/color/setting.js b/src/color/setting.js index 1dd44ace7e..a4d0548f7f 100644 --- a/src/color/setting.js +++ b/src/color/setting.js @@ -729,6 +729,11 @@ function setting(p5, fn) { * is mapped using (50 + 100) / 2 = 75 as its effective maximum. More complex or negative * ranges are currently not handled, so results in those cases may be ambiguous. * + * In p5.strands shader callbacks, calling `colorMode()` only affects that + * shader and doesn't change the sketch's color mode. If it isn't called, + * the sketch's color mode is used. When color() is + * given shader values, it supports the `RGB`, `HSB` and `HSL` color modes. + * * @method colorMode * @param {RGB|HSB|HSL|RGBP3|HWB|LAB|LCH|OKLAB|OKLCH} mode either RGB, HSB, HSL, * or one of the extended modes described above. diff --git a/src/strands/strands_api.js b/src/strands/strands_api.js index 630344f03e..e78f1305cd 100644 --- a/src/strands/strands_api.js +++ b/src/strands/strands_api.js @@ -1010,21 +1010,95 @@ export function initGlobalStrandsAPI(p5, fn, strandsContext) { return getStrandsColorMode(this).mode; }); + // Hue to RGB at full saturation and brightness, based on: + // http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl + // Uses fract() to wrap the hue, since mod() differs on WebGPU + const _hue2rgb = (instance, h) => { + const K = instance.vec3(1, 2 / 3, 1 / 3); + const p = instance.abs( + instance.fract(instance.vec3(h, h, h).add(K)).mult(6).sub(3) + ); + return instance.clamp(p.sub(1), 0, 1); + }; + + // HSB to RGB conversion based on: + // https://en.wikipedia.org/wiki/HSL_and_HSV#HSV_to_RGB + const _hsb2rgb = (instance, h, s, b) => + instance.mix(instance.vec3(1, 1, 1), _hue2rgb(instance, h), s).mult(b); + + // HSL to RGB conversion based on: + // https://en.wikipedia.org/wiki/HSL_and_HSV#HSL_to_RGB + const _hsl2rgb = (instance, h, s, l) => { + const chroma = p5.strandsNode(1).sub(instance.abs(l.mult(2).sub(1))).mult(s); + return _hue2rgb(instance, h).sub(0.5).mult(chroma).add(l); + }; + + // Converts shader values to a vec4, reading them in the strands colorMode + const _colorFromShaderValues = (instance, values) => { + const { mode, maxes } = getStrandsColorMode(instance); + if (![RGB, HSB, HSL].includes(mode)) { + FES.userError( + 'unsupported color mode', + `color() with shader values works in RGB, HSB and HSL color modes for now, not ${mode}. Numbers and color strings still work in every color mode.` + ); + } + // Split vectors into channels, e.g. color(vec3(h, s, b)) + const channels = values.flatMap(value => + value?.isStrandsNode && value.dimension > 1 + ? ['x', 'y', 'z', 'w'].slice(0, value.dimension).map(c => value[c]) + : [value] + ); + if (channels.length > 4) { + FES.userError( + 'parameter validation error', + `color() with shader values expects 1 to 4 values, but ${channels.length} were provided.` + ); + } + // From the colorMode range to 0-1 + const scale = (value, max) => p5.strandsNode(value).div(max); + const clamp01 = node => instance.clamp(node, 0, 1); + const ranges = maxes[mode]; + + let rgb; + let alpha = p5.strandsNode(1); + if (channels.length <= 2) { + // Grayscale uses the third channel's range, like p5.Color does + const gray = clamp01(scale(channels[0], ranges[2])); + rgb = instance.vec3(gray, gray, gray); + if (channels.length === 2) { + alpha = scale(channels[1], ranges[3]); + } + } else { + const [c1, c2, c3] = [0, 1, 2].map(i => scale(channels[i], ranges[i])); + if (mode === HSB) { + rgb = _hsb2rgb(instance, c1, clamp01(c2), clamp01(c3)); + } else if (mode === HSL) { + rgb = _hsl2rgb(instance, c1, clamp01(c2), clamp01(c3)); + } else { + rgb = instance.vec3(c1, c2, c3); + } + if (channels.length === 4) { + alpha = scale(channels[3], ranges[3]); + } + } + return instance.vec4(clamp01(rgb), clamp01(alpha)); + }; + const originalColor = fn.color; augmentFn(fn, p5, 'color', function (...args) { if (!strandsContext.active) { return originalColor.apply(this, args); } + const values = Array.isArray(args[0]) ? args[0] : args; + // Shader values can't go through the CPU parser below + if (values.some(value => value?.isStrandsNode)) { + return _colorFromShaderValues(this, values); + } // Reuse p5's parser - handles hex strings, rgb(), CSS named colors, numerics const { mode, maxes } = getStrandsColorMode(this); const c = args[0] instanceof Color ? args[0] - : new Color( - Array.isArray(args[0]) ? args[0] : args, - mode, - maxes[mode], - { clamp: true } - ); + : new Color(values, mode, maxes[mode], { clamp: true }); // _getRGBA() returns [r, g, b, a] normalized to 0-1 const rgba = c._getRGBA(); const { id, dimension } = build.primitiveConstructorNode( diff --git a/test/unit/webgl/p5.Shader.js b/test/unit/webgl/p5.Shader.js index 79928761c0..991bdab364 100644 --- a/test/unit/webgl/p5.Shader.js +++ b/test/unit/webgl/p5.Shader.js @@ -1251,6 +1251,151 @@ suite('p5.Shader', function () { const pixelColor = getCenterPixel(testShader); assert.approximately(pixelColor[0], 255, 5); }); + + const shaderValueCases = [ + { + name: 'shader HSB values', + setMode: () => myp5.colorMode(myp5.HSB, 1), + value: 0.5, + build: u => myp5.color(u, 1, 1), + expected: [0, 255, 255] + }, + { + name: 'shader HSL values', + setMode: () => myp5.colorMode(myp5.HSL, 1), + value: 1 / 3, + build: u => myp5.color(u, 1, 0.5), + expected: [0, 255, 0] + }, + { + name: 'shader RGB values', + setMode: () => myp5.colorMode(myp5.RGB), + value: 255, + build: u => myp5.color(u, 0, 0), + expected: [255, 0, 0] + }, + { + name: 'shader values in the colorMode ranges', + setMode: () => myp5.colorMode(myp5.HSB), + value: 240, + build: u => myp5.color(u, 100, 100), + expected: [0, 0, 255] + }, + { + name: 'a vector of shader values', + setMode: () => myp5.colorMode(myp5.HSB, 1), + value: 0.5, + build: u => myp5.color(myp5.vec3(u, 1, 1)), + expected: [0, 255, 255] + }, + { + name: 'a grayscale shader value', + setMode: () => myp5.colorMode(myp5.RGB, 1), + value: 0.5, + build: u => myp5.color(u), + expected: [128, 128, 128] + } + ]; + for (const { name, setMode, value, build, expected } of shaderValueCases) { + test(`color() builds colors from ${name}`, () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + const testShader = myp5.baseMaterialShader().modify( + () => { + const u = myp5.uniformFloat(() => value); + // Not myp5[mode]: the transpiler turns that into array indexing + setMode(); + myp5.getPixelInputs(inputs => { + inputs.color = build(u); + return inputs; + }); + }, + { myp5, setMode, value, build } + ); + + const pixelColor = getCenterPixel(testShader); + assert.approximately(pixelColor[0], expected[0], 5); + assert.approximately(pixelColor[1], expected[1], 5); + assert.approximately(pixelColor[2], expected[2], 5); + }); + } + + test('color() rebuilds a color from hue(), saturation() and brightness()', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.colorMode(myp5.HSB, 1); + myp5.getPixelInputs(inputs => { + const c = myp5.color('#ff8080'); + inputs.color = myp5.color( + myp5.hue(c), + myp5.saturation(c), + myp5.brightness(c) + ); + return inputs; + }); + }, + { myp5 } + ); + + const pixelColor = getCenterPixel(testShader); + assert.approximately(pixelColor[0], 255, 5); + assert.approximately(pixelColor[1], 128, 5); + assert.approximately(pixelColor[2], 128, 5); + }); + + test('color() wraps hues from shader values', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.colorMode(myp5.HSB, 1); + myp5.getPixelInputs(inputs => { + // Red's hue is 0, so this is the same hue as 0.25 + const h = myp5.hue(myp5.color('red')) + 1.25; + inputs.color = myp5.color(h, 1, 1); + return inputs; + }); + }, + { myp5 } + ); + + const pixelColor = getCenterPixel(testShader); + assert.approximately(pixelColor[0], 128, 5); + assert.approximately(pixelColor[1], 255, 5); + assert.approximately(pixelColor[2], 0, 5); + }); + + test('color() with shader values gives a friendly error in unsupported color modes', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + expect(() => { + myp5.baseMaterialShader().modify( + () => { + const u = myp5.uniformFloat(() => 50); + myp5.colorMode(myp5.LAB); + myp5.getPixelInputs(inputs => { + inputs.color = myp5.color(u, 0, 0); + return inputs; + }); + }, + { myp5 } + ); + }).toThrowError(/works in RGB, HSB and HSL color modes for now, not lab/); + }); + + test('color() with shader values gives a friendly error for more than 4 values', () => { + myp5.createCanvas(50, 50, myp5.WEBGL); + expect(() => { + myp5.baseMaterialShader().modify( + () => { + const u = myp5.uniformFloat(() => 1); + myp5.getPixelInputs(inputs => { + inputs.color = myp5.color(myp5.vec3(u, 0, 0), 1, 1); + return inputs; + }); + }, + { myp5 } + ); + }).toThrowError(/expects 1 to 4 values, but 5 were provided/); + }); }); test('handle custom uniform names with automatic values', () => { diff --git a/test/unit/webgpu/p5.Shader.js b/test/unit/webgpu/p5.Shader.js index cc49d10403..6e37545cf3 100644 --- a/test/unit/webgpu/p5.Shader.js +++ b/test/unit/webgpu/p5.Shader.js @@ -213,6 +213,61 @@ suite('WebGPU p5.Shader', function () { assert.approximately(pixelColor[2], 153, 5); // Blue channel should be ~153 (0.6 * 255) }); + test('color() rebuilds a color from hue(), saturation() and brightness() in strands', async () => { + await myp5.createCanvas(50, 50, myp5.WEBGPU); + myp5.pixelDensity(1); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.colorMode(myp5.HSB, 1); + myp5.getPixelInputs(inputs => { + const c = myp5.color('#ff8080'); + inputs.color = myp5.color( + myp5.hue(c), + myp5.saturation(c), + myp5.brightness(c) + ); + return inputs; + }); + }, + { myp5 } + ); + + myp5.noStroke(); + myp5.shader(testShader); + myp5.plane(myp5.width, myp5.height); + + const pixelColor = await myp5.get(25, 25); + assert.approximately(pixelColor[0], 255, 5); + assert.approximately(pixelColor[1], 128, 5); + assert.approximately(pixelColor[2], 128, 5); + }); + + test('color() wraps hues from shader values in strands', async () => { + await myp5.createCanvas(50, 50, myp5.WEBGPU); + myp5.pixelDensity(1); + const testShader = myp5.baseMaterialShader().modify( + () => { + myp5.colorMode(myp5.HSB, 1); + myp5.getPixelInputs(inputs => { + // Red's hue is 0, so this is the same hue as 0.25 + const h = myp5.hue(myp5.color('red')) + 1.25; + inputs.color = myp5.color(h, 1, 1); + return inputs; + }); + }, + { myp5 } + ); + + myp5.noStroke(); + myp5.shader(testShader); + myp5.plane(myp5.width, myp5.height); + + const pixelColor = await myp5.get(25, 25); + assert.approximately(pixelColor[0], 128, 5); + assert.approximately(pixelColor[1], 255, 5); + assert.approximately(pixelColor[2], 0, 5); + }); + test('continues inferred uniform suffixes across chained modify calls', async () => { await myp5.createCanvas(50, 50, myp5.WEBGPU); const shader1 = myp5.baseMaterialShader().modify(