Skip to content

Commit d1edcb4

Browse files
authored
updating the colorMode() function
1 parent 16c2cb4 commit d1edcb4

1 file changed

Lines changed: 160 additions & 2 deletions

File tree

src/color/setting.js

Lines changed: 160 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,34 @@ function setting(p5, fn){
746746
* Calling `colorMode(HSB)` or `colorMode(HSL)` changes to HSB or HSL system
747747
* instead of RGB. Pure red is `color(0, 100, 100)` in HSB and
748748
* `color(0, 100, 50)` in HSL.
749-
*
749+
* Some additional color modes that p5.js supports are:
750+
*
751+
* `RGBHDR` - High Dynamic Range RGB defined within the Display P3 color space.
752+
* Colors are expressed with an extended dynamic range. To render these colors
753+
* accurately, you must use the HDR canvas.
754+
*
755+
* `HWB` - Hue, Whiteness, Blackness.
756+
* Similar to HSB and HSL, this mode uses a hue angle.
757+
* Instead of saturation and lightness, HWB defines colors based on the percentage
758+
* of whiteness and blackness. This is the color model used by Chrome's GUI color picker.
759+
* Pure red in HWB is represented as `color(0, 0, 0)` (i.e., hue 0 with 0% whiteness and 0% blackness).
760+
*
761+
* <img src="assets/hwb.png"></img>
762+
*
763+
* `LAB` - Also known as CIE Lab, this color mode defines colors with Lightness, Alpha, and Beta.
764+
* It is widely used in professional color measurement contexts due to its perceptual uniformity.
765+
*
766+
* `LCH` - A more intuitive representation of the CIE Lab color space using Lightness, Chroma, and Hue.
767+
* This mode separates the color's chromatic intensity (chroma) from its lightness,
768+
* simplifying color selection and manipulation.
769+
*
770+
* `OKLAB` - A variant of the CIE Lab color space that corrects for non-uniformities inherent in LAB.
771+
* The adjustment provides a more perceptually accurate and uniform representation,
772+
* which is particularly beneficial for smooth color transitions.
773+
*
774+
* `OKLCH` - An easier-to-use representation of OKLAB, expressing colors in terms of Lightness, Chroma, and Hue.
775+
* This mode retains the perceptual benefits of OKLAB while offering a more intuitive format for color manipulation.
776+
*
750777
* <a href="#/p5.Color">p5.Color</a> objects remember the mode that they were
751778
* created in. Changing modes doesn't affect their appearance.
752779
*
@@ -843,6 +870,83 @@ function setting(p5, fn){
843870
* <code>
844871
* function setup() {
845872
* createCanvas(100, 100);
873+
*
874+
* // Draw a neutral gray background using the default color mode.
875+
* background(200);
876+
*
877+
* // Switch to HWB color mode.
878+
* // (Assuming p5.js supports HWB with a range of:
879+
* // hue: 0–360, whiteness: 0–100, blackness: 0–100.)
880+
* colorMode(HWB);
881+
*
882+
* // Set fill to pure red in HWB.
883+
* // Pure red in HWB is: hue = 0°, whiteness = 0%, blackness = 0%.
884+
* fill(0, 0, 0);
885+
*
886+
* // Draw a circle at the center.
887+
* circle(50, 50, 25);
888+
*
889+
* describe('A gray square with a red circle at its center, drawn using HWB color mode.');
890+
* }
891+
* </code>
892+
* </div>
893+
*
894+
* @example
895+
* <div>
896+
* <code>
897+
* function setup() {
898+
* createCanvas(100, 100);
899+
*
900+
* // Draw a neutral gray background using the default color mode.
901+
* background(200);
902+
*
903+
* // Switch to LAB color mode.
904+
* // In this mode, L typically ranges from 0 to 100 while a and b span roughly -128 to 127.
905+
* colorMode(LAB);
906+
*
907+
* // Set fill to pure red in LAB.
908+
* // The sRGB red (255, 0, 0) converts approximately to LAB as:
909+
* // L = 53, a = 80, b = 67.
910+
* fill(53, 80, 67);
911+
*
912+
* // Draw a circle at the center.
913+
* circle(50, 50, 25);
914+
*
915+
* describe('A gray square with a red circle at its center, drawn using LAB color mode.');
916+
* }
917+
* </code>
918+
* </div>
919+
*
920+
* @example
921+
* <div>
922+
* <code>
923+
* function setup() {
924+
* createCanvas(100, 100);
925+
*
926+
* // Draw a neutral gray background.
927+
* background(200);
928+
*
929+
* // Switch to LCH color mode.
930+
* // In LCH, colors are defined by Lightness, Chroma, and Hue (in degrees).
931+
* colorMode(LCH);
932+
*
933+
* // Set fill to an approximation of pure red in LCH:
934+
* // Lightness ≈ 53, Chroma ≈ 104, Hue ≈ 40°.
935+
* fill(53, 104, 40);
936+
*
937+
* // Draw a circle at the center.
938+
* circle(50, 50, 25);
939+
*
940+
* describe('A gray square with a red circle at its center, drawn using LCH color mode.');
941+
* }
942+
* </code>
943+
* </div>
944+
*
945+
* @example
946+
* <div>
947+
* <code>
948+
* function setup() {
949+
* createCanvas(100, 100);
846950
*
847951
* // Use RGB color with values in the range 0-100.
848952
* colorMode(RGB, 100);
@@ -933,8 +1037,62 @@ function setting(p5, fn){
9331037
* }
9341038
* </code>
9351039
* </div>
1040+
*
1041+
* @example
1042+
* <div>
1043+
* <code>
1044+
* let hslGraphic, lchGraphic, oklchGraphic;
1045+
*
1046+
* function setup() {
1047+
* createCanvas(600, 200);
1048+
* noLoop();
1049+
*
1050+
* // Create three graphics objects for HSL, LCH, and OKLCH color modes
1051+
* hslGraphic = createGraphics(200, 200);
1052+
* lchGraphic = createGraphics(200, 200);
1053+
* oklchGraphic = createGraphics(200, 200);
1054+
*
1055+
* // Draw HSL color wheel
1056+
* colorMode(HSL);
1057+
* hslGraphic.translate(100, 100);
1058+
* for (let i = 0; i < 1000; i++) {
1059+
* hslGraphic.stroke(360 / 1000 * i, 70, 50);
1060+
* hslGraphic.line(0, 0, hslGraphic.width / 2, 0);
1061+
* hslGraphic.rotate(TAU / 1000);
1062+
* }
1063+
*
1064+
* // Draw LCH color wheel
1065+
* colorMode(LCH);
1066+
* lchGraphic.translate(100, 100);
1067+
* for (let i = 0; i < 1000; i++) {
1068+
* lchGraphic.stroke(54, 106, 360 / 1000 * i);
1069+
* lchGraphic.line(0, 0, lchGraphic.width / 2, 0);
1070+
* lchGraphic.rotate(TAU / 1000);
1071+
* }
1072+
*
1073+
* // Draw OKLCH color wheel
1074+
* colorMode(OKLCH);
1075+
* oklchGraphic.translate(100, 100);
1076+
* for (let i = 0; i < 1000; i++) {
1077+
* oklchGraphic.stroke(54, 106, 360 / 1000 * i);
1078+
* oklchGraphic.line(0, 0, oklchGraphic.width / 2, 0);
1079+
* oklchGraphic.rotate(TAU / 1000);
1080+
* }
1081+
* }
1082+
*
1083+
* function draw() {
1084+
* // Set the styles
1085+
* colorMode(RGB);
1086+
* background(220);
1087+
*
1088+
* // Display the color wheels
1089+
* image(hslGraphic, 0, 0);
1090+
* image(lchGraphic, 200, 0);
1091+
* image(oklchGraphic, 400, 0);
1092+
* }
1093+
* </code>
1094+
* </div>
9361095
*/
937-
9381096
/**
9391097
* @method colorMode
9401098
* @param {RGB|HSB|HSL|RGBHDR|HWB|LAB|LCH|OKLAB|OKLCH} mode

0 commit comments

Comments
 (0)