Skip to content

Commit e538e51

Browse files
authored
Merge pull request #7690 from perminder-17/patch-6
Updating Gray-scale in colorMode()
2 parents e0a3357 + f99fea0 commit e538e51

File tree

1 file changed

+57
-8
lines changed

1 file changed

+57
-8
lines changed

src/color/setting.js

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -743,9 +743,9 @@ function setting(p5, fn){
743743
* Calling `colorMode(RGB, 100)` sets colors to use RGB color values
744744
* between 0 and 100. Pure red is `color(100, 0, 0)` in this model.
745745
*
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+
*
749749
* Some additional color modes that p5.js supports are:
750750
*
751751
* `RGBHDR` - High Dynamic Range RGB defined within the Display P3 color space.
@@ -758,7 +758,7 @@ function setting(p5, fn){
758758
* of whiteness and blackness. This is the color model used by Chrome's GUI color picker.
759759
* Pure red in HWB is represented as `color(0, 0, 0)` (i.e., hue 0 with 0% whiteness and 0% blackness).
760760
*
761-
* <img src="assets/hwb.png"></img>
761+
* <img src="assets/hwb.png"></img>
762762
*
763763
* `LAB` - Also known as CIE Lab, this color mode defines colors with Lightness, Alpha, and Beta.
764764
* It is widely used in professional color measurement contexts due to its perceptual uniformity.
@@ -773,14 +773,33 @@ function setting(p5, fn){
773773
*
774774
* `OKLCH` - An easier-to-use representation of OKLAB, expressing colors in terms of Lightness, Chroma, and Hue.
775775
* This mode retains the perceptual benefits of OKLAB while offering a more intuitive format for color manipulation.
776-
*
776+
*
777777
* <a href="#/p5.Color">p5.Color</a> objects remember the mode that they were
778778
* created in. Changing modes doesn't affect their appearance.
779779
*
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+
*
780800
* @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.
784803
* @param {Number} [max] range for all values.
785804
* @chainable
786805
*
@@ -1092,7 +1111,37 @@ function setting(p5, fn){
10921111
* }
10931112
* </code>
10941113
* </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>
10951143
*/
1144+
10961145
/**
10971146
* @method colorMode
10981147
* @param {RGB|HSB|HSL|RGBHDR|HWB|LAB|LCH|OKLAB|OKLCH} mode

0 commit comments

Comments
 (0)