@@ -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,31 @@ 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`: The single value will be used consistently for the gray equivalent
786
+ * in each of these modes (for example, mapping to red/green/blue channels equally in RGB).
787
+ *
788
+ * - `LAB, LCH, OKLAB, and OKLCH`: The single value is taken to be the `lightness (L)` component,
789
+ * with the specified max range for that channel.
790
+ *
791
+ * - `HWB`: Grayscale relies on both the` whiteness (W)` and `blackness (B)` channels. Since
792
+ * a single value cannot directly account for two distinct channels, the library uses an
793
+ * average of their max values to interpret the single grayscale parameter. For instance,
794
+ * if W has a max of 50 and B has a max of 100, then the single grayscale parameter
795
+ * is mapped using (50 + 100) / 2 = 75 as its effective maximum. More complex or negative
796
+ * ranges are currently not handled, so results in those cases may be ambiguous.
797
+ *
780
798
* @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).
799
+ * @param {RGB|HSB|HSL|RGBHDR|HWB|LAB|LCH|OKLAB|OKLCH } mode either RGB, HSB, HSL,
800
+ * or one of the extended modes described above.
784
801
* @param {Number } [max] range for all values.
785
802
* @chainable
786
803
*
@@ -1092,7 +1109,55 @@ function setting(p5, fn){
1092
1109
* }
1093
1110
* </code>
1094
1111
* </div>
1112
+ *
1113
+ * @example
1114
+ * <div>
1115
+ * <code>
1116
+ *
1117
+ * // Example: Single-value (Grayscale) colors in different color modes.
1118
+ * // Each rectangle is filled with one parameter, but its final color depends
1119
+ * // on how that parameter is interpreted by the current color mode.
1120
+ *
1121
+ *
1122
+ * function setup() {
1123
+ * createCanvas(300, 200);
1124
+ * noStroke();
1125
+ * noLoop();
1126
+ * }
1127
+ *
1128
+ * function draw() {
1129
+ * //--- Left rectangle: RGB mode
1130
+ * colorMode(RGB, 255);
1131
+ * fill(128); // Interpreted as R=G=B=128 in RGB
1132
+ * rect(0, 0, 100, 200);
1133
+ *
1134
+ * //--- Middle rectangle: LAB mode
1135
+ * // In LAB, a single value is interpreted as Lightness (L).
1136
+ * // The default max for each LAB component is 100, so a single value of 50
1137
+ * // becomes roughly halfway in terms of lightness.
1138
+ * colorMode(LAB, 100);
1139
+ * fill(50);
1140
+ * rect(100, 0, 100, 200);
1141
+ *
1142
+ * //--- Right rectangle: HWB mode
1143
+ * // In HWB, a single value is mapped onto whiteness and blackness together.
1144
+ * // Because both W and B are needed to specify a gray, the library averages
1145
+ * // their max values to interpret this single parameter.
1146
+ * colorMode(HWB, 100);
1147
+ * fill(50);
1148
+ * rect(200, 0, 100, 200);
1149
+ *
1150
+ * // Add text labels
1151
+ * fill(0); // Switch to black text for clarity (RGB mode for text)
1152
+ * textSize(14);
1153
+ * text("RGB (128)", 10, 20);
1154
+ * text("LAB (50)", 105, 20);
1155
+ * text("HWB (50)", 205, 20);
1156
+ * }
1157
+ * </code>
1158
+ * </div>
1095
1159
*/
1160
+
1096
1161
/**
1097
1162
* @method colorMode
1098
1163
* @param {RGB|HSB|HSL|RGBHDR|HWB|LAB|LCH|OKLAB|OKLCH } mode
0 commit comments