@@ -743,9 +743,9 @@ function setting(p5, fn){
743
743
* Calling `colorMode(RGB, 100)` sets colors to use RGB color values
744
744
* between 0 and 100. Pure red is `color(100, 0, 0)` in this model.
745
745
*
746
- * Calling `colorMode(HSB)` or `colorMode(HSL)` changes to HSB or HSL system
747
- * instead of RGB. Pure red is `color(0, 100, 100)` in HSB and
748
- * `color(0, 100, 50)` in HSL.
746
+ * Calling `colorMode(HSB)` or `colorMode(HSL)` changes to HSB or HSL systems instead of RGB.
747
+ * Pure red is `color(0, 100, 100)` in HSB and `color(0, 100, 50)` in HSL.
748
+ *
749
749
* Some additional color modes that p5.js supports are:
750
750
*
751
751
* `RGBHDR` - High Dynamic Range RGB defined within the Display P3 color space.
@@ -758,7 +758,7 @@ function setting(p5, fn){
758
758
* of whiteness and blackness. This is the color model used by Chrome's GUI color picker.
759
759
* Pure red in HWB is represented as `color(0, 0, 0)` (i.e., hue 0 with 0% whiteness and 0% blackness).
760
760
*
761
- * <img src="assets/hwb.png"></img>
761
+ * <img src="assets/hwb.png"></img>
762
762
*
763
763
* `LAB` - Also known as CIE Lab, this color mode defines colors with Lightness, Alpha, and Beta.
764
764
* It is widely used in professional color measurement contexts due to its perceptual uniformity.
@@ -773,14 +773,33 @@ function setting(p5, fn){
773
773
*
774
774
* `OKLCH` - An easier-to-use representation of OKLAB, expressing colors in terms of Lightness, Chroma, and Hue.
775
775
* This mode retains the perceptual benefits of OKLAB while offering a more intuitive format for color manipulation.
776
- *
776
+ *
777
777
* <a href="#/p5.Color">p5.Color</a> objects remember the mode that they were
778
778
* created in. Changing modes doesn't affect their appearance.
779
779
*
780
+ * `Single-value (Grayscale) Colors`:
781
+ * When a color is specified with only one parameter (e.g., `color(g)`), p5.js will interpret it
782
+ * as a grayscale color. However, how that single parameter translates into a grayscale value
783
+ * depends on the color mode:
784
+ *
785
+ * - `RGB, HSB, and HSL`: In RGB, the single value is interpreted using the “blue” maximum
786
+ * (i.e., the single parameter is mapped to the blue channel's max).
787
+ * In HSB and HSL, the single value is mapped to Brightness and Lightness max respectively with hue=0 .
788
+ * and saturation=0.
789
+ *
790
+ * - `LAB, LCH, OKLAB, and OKLCH`: The single value is taken to be the `lightness (L)` component,
791
+ * with the specified max range for that channel.
792
+ *
793
+ * - `HWB`: Grayscale relies on both the `whiteness (W)` and `blackness (B)` channels. Since
794
+ * a single value cannot directly account for two distinct channels, the library uses an
795
+ * average of their max values to interpret the single grayscale parameter. For instance,
796
+ * if W has a max of 50 and B has a max of 100, then the single grayscale parameter
797
+ * is mapped using (50 + 100) / 2 = 75 as its effective maximum. More complex or negative
798
+ * ranges are currently not handled, so results in those cases may be ambiguous.
799
+ *
780
800
* @method colorMode
781
- * @param {RGB|HSB|HSL|RGBHDR|HWB|LAB|LCH|OKLAB|OKLCH } mode either RGB, HSB
782
- * or HSL, corresponding to Red/Green/Blue and
783
- * Hue/Saturation/Brightness (or Lightness).
801
+ * @param {RGB|HSB|HSL|RGBHDR|HWB|LAB|LCH|OKLAB|OKLCH } mode either RGB, HSB, HSL,
802
+ * or one of the extended modes described above.
784
803
* @param {Number } [max] range for all values.
785
804
* @chainable
786
805
*
@@ -1092,7 +1111,37 @@ function setting(p5, fn){
1092
1111
* }
1093
1112
* </code>
1094
1113
* </div>
1114
+ *
1115
+ * @example
1116
+ * <div>
1117
+ * <code>
1118
+ * // Example: Single-value (Grayscale) colors in different color modes.
1119
+ * // The rectangle is filled with one parameter, but its final color depends
1120
+ * // on how that parameter is interpreted by the current color mode.
1121
+ *
1122
+ * function setup() {
1123
+ * createCanvas(100, 100);
1124
+ * noStroke();
1125
+ * noLoop();
1126
+ * }
1127
+ *
1128
+ * function draw() {
1129
+ * // Set color mode to RGB with range 0-255
1130
+ * colorMode(RGB, 255);
1131
+ *
1132
+ * // Fill with single grayscale value
1133
+ * fill(128);
1134
+ * rect(0, 0, 100, 100);
1135
+ *
1136
+ * // Add text label
1137
+ * fill(0); // Switch to black text for clarity
1138
+ * textSize(14);
1139
+ * text("RGB (128)", 10, 20);
1140
+ * }
1141
+ * </code>
1142
+ * </div>
1095
1143
*/
1144
+
1096
1145
/**
1097
1146
* @method colorMode
1098
1147
* @param {RGB|HSB|HSL|RGBHDR|HWB|LAB|LCH|OKLAB|OKLCH } mode
0 commit comments