Skip to content

Commit 13b91df

Browse files
committed
Merge remote-tracking branch 'upstream/dev-2.0' into matrix-docs
2 parents a844693 + 35ad365 commit 13b91df

File tree

8 files changed

+340
-74
lines changed

8 files changed

+340
-74
lines changed

src/color/p5.Color.js

Lines changed: 102 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,23 @@ class Color {
4444

4545
static colorMap = {};
4646
static #colorjsMaxes = {};
47+
static #grayscaleMap = {};
4748

4849
// Used to add additional color modes to p5.js
4950
// Uses underlying library's definition
5051
static addColorMode(mode, definition){
5152
ColorSpace.register(definition);
5253
Color.colorMap[mode] = definition.id;
54+
5355
// Get colorjs maxes
5456
Color.#colorjsMaxes[mode] = Object.values(definition.coords).reduce((acc, v) => {
5557
acc.push(v.refRange || v.range);
5658
return acc;
5759
}, []);
5860
Color.#colorjsMaxes[mode].push([0, 1]);
61+
62+
// Get grayscale mapping
63+
Color.#grayscaleMap[mode] = definition.fromGray;
5964
}
6065

6166
constructor(vals, colorMode, colorMaxes) {
@@ -99,15 +104,38 @@ class Color {
99104
let mappedVals;
100105

101106
if(colorMaxes){
107+
// NOTE: need to consider different number of arguments (eg. CMYK)
102108
if(vals.length === 4){
103109
mappedVals = Color.mapColorRange(vals, this.mode, colorMaxes);
104110
}else if(vals.length === 3){
105111
mappedVals = Color.mapColorRange([vals[0], vals[1], vals[2]], this.mode, colorMaxes);
106112
mappedVals.push(1);
107113
}else if(vals.length === 2){
108-
mappedVals = Color.mapColorRange([vals[0], vals[0], vals[0], vals[1]], this.mode, colorMaxes);
114+
// Grayscale with alpha
115+
if(Color.#grayscaleMap[this.mode]){
116+
mappedVals = Color.#grayscaleMap[this.mode](vals[0], colorMaxes);
117+
}else{
118+
mappedVals = Color.mapColorRange([vals[0], vals[0], vals[0]], this.mode, colorMaxes);
119+
}
120+
const alphaMaxes = Array.isArray(colorMaxes[colorMaxes.length-1]) ?
121+
colorMaxes[colorMaxes.length-1] :
122+
[0, colorMaxes[colorMaxes.length-1]];
123+
mappedVals.push(
124+
map(
125+
vals[1],
126+
alphaMaxes[0],
127+
alphaMaxes[1],
128+
0,
129+
1
130+
)
131+
);
109132
}else if(vals.length === 1){
110-
mappedVals = Color.mapColorRange([vals[0], vals[0], vals[0]], this.mode, colorMaxes);
133+
// Grayscale only
134+
if(Color.#grayscaleMap[this.mode]){
135+
mappedVals = Color.#grayscaleMap[this.mode](vals[0], colorMaxes);
136+
}else{
137+
mappedVals = Color.mapColorRange([vals[0], vals[0], vals[0]], this.mode, colorMaxes);
138+
}
111139
mappedVals.push(1);
112140
}else{
113141
throw new Error('Invalid color');
@@ -652,6 +680,78 @@ function color(p5, fn, lifecycles){
652680
*/
653681
p5.Color = Color;
654682

683+
sRGB.fromGray = P3.fromGray = function(val, maxes){
684+
// Use blue max
685+
const p5Maxes = maxes.map((max) => {
686+
if(!Array.isArray(max)){
687+
return [0, max];
688+
}else{
689+
return max;
690+
}
691+
});
692+
693+
const v = map(val, p5Maxes[2][0], p5Maxes[2][1], 0, 1);
694+
return [v, v, v];
695+
};
696+
697+
HSBSpace.fromGray = HSLSpace.fromGray = function(val, maxes){
698+
// Use brightness max
699+
const p5Maxes = maxes.map((max) => {
700+
if(!Array.isArray(max)){
701+
return [0, max];
702+
}else{
703+
return max;
704+
}
705+
});
706+
707+
const v = map(val, p5Maxes[2][0], p5Maxes[2][1], 0, 100);
708+
return [0, 0, v];
709+
};
710+
711+
HWBSpace.fromGray = function(val, maxes){
712+
// Use Whiteness and Blackness to create number line
713+
const p5Maxes = maxes.map((max) => {
714+
if(!Array.isArray(max)){
715+
return [0, max];
716+
}else{
717+
return max;
718+
}
719+
});
720+
721+
const wbMax =
722+
(Math.abs(p5Maxes[1][0] - p5Maxes[1][1])) / 2 +
723+
(Math.abs(p5Maxes[2][0] - p5Maxes[2][1])) / 2;
724+
725+
const nVal = map(val, 0, wbMax, 0, 100);
726+
let white, black;
727+
if(nVal < 50){
728+
black = nVal;
729+
white = 100 - nVal;
730+
}else if(nVal >= 50){
731+
white = nVal;
732+
black = 100 - nVal;
733+
}
734+
return [0, white, black];
735+
};
736+
737+
Lab.fromGray =
738+
LCHSpace.fromGray =
739+
OKLab.fromGray =
740+
OKLCHSpace.fromGray =
741+
function(val, maxes){
742+
// Use lightness max
743+
const p5Maxes = maxes.map((max) => {
744+
if(!Array.isArray(max)){
745+
return [0, max];
746+
}else{
747+
return max;
748+
}
749+
});
750+
751+
const v = map(val, p5Maxes[0][0], p5Maxes[0][1], 0, 100);
752+
return [v, 0, 0];
753+
};
754+
655755
// Register color modes and initialize Color maxes to what p5 has set for itself
656756
p5.Color.addColorMode(RGB, sRGB);
657757
p5.Color.addColorMode(RGBHDR, P3);

src/image/loading_displaying.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ function loadingDisplaying(p5, fn){
6666
*
6767
* <div>
6868
* <code>
69-
* function setup() {
69+
* async function setup() {
7070
* // Call handleImage() once the image loads.
71-
* loadImage('assets/laDefense.jpg', handleImage);
71+
* await loadImage('assets/laDefense.jpg', handleImage);
7272
*
7373
* describe('Image of the underside of a white umbrella and a gridded ceiling.');
7474
* }
@@ -82,10 +82,10 @@ function loadingDisplaying(p5, fn){
8282
*
8383
* <div>
8484
* <code>
85-
* function setup() {
85+
* async function setup() {
8686
* // Call handleImage() once the image loads or
8787
* // call handleError() if an error occurs.
88-
* loadImage('assets/laDefense.jpg', handleImage, handleError);
88+
* await loadImage('assets/laDefense.jpg', handleImage, handleError);
8989
* }
9090
*
9191
* // Display the image.
@@ -995,7 +995,7 @@ function loadingDisplaying(p5, fn){
995995
*
996996
* async function setup() {
997997
* // Load the image.
998-
* img = await loadImage('assets/laDefense50.jpg');
998+
* img = await loadImage('assets/laDefense50.png');
999999
*
10001000
* createCanvas(100, 100);
10011001
*

src/shape/curves.js

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -505,7 +505,7 @@ function curves(p5, fn){
505505
* noFill();
506506
* strokeWeight(1);
507507
* stroke(0);
508-
* curve(5, 26, 73, 24, 73, 61, 15, 65);
508+
* spline(5, 26, 73, 24, 73, 61, 15, 65);
509509
*
510510
* // Draw red spline curves from the anchor points to the control points.
511511
* stroke(255, 0, 0);
@@ -653,8 +653,6 @@ function curves(p5, fn){
653653
* @chainable
654654
*/
655655
fn.spline = function(...args) {
656-
// p5._validateParameters('curve', args);
657-
658656
if (!this._renderer.states.strokeColor && !this._renderer.states.fillColor) {
659657
return this;
660658
}
@@ -666,9 +664,9 @@ function curves(p5, fn){
666664
/**
667665
* Calculates coordinates along a spline curve using interpolation.
668666
*
669-
* `curvePoint()` calculates coordinates along a spline curve using the
667+
* `splinePoint()` calculates coordinates along a spline curve using the
670668
* anchor and control points. It expects points in the same order as the
671-
* <a href="#/p5/curve">curve()</a> function. `curvePoint()` works one axis
669+
* <a href="#/p5/spline">spline()</a> function. `splinePoint()` works one axis
672670
* at a time. Passing the anchor and control points' x-coordinates will
673671
* calculate the x-coordinate of a point on the curve. Passing the anchor and
674672
* control points' y-coordinates will calculate the y-coordinate of a point on
@@ -685,7 +683,7 @@ function curves(p5, fn){
685683
* is the first anchor point, 1 is the second anchor point, and 0.5 is halfway
686684
* between them.
687685
*
688-
* @method curvePoint
686+
* @method splinePoint
689687
* @param {Number} a coordinate of first anchor point.
690688
* @param {Number} b coordinate of first control point.
691689
* @param {Number} c coordinate of second control point.
@@ -713,24 +711,24 @@ function curves(p5, fn){
713711
*
714712
* // Draw the curve.
715713
* noFill();
716-
* curve(x1, y1, x2, y2, x3, y3, x4, y4);
714+
* spline(x1, y1, x2, y2, x3, y3, x4, y4);
717715
*
718716
* // Draw circles along the curve's path.
719717
* fill(255);
720718
*
721719
* // Top.
722-
* let x = curvePoint(x1, x2, x3, x4, 0);
723-
* let y = curvePoint(y1, y2, y3, y4, 0);
720+
* let x = splinePoint(x1, x2, x3, x4, 0);
721+
* let y = splinePoint(y1, y2, y3, y4, 0);
724722
* circle(x, y, 5);
725723
*
726724
* // Center.
727-
* x = curvePoint(x1, x2, x3, x4, 0.5);
728-
* y = curvePoint(y1, y2, y3, y4, 0.5);
725+
* x = splinePoint(x1, x2, x3, x4, 0.5);
726+
* y = splinePoint(y1, y2, y3, y4, 0.5);
729727
* circle(x, y, 5);
730728
*
731729
* // Bottom.
732-
* x = curvePoint(x1, x2, x3, x4, 1);
733-
* y = curvePoint(y1, y2, y3, y4, 1);
730+
* x = splinePoint(x1, x2, x3, x4, 1);
731+
* y = splinePoint(y1, y2, y3, y4, 1);
734732
* circle(x, y, 5);
735733
*
736734
* describe('A black curve on a gray square. The endpoints and center of the curve are marked with white circles.');
@@ -761,12 +759,12 @@ function curves(p5, fn){
761759
*
762760
* // Draw the curve.
763761
* noFill();
764-
* curve(x1, y1, x2, y2, x3, y3, x4, y4);
762+
* spline(x1, y1, x2, y2, x3, y3, x4, y4);
765763
*
766764
* // Calculate the circle's coordinates.
767765
* let t = 0.5 * sin(frameCount * 0.01) + 0.5;
768-
* let x = curvePoint(x1, x2, x3, x4, t);
769-
* let y = curvePoint(y1, y2, y3, y4, t);
766+
* let x = splinePoint(x1, x2, x3, x4, t);
767+
* let y = splinePoint(y1, y2, y3, y4, t);
770768
*
771769
* // Draw the circle.
772770
* fill(255);
@@ -775,8 +773,7 @@ function curves(p5, fn){
775773
* </code>
776774
* </div>
777775
*/
778-
fn.curvePoint = function(a, b, c, d, t) {
779-
// p5._validateParameters('curvePoint', arguments);
776+
fn.splinePoint = function(a, b, c, d, t) {
780777
const s = this._renderer.states.splineProperties.tightness,
781778
t3 = t * t * t,
782779
t2 = t * t,
@@ -793,9 +790,9 @@ function curves(p5, fn){
793790
* Tangent lines skim the surface of a curve. A tangent line's slope equals
794791
* the curve's slope at the point where it intersects.
795792
*
796-
* `curveTangent()` calculates coordinates along a tangent line using the
793+
* `splineTangent()` calculates coordinates along a tangent line using the
797794
* spline curve's anchor and control points. It expects points in the same
798-
* order as the <a href="#/p5/curve">curve()</a> function. `curveTangent()`
795+
* order as the <a href="#/p5/spline">spline()</a> function. `splineTangent()`
799796
* works one axis at a time. Passing the anchor and control points'
800797
* x-coordinates will calculate the x-coordinate of a point on the tangent
801798
* line. Passing the anchor and control points' y-coordinates will calculate
@@ -812,7 +809,7 @@ function curves(p5, fn){
812809
* is the first anchor point, 1 is the second anchor point, and 0.5 is halfway
813810
* between them.
814811
*
815-
* @method curveTangent
812+
* @method splineTangent
816813
* @param {Number} a coordinate of first control point.
817814
* @param {Number} b coordinate of first anchor point.
818815
* @param {Number} c coordinate of second anchor point.
@@ -840,48 +837,48 @@ function curves(p5, fn){
840837
*
841838
* // Draw the curve.
842839
* noFill();
843-
* curve(x1, y1, x2, y2, x3, y3, x4, y4);
840+
* spline(x1, y1, x2, y2, x3, y3, x4, y4);
844841
*
845842
* // Draw tangents along the curve's path.
846843
* fill(255);
847844
*
848845
* // Top circle.
849846
* stroke(0);
850-
* let x = curvePoint(x1, x2, x3, x4, 0);
851-
* let y = curvePoint(y1, y2, y3, y4, 0);
847+
* let x = splinePoint(x1, x2, x3, x4, 0);
848+
* let y = splinePoint(y1, y2, y3, y4, 0);
852849
* circle(x, y, 5);
853850
*
854851
* // Top tangent line.
855852
* // Scale the tangent point to draw a shorter line.
856853
* stroke(255, 0, 0);
857-
* let tx = 0.2 * curveTangent(x1, x2, x3, x4, 0);
858-
* let ty = 0.2 * curveTangent(y1, y2, y3, y4, 0);
854+
* let tx = 0.2 * splineTangent(x1, x2, x3, x4, 0);
855+
* let ty = 0.2 * splineTangent(y1, y2, y3, y4, 0);
859856
* line(x + tx, y + ty, x - tx, y - ty);
860857
*
861858
* // Center circle.
862859
* stroke(0);
863-
* x = curvePoint(x1, x2, x3, x4, 0.5);
864-
* y = curvePoint(y1, y2, y3, y4, 0.5);
860+
* x = splinePoint(x1, x2, x3, x4, 0.5);
861+
* y = splinePoint(y1, y2, y3, y4, 0.5);
865862
* circle(x, y, 5);
866863
*
867864
* // Center tangent line.
868865
* // Scale the tangent point to draw a shorter line.
869866
* stroke(255, 0, 0);
870-
* tx = 0.2 * curveTangent(x1, x2, x3, x4, 0.5);
871-
* ty = 0.2 * curveTangent(y1, y2, y3, y4, 0.5);
867+
* tx = 0.2 * splineTangent(x1, x2, x3, x4, 0.5);
868+
* ty = 0.2 * splineTangent(y1, y2, y3, y4, 0.5);
872869
* line(x + tx, y + ty, x - tx, y - ty);
873870
*
874871
* // Bottom circle.
875872
* stroke(0);
876-
* x = curvePoint(x1, x2, x3, x4, 1);
877-
* y = curvePoint(y1, y2, y3, y4, 1);
873+
* x = splinePoint(x1, x2, x3, x4, 1);
874+
* y = splinePoint(y1, y2, y3, y4, 1);
878875
* circle(x, y, 5);
879876
*
880877
* // Bottom tangent line.
881878
* // Scale the tangent point to draw a shorter line.
882879
* stroke(255, 0, 0);
883-
* tx = 0.2 * curveTangent(x1, x2, x3, x4, 1);
884-
* ty = 0.2 * curveTangent(y1, y2, y3, y4, 1);
880+
* tx = 0.2 * splineTangent(x1, x2, x3, x4, 1);
881+
* ty = 0.2 * splineTangent(y1, y2, y3, y4, 1);
885882
* line(x + tx, y + ty, x - tx, y - ty);
886883
*
887884
* describe(
@@ -891,9 +888,7 @@ function curves(p5, fn){
891888
* </code>
892889
* </div>
893890
*/
894-
fn.curveTangent = function(a, b, c, d, t) {
895-
// p5._validateParameters('curveTangent', arguments);
896-
891+
fn.splineTangent = function(a, b, c, d, t) {
897892
const s = this._renderer.states.splineProperties.tightness,
898893
tt3 = t * t * 3,
899894
t2 = t * 2,

0 commit comments

Comments
 (0)