Skip to content

Commit f0aed8d

Browse files
committed
Update p5.Color references
1 parent f14a45e commit f0aed8d

File tree

1 file changed

+123
-65
lines changed

1 file changed

+123
-65
lines changed

src/color/p5.Color.js

Lines changed: 123 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,9 @@ const colorPatterns = {
316316
};
317317

318318
/**
319-
* A class to describe a color. Each `p5.Color` object stores the color mode
319+
* A class to describe a color.
320+
*
321+
* Each `p5.Color` object stores the color mode
320322
* and level maxes that were active during its construction. These values are
321323
* used to interpret the arguments passed to the object's constructor. They
322324
* also determine output formatting such as when
@@ -330,8 +332,8 @@ const colorPatterns = {
330332
* When different color representations are calculated, the results are cached
331333
* for performance. These values are normalized, floating-point numbers.
332334
*
333-
* <a href="#/p5/color">color()</a> is the recommended way to create an instance
334-
* of this class.
335+
* Note: <a href="#/p5/color">color()</a> is the recommended way to create an
336+
* instance of this class.
335337
*
336338
* @class p5.Color
337339
* @constructor
@@ -358,8 +360,15 @@ p5.Color = class Color {
358360
}
359361

360362
/**
361-
* Returns the color formatted as a string. Doing so can be useful for
362-
* debugging, or for using p5.js with other libraries.
363+
* Returns the color formatted as a `String`.
364+
*
365+
* Calling `myColor.toString()` can be useful for debugging, as in
366+
* `print(myColor.toString())`. It's also helpful for using p5.js with other
367+
* libraries.
368+
*
369+
* The parameter, `format`, is optional. If a format string is passed, as in
370+
* `myColor.toString('#rrggbb')`, it will determine how the color string is
371+
* formatted. By default, color strings are formatted as `'rgba(r, g, b, a)'`.
363372
*
364373
* @method toString
365374
* @param {String} [format] how the color string will be formatted.
@@ -370,26 +379,25 @@ p5.Color = class Color {
370379
* 'rgb%' 'hsb%' 'hsl%' 'rgba%' 'hsba%' and 'hsla%' format as percentages.
371380
* @return {String} the formatted string.
372381
*
373-
* @example
374382
* <div>
375383
* <code>
376-
* createCanvas(200, 100);
377-
* stroke(255);
378-
* const myColor = color(100, 100, 250);
379-
* fill(myColor);
380-
* rotate(HALF_PI);
381-
* text(myColor.toString(), 0, -5);
382-
* text(myColor.toString('#rrggbb'), 0, -30);
383-
* text(myColor.toString('rgba%'), 0, -55);
384-
* describe('Three text representation of a color written sideways.');
385-
* </code>
386-
* </div>
384+
* function setup() {
385+
* createCanvas(100, 100);
387386
*
388-
* <div>
389-
* <code>
390-
* const myColor = color(100, 130, 250);
391-
* text(myColor.toString('#rrggbb'), 25, 25);
392-
* describe('A hexadecimal representation of a color.');
387+
* background(200);
388+
*
389+
* // Create a p5.Color object.
390+
* let myColor = color('darkorchid');
391+
*
392+
* // Style the text.
393+
* textAlign(CENTER);
394+
* textSize(16);
395+
*
396+
* // Display the text.
397+
* text(myColor.toString('#rrggbb'), 50, 50);
398+
*
399+
* describe('The text "#9932cc" written in purple on a gray background.');
400+
* }
393401
* </code>
394402
* </div>
395403
*/
@@ -561,26 +569,38 @@ p5.Color = class Color {
561569
}
562570

