diff --git a/src/color/creating_reading.js b/src/color/creating_reading.js index 340887294d..fd19556141 100644 --- a/src/color/creating_reading.js +++ b/src/color/creating_reading.js @@ -341,6 +341,29 @@ function creatingReading(p5, fn){ * * describe('Two blue rectangles. A darker rectangle on the left and a brighter one on the right.'); * } + * + * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * // Same syntax as regular sketch code... + * let c = color(255, 0, 0); + * // ...but c is a vec4 with normalized RGBA (0-1), not a p5.Color. + * return vec4(c.r, c.g, c.b, c.a); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A red sphere on a gray background.'); + * } */ /** * @method color @@ -396,12 +419,37 @@ function creatingReading(p5, fn){ * to 255. If the colorMode() is set to RGB, it * returns the red value in the given range. * + * 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. + * * @method red * @param {p5.Color|Number[]|String} color p5.Color object, array of * color components, or CSS color string. * @return {Number} the red value. * * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * let r = red(col); + * return vec4(r, 0, 0, 1.0); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A sphere colored using only its red channel.'); + * } + * + * @example * function setup() { * createCanvas(100, 100); * @@ -517,12 +565,37 @@ function creatingReading(p5, fn){ * to 255. If the colorMode() is set to RGB, it * returns the green value in the given range. * + * 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. + * * @method green * @param {p5.Color|Number[]|String} color p5.Color object, array of * color components, or CSS color string. * @return {Number} the green value. * * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * let g = green(col); + * return vec4(0, g, 0, 1.0); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A sphere colored using only its green channel.'); + * } + * + * @example * function setup() { * createCanvas(100, 100); * @@ -638,12 +711,37 @@ function creatingReading(p5, fn){ * to 255. If the colorMode() is set to RGB, it * returns the blue value in the given range. * + * 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. + * * @method blue * @param {p5.Color|Number[]|String} color p5.Color object, array of * color components, or CSS color string. * @return {Number} the blue value. * * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * let b = blue(col); + * return vec4(0, 0, b, 1.0); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A sphere colored using only its blue channel.'); + * } + * + * @example * function setup() { * createCanvas(100, 100); * @@ -755,12 +853,37 @@ function creatingReading(p5, fn){ * p5.Color object, an array of color components, or * a CSS color string. * + * 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. + * * @method alpha * @param {p5.Color|Number[]|String} color p5.Color object, array of * color components, or CSS color string. * @return {Number} the alpha value. * * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * let a = alpha(col); + * return vec4(col.r, col.g, col.b, a * 0.5); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A semi-transparent sphere.'); + * } + * + * @example * function setup() { * createCanvas(100, 100); * @@ -850,12 +973,37 @@ function creatingReading(p5, fn){ * colorMode() is set to HSB or HSL, it returns the hue * value in the given mode. * + * 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. + * * @method hue * @param {p5.Color|Number[]|String} color p5.Color object, array of * color components, or CSS color string. * @return {Number} the hue value. * * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * let h = hue(col); + * return vec4(h, h, h, 1.0); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A sphere shaded in grayscale based on hue.'); + * } + * + * @example * function setup() { * createCanvas(100, 100); * @@ -976,12 +1124,37 @@ function creatingReading(p5, fn){ * colorMode() is set to HSB or HSL, it returns the * saturation value in the given mode. * + * 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. + * * @method saturation * @param {p5.Color|Number[]|String} color p5.Color object, array of * color components, or CSS color string. * @return {Number} the saturation value * * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * let s = saturation(col); + * return vec4(s, s, s, 1.0); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A sphere shaded in grayscale based on saturation.'); + * } + * + * @example * function setup() { * createCanvas(100, 100); * @@ -1134,12 +1307,37 @@ function creatingReading(p5, fn){ * to 100. If the colorMode() is set to HSB, it * returns the brightness value in the given range. * + * 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. + * * @method brightness * @param {p5.Color|Number[]|String} color p5.Color object, array of * color components, or CSS color string. * @return {Number} the brightness value. * * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * let b = brightness(col); + * return vec4(b, b, b, 1.0); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A sphere shaded in grayscale based on brightness.'); + * } + * + * @example * function setup() { * createCanvas(100, 100); * @@ -1264,12 +1462,37 @@ function creatingReading(p5, fn){ * to 100. If the colorMode() is set to HSL, it * returns the lightness value in the given range. * + * 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. + * * @method lightness * @param {p5.Color|Number[]|String} color p5.Color object, array of * color components, or CSS color string. * @return {Number} the lightness value. * * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * let l = lightness(col); + * return vec4(l, l, l, 1.0); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A sphere shaded in grayscale based on lightness.'); + * } + * + * @example * function setup() { * createCanvas(100, 100); * @@ -1396,6 +1619,10 @@ function creatingReading(p5, fn){ * The way that colors are interpolated depends on the current * colorMode(). * + * 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. + * * @method lerpColor * @param {p5.Color} c1 interpolate from this color. * @param {p5.Color} c2 interpolate to this color. @@ -1403,6 +1630,32 @@ function creatingReading(p5, fn){ * @return {p5.Color} interpolated color. * * @example + * let myShader; + * function setup() { + * createCanvas(100, 100, WEBGL); + * + * myShader = baseMaterialShader().modify(() => { + * getFinalColor((col) => { + * let c1 = color('red'); + * let c2 = color('blue'); + * let mixed = lerpColor(c1, c2, 0.5); + * let r = red(mixed); + * let g = green(mixed); + * let b = blue(mixed); + * return vec4(r, g, b, 1.0); + * }); + * }); + * } + * + * function draw() { + * shader(myShader); + * noStroke(); + * sphere(40); + * + * describe('A purple sphere, a blend of red and blue.'); + * } + * + * @example * function setup() { * createCanvas(100, 100); * @@ -1499,4 +1752,4 @@ export default creatingReading; if(typeof p5 !== 'undefined'){ creatingReading(p5, p5.prototype); -} +} \ No newline at end of file