563571
/**
564-
* Sets the red component of a color. The range depends on the
565-
* <a href="#/colorMode">colorMode()</a>. In the default RGB mode it's
566-
* between 0 and 255.
572+
* Sets the red component of a color.
573+
*
574+
* The range depends on the <a href="#/colorMode">colorMode()</a>. In the
575+
* default RGB mode it's between 0 and 255.
567576
*
568577
* @method setRed
569578
* @param {Number} red the new red value.
570579
*
571580
* @example
572581
* <div>
573582
* <code>
574-
* let backgroundColor;
575-
*
576583
* function setup() {
577-
* backgroundColor = color(100, 50, 150);
578-
* }
584+
* createCanvas(100, 100);
585+
*
586+
* background(200);
579587
*
580-
* function draw() {
581-
* backgroundColor.setRed(128 + 128 * sin(millis() / 1000));
582-
* background(backgroundColor);
583-
* describe('A canvas with a gradually changing background color.');
588+
* // Create a p5.Color object.
589+
* let c = color(255, 128, 128);
590+
*
591+
* // Draw the left rectangle.
592+
* noStroke();
593+
* fill(c);
594+
* rect(15, 20, 35, 60);
595+
*
596+
* // Change the red value.
597+
* c.setRed(64);
598+
*
599+
* // Draw the right rectangle.
600+
* fill(c);
601+
* rect(50, 20, 35, 60);
602+
*
603+
* describe('Two rectangles. The left one is salmon pink and the right one is teal.');
584604
* }
585605
* </code>
586606
* </div>
@@ -591,26 +611,38 @@ p5.Color = class Color {
591611
}
592612

593613
/**
594-
* Sets the green component of a color. The range depends on the
595-
* <a href="#/colorMode">colorMode()</a>. In the default RGB mode it's
596-
* between 0 and 255.
614+
* Sets the green component of a color.
615+
*
616+
* The range depends on the <a href="#/colorMode">colorMode()</a>. In the
617+
* default RGB mode it's between 0 and 255.
597618
*
598619
* @method setGreen
599620
* @param {Number} green the new green value.
600621
*
601622
* @example
602623
* <div>
603624
* <code>
604-
* let backgroundColor;
605-
*
606625
* function setup() {
607-
* backgroundColor = color(100, 50, 150);
608-
* }
626+
* createCanvas(100, 100);
627+
*
628+
* background(200);
629+
*
630+
* // Create a p5.Color object.
631+
* let c = color(255, 128, 128);
632+
*
633+
* // Draw the left rectangle.
634+
* noStroke();
635+
* fill(c);
636+
* rect(15, 20, 35, 60);
637+
*
638+
* // Change the green value.
639+
* c.setGreen(255);
609640
*
610-
* function draw() {
611-
* backgroundColor.setGreen(128 + 128 * sin(millis() / 1000));
612-
* background(backgroundColor);
613-
* describe('A canvas with a gradually changing background color.');
641+
* // Draw the right rectangle.
642+
* fill(c);
643+
* rect(50, 20, 35, 60);
644+
*
645+
* describe('Two rectangles. The left one is salmon pink and the right one is yellow.');
614646
* }
615647
* </code>
616648
* </div>
@@ -621,26 +653,38 @@ p5.Color = class Color {
621653
}
622654

623655
/**
624-
* Sets the blue component of a color. The range depends on the
625-
* <a href="#/colorMode">colorMode()</a>. In the default RGB mode it's
626-
* between 0 and 255.
656+
* Sets the blue component of a color.
657+
*
658+
* The range depends on the <a href="#/colorMode">colorMode()</a>. In the
659+
* default RGB mode it's between 0 and 255.
627660
*
628661
* @method setBlue
629662
* @param {Number} blue the new blue value.
630663
*
631664
* @example
632665
* <div>
633666
* <code>
634-
* let backgroundColor;
635-
*
636667
* function setup() {
637-
* backgroundColor = color(100, 50, 150);
638-
* }
668+
* createCanvas(100, 100);
669+
*
670+
* background(200);
671+
*
672+
* // Create a p5.Color object.
673+
* let c = color(255, 128, 128);
674+
*
675+
* // Draw the left rectangle.
676+
* noStroke();
677+
* fill(c);
678+
* rect(15, 20, 35, 60);
679+
*
680+
* // Change the blue value.
681+
* c.setBlue(255);
639682
*
640-
* function draw() {
641-
* backgroundColor.setBlue(128 + 128 * sin(millis() / 1000));
642-
* background(backgroundColor);
643-
* describe('A canvas with a gradually changing background color.');
683+
* // Draw the right rectangle.
684+
* fill(c);
685+
* rect(50, 20, 35, 60);
686+
*
687+
* describe('Two rectangles. The left one is salmon pink and the right one is pale fuchsia.');
644688
* }
645689
* </code>
646690
* </div>
@@ -651,26 +695,40 @@ p5.Color = class Color {
651695
}
652696

653697
/**
654-
* Sets the alpha (transparency) value of a color. The range depends on the
698+
* Sets the alpha (transparency) value of a color.
699+
*
700+
* The range depends on the
655701
* <a href="#/colorMode">colorMode()</a>. In the default RGB mode it's
656702
* between 0 and 255.
657703
*
658704
* @method setAlpha
659705
* @param {Number} alpha the new alpha value.
660706
*
661707
* @example
708+
* @example
662709
* <div>
663710
* <code>
664-
* function draw() {
665-
* clear();
711+
* function setup() {
712+
* createCanvas(100, 100);
713+
*
666714
* background(200);
667-
* const squareColor = color(100, 50, 100);
668-
* squareColor.setAlpha(128 + 128 * sin(millis() / 1000));
669-
* fill(squareColor);
670-
* rect(13, 13, width - 26, height - 26);
671-
* describe(
672-
* 'A purple square with gradually changing opacity drawn on a gray background.'
673-
* );
715+
*
716+
* // Create a p5.Color object.
717+
* let c = color(255, 128, 128);
718+
*
719+
* // Draw the left rectangle.
720+
* noStroke();
721+
* fill(c);
722+
* rect(15, 20, 35, 60);
723+
*
724+
* // Change the alpha value.
725+
* c.setAlpha(128);
726+
*
727+
* // Draw the right rectangle.
728+
* fill(c);
729+
* rect(50, 20, 35, 60);
730+
*
731+
* describe('Two rectangles. The left one is salmon pink and the right one is faded pink.');
674732
* }
675733
* </code>
676734
* </div>

0 commit comments

Comments
 (0)