p5 2.0.0 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (188) hide show
  1. package/{src → dist}/accessibility/color_namer.js +48 -3
  2. package/{src → dist}/accessibility/describe.js +2 -2
  3. package/{src → dist}/accessibility/gridOutput.js +2 -2
  4. package/dist/accessibility/index.js +60 -0
  5. package/{src → dist}/accessibility/outputs.js +2 -2
  6. package/{src → dist}/accessibility/textOutput.js +2 -2
  7. package/dist/app.js +120 -0
  8. package/{src → dist}/color/color_conversion.js +48 -10
  9. package/{src → dist}/color/color_spaces/hsb.js +3 -1
  10. package/dist/color/creating_reading.js +3 -0
  11. package/dist/color/index.js +13 -0
  12. package/dist/color/p5.Color.culori.js +1 -0
  13. package/dist/color/p5.Color.js +3 -0
  14. package/{src → dist}/color/setting.js +9 -6
  15. package/{src/core/constants.js → dist/constants-C-g_eAdC.js} +266 -130
  16. package/{src → dist}/core/States.js +3 -1
  17. package/dist/core/constants.js +1 -0
  18. package/{src → dist}/core/environment.js +7 -6
  19. package/{src → dist}/core/friendly_errors/browser_errors.js +1 -1
  20. package/{src → dist}/core/friendly_errors/fes_core.js +14 -44
  21. package/{src → dist}/core/friendly_errors/file_errors.js +6 -3
  22. package/dist/core/friendly_errors/index.js +23 -0
  23. package/dist/core/friendly_errors/param_validator.js +5455 -0
  24. package/{src → dist}/core/friendly_errors/sketch_reader.js +50 -4
  25. package/{src → dist}/core/friendly_errors/sketch_verifier.js +6 -6
  26. package/{src → dist}/core/friendly_errors/stacktrace.js +3 -5
  27. package/{src → dist}/core/friendly_errors/validate_params.js +50 -41
  28. package/{src → dist}/core/helpers.js +9 -6
  29. package/dist/core/init.js +105 -0
  30. package/dist/core/internationalization.js +302 -0
  31. package/dist/core/legacy.js +73 -0
  32. package/dist/core/main.js +44 -0
  33. package/dist/core/noop.js +3 -0
  34. package/dist/core/p5.Graphics.js +40 -0
  35. package/dist/core/p5.Renderer.js +11 -0
  36. package/dist/core/p5.Renderer2D.js +44 -0
  37. package/dist/core/reference.js +1 -0
  38. package/dist/core/rendering.js +40 -0
  39. package/{src → dist}/core/structure.js +3 -3
  40. package/{src → dist}/core/transform.js +2 -2
  41. package/{src/color/creating_reading.js → dist/creating_reading-D4AAKRbx.js} +841 -13
  42. package/{src → dist}/data/index.js +3 -1
  43. package/{src → dist}/data/local_storage.js +2 -8
  44. package/{src → dist}/dom/dom.js +11 -5
  45. package/dist/dom/index.js +18 -0
  46. package/{src → dist}/dom/p5.Element.js +14 -12
  47. package/{src → dist}/dom/p5.File.js +4 -4
  48. package/{src → dist}/dom/p5.MediaElement.js +10 -4
  49. package/{src → dist}/events/acceleration.js +2 -2
  50. package/{src → dist}/events/index.js +3 -1
  51. package/{src → dist}/events/keyboard.js +14 -11
  52. package/{src → dist}/events/pointer.js +16 -17
  53. package/dist/image/const.js +9 -0
  54. package/{src → dist}/image/filterRenderer2D.js +57 -37
  55. package/{src → dist}/image/filters.js +1 -3
  56. package/dist/image/image.js +40 -0
  57. package/dist/image/index.js +51 -0
  58. package/dist/image/loading_displaying.js +40 -0
  59. package/dist/image/p5.Image.js +11 -0
  60. package/{src → dist}/image/pixels.js +4 -3
  61. package/{src → dist}/io/csv.js +72 -70
  62. package/dist/io/files.js +40 -0
  63. package/dist/io/index.js +51 -0
  64. package/{src → dist}/io/p5.Table.js +6 -6
  65. package/{src → dist}/io/p5.TableRow.js +3 -4
  66. package/{src → dist}/io/p5.XML.js +2 -5
  67. package/{src → dist}/io/utilities.js +1 -1
  68. package/{src/core/p5.Renderer2D.js → dist/main-s72KWcUy.js} +735 -57
  69. package/{src → dist}/math/Matrices/Matrix.js +10 -8
  70. package/{src → dist}/math/Matrices/MatrixInterface.js +5 -3
  71. package/{src → dist}/math/Matrices/MatrixNumjs.js +12 -26
  72. package/{src → dist}/math/calculation.js +2 -2
  73. package/{src → dist}/math/index.js +6 -3
  74. package/{src → dist}/math/math.js +2 -2
  75. package/{src → dist}/math/noise.js +2 -2
  76. package/{src → dist}/math/p5.Matrix.js +7 -4
  77. package/{src → dist}/math/p5.Vector.js +6 -6
  78. package/{src → dist}/math/random.js +2 -2
  79. package/{src → dist}/math/trigonometry.js +16 -15
  80. package/{src/image/p5.Image.js → dist/p5.Renderer-CwAYZOC2.js} +390 -19
  81. package/dist/rendering--aAe5aq3.js +24925 -0
  82. package/{src → dist}/shape/2d_primitives.js +18 -17
  83. package/{src → dist}/shape/attributes.js +18 -17
  84. package/{src → dist}/shape/curves.js +2 -2
  85. package/{src → dist}/shape/custom_shapes.js +44 -64
  86. package/{src → dist}/shape/index.js +10 -2
  87. package/{src → dist}/shape/vertex.js +2 -3
  88. package/dist/type/index.js +25 -0
  89. package/{src → dist}/type/lib/Typr.js +76 -94
  90. package/{src → dist}/type/p5.Font.js +37 -61
  91. package/{src → dist}/type/textCore.js +34 -57
  92. package/{src → dist}/type/unicodeRanges.js +3 -1
  93. package/{src → dist}/utilities/conversion.js +2 -2
  94. package/{src → dist}/utilities/index.js +3 -1
  95. package/{src → dist}/utilities/time_date.js +6 -7
  96. package/{src → dist}/utilities/utility_functions.js +2 -2
  97. package/dist/webgl/3d_primitives.js +40 -0
  98. package/{src → dist}/webgl/GeometryBufferCache.js +3 -1
  99. package/{src → dist}/webgl/GeometryBuilder.js +12 -8
  100. package/{src → dist}/webgl/ShaderGenerator.js +79 -82
  101. package/{src → dist}/webgl/ShapeBuilder.js +26 -23
  102. package/dist/webgl/index.js +76 -0
  103. package/{src → dist}/webgl/interaction.js +7 -6
  104. package/dist/webgl/light.js +40 -0
  105. package/{src → dist}/webgl/loading.js +45 -12
  106. package/dist/webgl/material.js +40 -0
  107. package/dist/webgl/p5.Camera.js +40 -0
  108. package/{src → dist}/webgl/p5.DataArray.js +3 -5
  109. package/dist/webgl/p5.Framebuffer.js +40 -0
  110. package/{src → dist}/webgl/p5.Geometry.js +12 -15
  111. package/{src → dist}/webgl/p5.Quat.js +5 -4
  112. package/{src → dist}/webgl/p5.RenderBuffer.js +2 -3
  113. package/dist/webgl/p5.RendererGL.js +40 -0
  114. package/dist/webgl/p5.Shader.js +40 -0
  115. package/dist/webgl/p5.Texture.js +40 -0
  116. package/{src → dist}/webgl/text.js +51 -9
  117. package/lib/p5.esm.js +102 -48
  118. package/lib/p5.js +102 -48
  119. package/lib/p5.min.js +1 -1
  120. package/package.json +17 -16
  121. package/translations/dev.js +6 -6
  122. package/translations/index.js +1 -1
  123. package/src/README.md +0 -27
  124. package/src/accessibility/index.js +0 -13
  125. package/src/app.js +0 -61
  126. package/src/color/index.js +0 -9
  127. package/src/color/p5.Color.culori.js +0 -66
  128. package/src/color/p5.Color.js +0 -851
  129. package/src/core/README.md +0 -91
  130. package/src/core/friendly_errors/index.js +0 -13
  131. package/src/core/friendly_errors/param_validator.js +0 -561
  132. package/src/core/init.js +0 -58
  133. package/src/core/internationalization.js +0 -195
  134. package/src/core/legacy.js +0 -29
  135. package/src/core/main.js +0 -689
  136. package/src/core/noop.js +0 -1
  137. package/src/core/p5.Graphics.js +0 -696
  138. package/src/core/p5.Renderer.js +0 -408
  139. package/src/core/reference.js +0 -2060
  140. package/src/core/rendering.js +0 -697
  141. package/src/dom/index.js +0 -11
  142. package/src/image/const.js +0 -6
  143. package/src/image/image.js +0 -731
  144. package/src/image/index.js +0 -15
  145. package/src/image/loading_displaying.js +0 -1431
  146. package/src/io/files.js +0 -2210
  147. package/src/io/index.js +0 -11
  148. package/src/math/README.md +0 -40
  149. package/src/type/index.js +0 -9
  150. package/src/webgl/3d_primitives.js +0 -2741
  151. package/src/webgl/index.js +0 -37
  152. package/src/webgl/light.js +0 -1851
  153. package/src/webgl/material.js +0 -3854
  154. package/src/webgl/p5.Camera.js +0 -4010
  155. package/src/webgl/p5.Framebuffer.js +0 -1865
  156. package/src/webgl/p5.RendererGL.js +0 -2867
  157. package/src/webgl/p5.Shader.js +0 -1505
  158. package/src/webgl/p5.Texture.js +0 -541
  159. package/src/webgl/shaders/basic.frag +0 -6
  160. package/src/webgl/shaders/filters/base.frag +0 -22
  161. package/src/webgl/shaders/filters/base.vert +0 -19
  162. package/src/webgl/shaders/filters/blur.frag +0 -60
  163. package/src/webgl/shaders/filters/default.vert +0 -18
  164. package/src/webgl/shaders/filters/dilate.frag +0 -39
  165. package/src/webgl/shaders/filters/erode.frag +0 -39
  166. package/src/webgl/shaders/filters/gray.frag +0 -16
  167. package/src/webgl/shaders/filters/invert.frag +0 -15
  168. package/src/webgl/shaders/filters/opaque.frag +0 -12
  169. package/src/webgl/shaders/filters/posterize.frag +0 -29
  170. package/src/webgl/shaders/filters/threshold.frag +0 -23
  171. package/src/webgl/shaders/font.frag +0 -216
  172. package/src/webgl/shaders/font.vert +0 -44
  173. package/src/webgl/shaders/imageLight.vert +0 -33
  174. package/src/webgl/shaders/imageLightDiffused.frag +0 -82
  175. package/src/webgl/shaders/imageLightSpecular.frag +0 -134
  176. package/src/webgl/shaders/light.vert +0 -37
  177. package/src/webgl/shaders/light_texture.frag +0 -26
  178. package/src/webgl/shaders/lighting.glsl +0 -227
  179. package/src/webgl/shaders/line.frag +0 -74
  180. package/src/webgl/shaders/line.vert +0 -294
  181. package/src/webgl/shaders/normal.frag +0 -6
  182. package/src/webgl/shaders/normal.vert +0 -72
  183. package/src/webgl/shaders/phong.frag +0 -84
  184. package/src/webgl/shaders/phong.vert +0 -87
  185. package/src/webgl/shaders/point.frag +0 -29
  186. package/src/webgl/shaders/point.vert +0 -19
  187. package/src/webgl/shaders/sphereMapping.frag +0 -26
  188. package/src/webgl/shaders/webgl2Compatibility.glsl +0 -34
@@ -1,3 +1,832 @@
1
+ import { sRGB, P3, HSL as HSL$1, HWB as HWB$1, Lab, LCH as LCH$1, OKLab, OKLCH as OKLCH$1, ColorSpace, to, parse, range, serialize } from 'colorjs.io/fn';
2
+ import HSBSpace from './color/color_spaces/hsb.js';
3
+
4
+ /**
5
+ * @module Color
6
+ * @submodule Creating & Reading
7
+ * @for p5
8
+ * @requires core
9
+ * @requires color_conversion
10
+ */
11
+
12
+
13
+ const map = (n, start1, stop1, start2, stop2, clamp) => {
14
+ let result = ((n - start1) / (stop1 - start1) * (stop2 - start2) + start2);
15
+ if (clamp) {
16
+ result = Math.max(result, Math.min(start2, stop2));
17
+ result = Math.min(result, Math.max(start2, stop2));
18
+ }
19
+ return result;
20
+ };
21
+
22
+ const serializationMap = {};
23
+
24
+ class Color {
25
+ // Reference to underlying color object depending on implementation
26
+ // Not meant to be used publicly unless the implementation is known for sure
27
+ _color;
28
+ // Color mode of the Color object, uses p5 color modes
29
+ mode;
30
+
31
+ static colorMap = {};
32
+ static #colorjsMaxes = {};
33
+ static #grayscaleMap = {};
34
+
35
+ // Used to add additional color modes to p5.js
36
+ // Uses underlying library's definition
37
+ static addColorMode(mode, definition){
38
+ ColorSpace.register(definition);
39
+ Color.colorMap[mode] = definition.id;
40
+
41
+ // Get colorjs maxes
42
+ Color.#colorjsMaxes[mode] = Object.values(definition.coords).reduce((acc, v) => {
43
+ acc.push(v.refRange || v.range);
44
+ return acc;
45
+ }, []);
46
+ Color.#colorjsMaxes[mode].push([0, 1]);
47
+
48
+ // Get grayscale mapping
49
+ Color.#grayscaleMap[mode] = definition.fromGray;
50
+ }
51
+
52
+ constructor(vals, colorMode, colorMaxes, { clamp = false } = {}) {
53
+ // This changes with the color object
54
+ this.mode = colorMode || RGB;
55
+
56
+ if(vals instanceof Color){
57
+ // Received Color object to be used for color mode conversion
58
+ const mode = colorMode ?
59
+ Color.colorMap[colorMode] :
60
+ Color.colorMap[vals.mode];
61
+ this._color = to(vals._color, mode);
62
+ this.mode = mode;
63
+
64
+ }else if (typeof vals === 'object' && !Array.isArray(vals) && vals !== null){
65
+ // Received color.js object to be used internally
66
+ const mode = colorMode ?
67
+ Color.colorMap[colorMode] :
68
+ vals.spaceId;
69
+ this._color = to(vals, mode);
70
+ this.mode = colorMode || Object.entries(Color.colorMap).find(([key, val]) => {
71
+ return val === this._color.spaceId;
72
+ });
73
+
74
+ } else if(typeof vals[0] === 'string') {
75
+ // Received string
76
+ try{
77
+ this._color = parse(vals[0]);
78
+ const [mode] = Object.entries(Color.colorMap).find(([key, val]) => {
79
+ return val === this._color.spaceId;
80
+ });
81
+ this.mode = mode;
82
+ this._color = to(this._color, this._color.spaceId);
83
+ }catch(err){
84
+ // TODO: Invalid color string
85
+ throw new Error('Invalid color string');
86
+ }
87
+
88
+ }else {
89
+ // Received individual channel values
90
+ let mappedVals;
91
+
92
+ if(colorMaxes){
93
+ // NOTE: need to consider different number of arguments (eg. CMYK)
94
+ if(vals.length === 4){
95
+ mappedVals = Color.mapColorRange(vals, this.mode, colorMaxes, clamp);
96
+ }else if(vals.length === 3){
97
+ mappedVals = Color.mapColorRange([vals[0], vals[1], vals[2]], this.mode, colorMaxes, clamp);
98
+ mappedVals.push(1);
99
+ }else if(vals.length === 2){
100
+ // Grayscale with alpha
101
+ if(Color.#grayscaleMap[this.mode]){
102
+ mappedVals = Color.#grayscaleMap[this.mode](vals[0], colorMaxes, clamp);
103
+ }else {
104
+ mappedVals = Color.mapColorRange([vals[0], vals[0], vals[0]], this.mode, colorMaxes, clamp);
105
+ }
106
+ const alphaMaxes = Array.isArray(colorMaxes[colorMaxes.length-1]) ?
107
+ colorMaxes[colorMaxes.length-1] :
108
+ [0, colorMaxes[colorMaxes.length-1]];
109
+ mappedVals.push(
110
+ map(
111
+ vals[1],
112
+ alphaMaxes[0],
113
+ alphaMaxes[1],
114
+ 0,
115
+ 1,
116
+ clamp
117
+ )
118
+ );
119
+ }else if(vals.length === 1){
120
+ // Grayscale only
121
+ if(Color.#grayscaleMap[this.mode]){
122
+ mappedVals = Color.#grayscaleMap[this.mode](vals[0], colorMaxes, clamp);
123
+ }else {
124
+ mappedVals = Color.mapColorRange([vals[0], vals[0], vals[0]], this.mode, colorMaxes, clamp);
125
+ }
126
+ mappedVals.push(1);
127
+ }else {
128
+ throw new Error('Invalid color');
129
+ }
130
+ }else {
131
+ mappedVals = vals;
132
+ }
133
+
134
+ const space = Color.colorMap[this.mode] || console.error('Invalid color mode');
135
+ const coords = mappedVals.slice(0, 3);
136
+
137
+ const color = {
138
+ space,
139
+ coords,
140
+ alpha: mappedVals[3]
141
+ };
142
+ this._color = to(color, space);
143
+ }
144
+ }
145
+
146
+ // Convert from p5 color range to color.js color range
147
+ static mapColorRange(origin, mode, maxes, clamp){
148
+ const p5Maxes = maxes.map((max) => {
149
+ if(!Array.isArray(max)){
150
+ return [0, max];
151
+ }else {
152
+ return max;
153
+ }
154
+ });
155
+ const colorjsMaxes = Color.#colorjsMaxes[mode];
156
+
157
+ return origin.map((channel, i) => {
158
+ const newval = map(channel, p5Maxes[i][0], p5Maxes[i][1], colorjsMaxes[i][0], colorjsMaxes[i][1], clamp);
159
+ return newval;
160
+ });
161
+ }
162
+
163
+ // Convert from color.js color range to p5 color range
164
+ static unmapColorRange(origin, mode, maxes){
165
+ const p5Maxes = maxes.map((max) => {
166
+ if(!Array.isArray(max)){
167
+ return [0, max];
168
+ }else {
169
+ return max;
170
+ }
171
+ });
172
+ const colorjsMaxes = Color.#colorjsMaxes[mode];
173
+
174
+ return origin.map((channel, i) => {
175
+ const newval = map(channel, colorjsMaxes[i][0], colorjsMaxes[i][1], p5Maxes[i][0], p5Maxes[i][1]);
176
+ return newval;
177
+ });
178
+ }
179
+
180
+ // Will do conversion in-Gamut as out of Gamut conversion is only really useful for futher conversions
181
+ #toColorMode(mode){
182
+ return new Color(this._color, mode);
183
+ }
184
+
185
+ // Get raw coordinates of underlying library, can differ between libraries
186
+ get _array() {
187
+ return this._getRGBA();
188
+ }
189
+
190
+ array(){
191
+ return this._array;
192
+ }
193
+
194
+ lerp(color, amt, mode){
195
+ // Find the closest common ancestor color space
196
+ let spaceIndex = -1;
197
+ while(
198
+ (
199
+ spaceIndex+1 < this._color.space.path.length ||
200
+ spaceIndex+1 < color._color.space.path.length
201
+ ) &&
202
+ this._color.space.path[spaceIndex+1] === color._color.space.path[spaceIndex+1]
203
+ ){
204
+ spaceIndex += 1;
205
+ }
206
+
207
+ if (spaceIndex === -1) {
208
+ // This probably will not occur in practice
209
+ throw new Error('Cannot lerp colors. No common color space found');
210
+ }
211
+
212
+ const obj = range(this._color, color._color, {
213
+ space: this._color.space.path[spaceIndex].id
214
+ })(amt);
215
+
216
+ return new Color(obj, mode || this.mode);
217
+ }
218
+
219
+ /**
220
+ * Returns the color formatted as a `String`.
221
+ *
222
+ * Calling `myColor.toString()` can be useful for debugging, as in
223
+ * `print(myColor.toString())`. It's also helpful for using p5.js with other
224
+ * libraries.
225
+ *
226
+ * The parameter, `format`, is optional. If a format string is passed, as in
227
+ * `myColor.toString('#rrggbb')`, it will determine how the color string is
228
+ * formatted. By default, color strings are formatted as `'rgba(r, g, b, a)'`.
229
+ *
230
+ * @param {String} [format] how the color string will be formatted.
231
+ * Leaving this empty formats the string as rgba(r, g, b, a).
232
+ * '#rgb' '#rgba' '#rrggbb' and '#rrggbbaa' format as hexadecimal color codes.
233
+ * 'rgb' 'hsb' and 'hsl' return the color formatted in the specified color mode.
234
+ * 'rgba' 'hsba' and 'hsla' are the same as above but with alpha channels.
235
+ * 'rgb%' 'hsb%' 'hsl%' 'rgba%' 'hsba%' and 'hsla%' format as percentages.
236
+ * @return {String} the formatted string.
237
+ *
238
+ * @example
239
+ * <div>
240
+ * <code>
241
+ * function setup() {
242
+ * createCanvas(100, 100);
243
+ *
244
+ * background(200);
245
+ *
246
+ * // Create a p5.Color object.
247
+ * let myColor = color('darkorchid');
248
+ *
249
+ * // Style the text.
250
+ * textAlign(CENTER);
251
+ * textSize(16);
252
+ *
253
+ * // Display the text.
254
+ * text(myColor.toString('#rrggbb'), 50, 50);
255
+ *
256
+ * describe('The text "#9932cc" written in purple on a gray background.');
257
+ * }
258
+ * </code>
259
+ * </div>
260
+ */
261
+ toString(format) {
262
+ const key = `${this._color.space.id}-${this._color.coords.join(",")}-${this._color.alpha}-${format}`;
263
+ let colorString = serializationMap[key];
264
+
265
+ if(!colorString){
266
+ colorString = serialize(this._color, {
267
+ format
268
+ });
269
+ serializationMap[key] = colorString;
270
+ }
271
+ return colorString;
272
+ }
273
+
274
+ /**
275
+ * Sets the red component of a color.
276
+ *
277
+ * The range depends on the <a href="#/p5/colorMode">colorMode()</a>. In the
278
+ * default RGB mode it's between 0 and 255.
279
+ *
280
+ * @param {Number} red the new red value.
281
+ *
282
+ * @example
283
+ * <div>
284
+ * <code>
285
+ * function setup() {
286
+ * createCanvas(100, 100);
287
+ *
288
+ * background(200);
289
+ *
290
+ * // Create a p5.Color object.
291
+ * let c = color(255, 128, 128);
292
+ *
293
+ * // Draw the left rectangle.
294
+ * noStroke();
295
+ * fill(c);
296
+ * rect(15, 20, 35, 60);
297
+ *
298
+ * // Change the red value.
299
+ * c.setRed(64);
300
+ *
301
+ * // Draw the right rectangle.
302
+ * fill(c);
303
+ * rect(50, 20, 35, 60);
304
+ *
305
+ * describe('Two rectangles. The left one is salmon pink and the right one is teal.');
306
+ * }
307
+ * </code>
308
+ * </div>
309
+ */
310
+ setRed(new_red, max=[0, 1]) {
311
+ if(!Array.isArray(max)){
312
+ max = [0, max];
313
+ }
314
+
315
+ const colorjsMax = Color.#colorjsMaxes[RGB][0];
316
+ const newval = map(new_red, max[0], max[1], colorjsMax[0], colorjsMax[1]);
317
+
318
+ if(this.mode === RGB || this.mode === RGBHDR){
319
+ this._color.coords[0] = newval;
320
+ }else {
321
+ // Will do an imprecise conversion to 'srgb', not recommended
322
+ const space = this._color.space.id;
323
+ const representation = to(this._color, 'srgb');
324
+ representation.coords[0] = newval;
325
+ this._color = to(representation, space);
326
+ }
327
+ }
328
+
329
+ /**
330
+ * Sets the green component of a color.
331
+ *
332
+ * The range depends on the <a href="#/p5/colorMode">colorMode()</a>. In the
333
+ * default RGB mode it's between 0 and 255.
334
+ *
335
+ * @param {Number} green the new green value.
336
+ *
337
+ * @example
338
+ * <div>
339
+ * <code>
340
+ * function setup() {
341
+ * createCanvas(100, 100);
342
+ *
343
+ * background(200);
344
+ *
345
+ * // Create a p5.Color object.
346
+ * let c = color(255, 128, 128);
347
+ *
348
+ * // Draw the left rectangle.
349
+ * noStroke();
350
+ * fill(c);
351
+ * rect(15, 20, 35, 60);
352
+ *
353
+ * // Change the green value.
354
+ * c.setGreen(255);
355
+ *
356
+ * // Draw the right rectangle.
357
+ * fill(c);
358
+ * rect(50, 20, 35, 60);
359
+ *
360
+ * describe('Two rectangles. The left one is salmon pink and the right one is yellow.');
361
+ * }
362
+ * </code>
363
+ * </div>
364
+ **/
365
+ setGreen(new_green, max=[0, 1]) {
366
+ if(!Array.isArray(max)){
367
+ max = [0, max];
368
+ }
369
+
370
+ const colorjsMax = Color.#colorjsMaxes[RGB][1];
371
+ const newval = map(new_green, max[0], max[1], colorjsMax[0], colorjsMax[1]);
372
+
373
+ if(this.mode === RGB || this.mode === RGBHDR){
374
+ this._color.coords[1] = newval;
375
+ }else {
376
+ // Will do an imprecise conversion to 'srgb', not recommended
377
+ const space = this._color.space.id;
378
+ const representation = to(this._color, 'srgb');
379
+ representation.coords[1] = newval;
380
+ this._color = to(representation, space);
381
+ }
382
+ }
383
+
384
+ /**
385
+ * Sets the blue component of a color.
386
+ *
387
+ * The range depends on the <a href="#/p5/colorMode">colorMode()</a>. In the
388
+ * default RGB mode it's between 0 and 255.
389
+ *
390
+ * @param {Number} blue the new blue value.
391
+ *
392
+ * @example
393
+ * <div>
394
+ * <code>
395
+ * function setup() {
396
+ * createCanvas(100, 100);
397
+ *
398
+ * background(200);
399
+ *
400
+ * // Create a p5.Color object.
401
+ * let c = color(255, 128, 128);
402
+ *
403
+ * // Draw the left rectangle.
404
+ * noStroke();
405
+ * fill(c);
406
+ * rect(15, 20, 35, 60);
407
+ *
408
+ * // Change the blue value.
409
+ * c.setBlue(255);
410
+ *
411
+ * // Draw the right rectangle.
412
+ * fill(c);
413
+ * rect(50, 20, 35, 60);
414
+ *
415
+ * describe('Two rectangles. The left one is salmon pink and the right one is pale fuchsia.');
416
+ * }
417
+ * </code>
418
+ * </div>
419
+ **/
420
+ setBlue(new_blue, max=[0, 1]) {
421
+ if(!Array.isArray(max)){
422
+ max = [0, max];
423
+ }
424
+
425
+ const colorjsMax = Color.#colorjsMaxes[RGB][2];
426
+ const newval = map(new_blue, max[0], max[1], colorjsMax[0], colorjsMax[1]);
427
+
428
+ if(this.mode === RGB || this.mode === RGBHDR){
429
+ this._color.coords[2] = newval;
430
+ }else {
431
+ // Will do an imprecise conversion to 'srgb', not recommended
432
+ const space = this._color.space.id;
433
+ const representation = to(this._color, 'srgb');
434
+ representation.coords[2] = newval;
435
+ this._color = to(representation, space);
436
+ }
437
+ }
438
+
439
+ /**
440
+ * Sets the alpha (transparency) value of a color.
441
+ *
442
+ * The range depends on the
443
+ * <a href="#/p5/colorMode">colorMode()</a>. In the default RGB mode it's
444
+ * between 0 and 255.
445
+ *
446
+ * @param {Number} alpha the new alpha value.
447
+ *
448
+ * @example
449
+ * <div>
450
+ * <code>
451
+ * function setup() {
452
+ * createCanvas(100, 100);
453
+ *
454
+ * background(200);
455
+ *
456
+ * // Create a p5.Color object.
457
+ * let c = color(255, 128, 128);
458
+ *
459
+ * // Draw the left rectangle.
460
+ * noStroke();
461
+ * fill(c);
462
+ * rect(15, 20, 35, 60);
463
+ *
464
+ * // Change the alpha value.
465
+ * c.setAlpha(128);
466
+ *
467
+ * // Draw the right rectangle.
468
+ * fill(c);
469
+ * rect(50, 20, 35, 60);
470
+ *
471
+ * describe('Two rectangles. The left one is salmon pink and the right one is faded pink.');
472
+ * }
473
+ * </code>
474
+ * </div>
475
+ **/
476
+ setAlpha(new_alpha, max=[0, 1]) {
477
+ if(!Array.isArray(max)){
478
+ max = [0, max];
479
+ }
480
+
481
+ const colorjsMax = Color.#colorjsMaxes[this.mode][3];
482
+ const newval = map(new_alpha, max[0], max[1], colorjsMax[0], colorjsMax[1]);
483
+
484
+ this._color.alpha = newval;
485
+ }
486
+
487
+ _getRGBA(maxes=[1, 1, 1, 1]) {
488
+ // Get colorjs maxes
489
+ const colorjsMaxes = Color.#colorjsMaxes[RGB];
490
+
491
+ // Normalize everything to 0,1 or the provided range (map)
492
+ let coords = structuredClone(to(this._color, 'srgb').coords);
493
+ coords.push(this._color.alpha);
494
+
495
+ const rangeMaxes = maxes.map((v) => {
496
+ if(!Array.isArray(v)){
497
+ return [0, v];
498
+ }else {
499
+ return v
500
+ }
501
+ });
502
+
503
+ coords = coords.map((coord, i) => {
504
+ return map(coord, colorjsMaxes[i][0], colorjsMaxes[i][1], rangeMaxes[i][0], rangeMaxes[i][1]);
505
+ });
506
+
507
+ return coords;
508
+ }
509
+
510
+ _getMode() {
511
+ return this.mode;
512
+ }
513
+
514
+ _getRed(max=[0, 1]) {
515
+ if(!Array.isArray(max)){
516
+ max = [0, max];
517
+ }
518
+
519
+ if(this.mode === RGB || this.mode === RGBHDR){
520
+ const colorjsMax = Color.#colorjsMaxes[this.mode][0];
521
+ return map(this._color.coords[0], colorjsMax[0], colorjsMax[1], max[0], max[1]);
522
+ }else {
523
+ // Will do an imprecise conversion to 'srgb', not recommended
524
+ const colorjsMax = Color.#colorjsMaxes[RGB][0];
525
+ return map(to(this._color, 'srgb').coords[0], colorjsMax[0], colorjsMax[1], max[0], max[1]);
526
+ }
527
+ }
528
+
529
+ _getGreen(max=[0, 1]) {
530
+ if(!Array.isArray(max)){
531
+ max = [0, max];
532
+ }
533
+
534
+ if(this.mode === RGB || this.mode === RGBHDR){
535
+ const colorjsMax = Color.#colorjsMaxes[this.mode][1];
536
+ return map(this._color.coords[1], colorjsMax[0], colorjsMax[1], max[0], max[1]);
537
+ }else {
538
+ // Will do an imprecise conversion to 'srgb', not recommended
539
+ const colorjsMax = Color.#colorjsMaxes[RGB][1];
540
+ return map(to(this._color, 'srgb').coords[1], colorjsMax[0], colorjsMax[1], max[0], max[1]);
541
+ }
542
+ }
543
+
544
+ _getBlue(max=[0, 1]) {
545
+ if(!Array.isArray(max)){
546
+ max = [0, max];
547
+ }
548
+
549
+ if(this.mode === RGB || this.mode === RGBHDR){
550
+ const colorjsMax = Color.#colorjsMaxes[this.mode][2];
551
+ return map(this._color.coords[2], colorjsMax[0], colorjsMax[1], max[0], max[1]);
552
+ }else {
553
+ // Will do an imprecise conversion to 'srgb', not recommended
554
+ const colorjsMax = Color.#colorjsMaxes[RGB][2];
555
+ return map(to(this._color, 'srgb').coords[2], colorjsMax[0], colorjsMax[1], max[0], max[1]);
556
+ }
557
+ }
558
+
559
+ _getAlpha(max=[0, 1]) {
560
+ if(!Array.isArray(max)){
561
+ max = [0, max];
562
+ }
563
+
564
+ const colorjsMax = Color.#colorjsMaxes[this.mode][3];
565
+ return map(this._color.alpha, colorjsMax[0], colorjsMax[1], max[0], max[1]);
566
+ }
567
+
568
+ /**
569
+ * Hue is the same in HSB and HSL, but the maximum value may be different.
570
+ * This function will return the HSB-normalized saturation when supplied with
571
+ * an HSB color object, but will default to the HSL-normalized saturation
572
+ * otherwise.
573
+ */
574
+ _getHue(max=[0, 360]) {
575
+ if(!Array.isArray(max)){
576
+ max = [0, max];
577
+ }
578
+
579
+ if(this.mode === HSB || this.mode === HSL){
580
+ const colorjsMax = Color.#colorjsMaxes[this.mode][0];
581
+ return map(this._color.coords[0], colorjsMax[0], colorjsMax[1], max[0], max[1]);
582
+ }else {
583
+ // Will do an imprecise conversion to 'HSL', not recommended
584
+ const colorjsMax = Color.#colorjsMaxes[HSL][0];
585
+ return map(to(this._color, 'hsl').coords[0], colorjsMax[0], colorjsMax[1], max[0], max[1]);
586
+ }
587
+ }
588
+
589
+ /**
590
+ * Saturation is scaled differently in HSB and HSL. This function will return
591
+ * the HSB saturation when supplied with an HSB color object, but will default
592
+ * to the HSL saturation otherwise.
593
+ */
594
+ _getSaturation(max=[0, 100]) {
595
+ if(!Array.isArray(max)){
596
+ max = [0, max];
597
+ }
598
+
599
+ if(this.mode === HSB || this.mode === HSL){
600
+ const colorjsMax = Color.#colorjsMaxes[this.mode][1];
601
+ return map(this._color.coords[1], colorjsMax[0], colorjsMax[1], max[0], max[1]);
602
+ }else {
603
+ // Will do an imprecise conversion to 'HSL', not recommended
604
+ const colorjsMax = Color.#colorjsMaxes[HSL][1];
605
+ return map(to(this._color, 'hsl').coords[1], colorjsMax[0], colorjsMax[1], max[0], max[1]);
606
+ }
607
+ }
608
+
609
+ _getBrightness(max=[0, 100]) {
610
+ if(!Array.isArray(max)){
611
+ max = [0, max];
612
+ }
613
+
614
+ if(this.mode === HSB){
615
+ const colorjsMax = Color.#colorjsMaxes[this.mode][2];
616
+ return map(this._color.coords[2], colorjsMax[0], colorjsMax[1], max[0], max[1]);
617
+ }else {
618
+ // Will do an imprecise conversion to 'HSB', not recommended
619
+ const colorjsMax = Color.#colorjsMaxes[HSB][2];
620
+ return map(to(this._color, 'hsb').coords[2], colorjsMax[0], colorjsMax[1], max[0], max[1]);
621
+ }
622
+ }
623
+
624
+ _getLightness(max=[0, 100]) {
625
+ if(!Array.isArray(max)){
626
+ max = [0, max];
627
+ }
628
+
629
+ if(this.mode === HSL){
630
+ const colorjsMax = Color.#colorjsMaxes[this.mode][2];
631
+ return map(this._color.coords[2], colorjsMax[0], colorjsMax[1], max[0], max[1]);
632
+ }else {
633
+ // Will do an imprecise conversion to 'HSL', not recommended
634
+ const colorjsMax = Color.#colorjsMaxes[HSL][2];
635
+ return map(to(this._color, 'hsl').coords[2], colorjsMax[0], colorjsMax[1], max[0], max[1]);
636
+ }
637
+ }
638
+ }
639
+
640
+ function color(p5, fn, lifecycles){
641
+ /**
642
+ * A class to describe a color.
643
+ *
644
+ * Each `p5.Color` object stores the color mode
645
+ * and level maxes that were active during its construction. These values are
646
+ * used to interpret the arguments passed to the object's constructor. They
647
+ * also determine output formatting such as when
648
+ * <a href="#/p5/saturation">saturation()</a> is called.
649
+ *
650
+ * Color is stored internally as an array of ideal RGBA values in floating
651
+ * point form, normalized from 0 to 1. These values are used to calculate the
652
+ * closest screen colors, which are RGBA levels from 0 to 255. Screen colors
653
+ * are sent to the renderer.
654
+ *
655
+ * When different color representations are calculated, the results are cached
656
+ * for performance. These values are normalized, floating-point numbers.
657
+ *
658
+ * Note: <a href="#/p5/color">color()</a> is the recommended way to create an
659
+ * instance of this class.
660
+ *
661
+ * @class p5.Color
662
+ * @param {p5} [pInst] pointer to p5 instance.
663
+ *
664
+ * @param {Number[]|String} vals an array containing the color values
665
+ * for red, green, blue and alpha channel
666
+ * or CSS color.
667
+ */
668
+ p5.Color = Color;
669
+
670
+ sRGB.fromGray = P3.fromGray = function(val, maxes, clamp){
671
+ // Use blue max
672
+ const p5Maxes = maxes.map((max) => {
673
+ if(!Array.isArray(max)){
674
+ return [0, max];
675
+ }else {
676
+ return max;
677
+ }
678
+ });
679
+
680
+ const v = map(val, p5Maxes[2][0], p5Maxes[2][1], 0, 1, clamp);
681
+ return [v, v, v];
682
+ };
683
+
684
+ HSBSpace.fromGray = HSL$1.fromGray = function(val, maxes, clamp){
685
+ // Use brightness max
686
+ const p5Maxes = maxes.map((max) => {
687
+ if(!Array.isArray(max)){
688
+ return [0, max];
689
+ }else {
690
+ return max;
691
+ }
692
+ });
693
+
694
+ const v = map(val, p5Maxes[2][0], p5Maxes[2][1], 0, 100, clamp);
695
+ return [0, 0, v];
696
+ };
697
+
698
+ HWB$1.fromGray = function(val, maxes, clamp){
699
+ // Use Whiteness and Blackness to create number line
700
+ const p5Maxes = maxes.map((max) => {
701
+ if(!Array.isArray(max)){
702
+ return [0, max];
703
+ }else {
704
+ return max;
705
+ }
706
+ });
707
+
708
+ const wbMax =
709
+ (Math.abs(p5Maxes[1][0] - p5Maxes[1][1])) / 2 +
710
+ (Math.abs(p5Maxes[2][0] - p5Maxes[2][1])) / 2;
711
+
712
+ const nVal = map(val, 0, wbMax, 0, 100);
713
+ let white, black;
714
+ if(nVal < 50){
715
+ black = nVal;
716
+ white = 100 - nVal;
717
+ }else if(nVal >= 50){
718
+ white = nVal;
719
+ black = 100 - nVal;
720
+ }
721
+ return [0, white, black];
722
+ };
723
+
724
+ Lab.fromGray =
725
+ LCH$1.fromGray =
726
+ OKLab.fromGray =
727
+ OKLCH$1.fromGray =
728
+ function(val, maxes, clamp){
729
+ // Use lightness max
730
+ const p5Maxes = maxes.map((max) => {
731
+ if(!Array.isArray(max)){
732
+ return [0, max];
733
+ }else {
734
+ return max;
735
+ }
736
+ });
737
+
738
+ const v = map(val, p5Maxes[0][0], p5Maxes[0][1], 0, 100, clamp);
739
+ return [v, 0, 0];
740
+ };
741
+
742
+ // Register color modes and initialize Color maxes to what p5 has set for itself
743
+ p5.Color.addColorMode(RGB, sRGB);
744
+ p5.Color.addColorMode(RGBHDR, P3);
745
+ p5.Color.addColorMode(HSB, HSBSpace);
746
+ p5.Color.addColorMode(HSL, HSL$1);
747
+ p5.Color.addColorMode(HWB, HWB$1);
748
+ p5.Color.addColorMode(LAB, Lab);
749
+ p5.Color.addColorMode(LCH, LCH$1);
750
+ p5.Color.addColorMode(OKLAB, OKLab);
751
+ p5.Color.addColorMode(OKLCH, OKLCH$1);
752
+
753
+ lifecycles.presetup = function(){
754
+ const pInst = this;
755
+
756
+ // Decorate set methods
757
+ const setMethods = ['Red', 'Green', 'Blue', 'Alpha'];
758
+ for(let i in setMethods){
759
+ const method = setMethods[i];
760
+ const setCopy = p5.Color.prototype['set' + method];
761
+ p5.Color.prototype['set' + method] = function(newval, max){
762
+ max = max || pInst?._renderer?.states?.colorMaxes?.[RGB][i];
763
+ return setCopy.call(this, newval, max);
764
+ };
765
+ }
766
+
767
+ // Decorate get methods
768
+ function decorateGet(channel, modes){
769
+ const getCopy = p5.Color.prototype['_get' + channel];
770
+ p5.Color.prototype['_get' + channel] = function(max){
771
+ if(Object.keys(modes).includes(this.mode)){
772
+ max = max || pInst?._renderer?.states?.colorMaxes?.[this.mode][modes[this.mode]];
773
+ }else {
774
+ const defaultMode = Object.keys(modes)[0];
775
+ max = max || pInst?._renderer?.states?.colorMaxes?.[defaultMode][modes[defaultMode]];
776
+ }
777
+
778
+ return getCopy.call(this, max);
779
+ };
780
+ }
781
+
782
+ decorateGet('Red', {
783
+ [RGB]: 0,
784
+ [RGBHDR]: 0
785
+ });
786
+ decorateGet('Green', {
787
+ [RGB]: 1,
788
+ [RGBHDR]: 1
789
+ });
790
+ decorateGet('Blue', {
791
+ [RGB]: 2,
792
+ [RGBHDR]: 2
793
+ });
794
+ decorateGet('Alpha', {
795
+ [RGB]: 3,
796
+ [RGBHDR]: 3,
797
+ [HSB]: 3,
798
+ [HSL]: 3,
799
+ [HWB]: 3,
800
+ [LAB]: 3,
801
+ [LCH]: 3,
802
+ [OKLAB]: 3,
803
+ [OKLCH]: 3
804
+ });
805
+
806
+ decorateGet('Hue', {
807
+ [HSL]: 0,
808
+ [HSB]: 0,
809
+ [HWB]: 0,
810
+ [LCH]: 2,
811
+ [OKLCH]: 2
812
+ });
813
+ decorateGet('Saturation', {
814
+ [HSL]: 1,
815
+ [HSB]: 1
816
+ });
817
+ decorateGet('Brightness', {
818
+ [HSB]: 2
819
+ });
820
+ decorateGet('Lightness', {
821
+ [HSL]: 2
822
+ });
823
+ };
824
+ }
825
+
826
+ if(typeof p5 !== 'undefined'){
827
+ color(p5, p5.prototype);
828
+ }
829
+
1
830
  /**
2
831
  * @module Color
3
832
  * @submodule Creating & Reading
@@ -6,20 +835,19 @@
6
835
  * @requires constants
7
836
  */
8
837
 
9
- import { Color } from './p5.Color';
10
838
 
11
839
  /**
12
840
  * @typedef {'rgb'} RGB
13
841
  * @property {RGB} RGB
14
842
  * @final
15
843
  */
16
- export const RGB = 'rgb';
844
+ const RGB = 'rgb';
17
845
  /**
18
846
  * @typedef {'rgbhdr'} RGBHDR
19
847
  * @property {RGBHDR} RGBHDR
20
848
  * @final
21
849
  */
22
- export const RGBHDR = 'rgbhdr';
850
+ const RGBHDR = 'rgbhdr';
23
851
  /**
24
852
  * HSB (hue, saturation, brightness) is a type of color model.
25
853
  * You can learn more about it at
@@ -29,49 +857,49 @@ export const RGBHDR = 'rgbhdr';
29
857
  * @property {HSB} HSB
30
858
  * @final
31
859
  */
32
- export const HSB = 'hsb';
860
+ const HSB = 'hsb';
33
861
  /**
34
862
  * @typedef {'hsl'} HSL
35
863
  * @property {HSL} HSL
36
864
  * @final
37
865
  */
38
- export const HSL = 'hsl';
866
+ const HSL = 'hsl';
39
867
  /**
40
868
  * @typedef {'hwb'} HWB
41
869
  * @property {HWB} HWB
42
870
  * @final
43
871
  */
44
- export const HWB = 'hwb';
872
+ const HWB = 'hwb';
45
873
  /**
46
874
  * @typedef {'lab'} LAB
47
875
  * @property {LAB} LAB
48
876
  * @final
49
877
  */
50
- export const LAB = 'lab';
878
+ const LAB = 'lab';
51
879
  /**
52
880
  * @typedef {'lch'} LCH
53
881
  * @property {LCH} LCH
54
882
  * @final
55
883
  */
56
- export const LCH = 'lch';
884
+ const LCH = 'lch';
57
885
  /**
58
886
  * @typedef {'oklab'} OKLAB
59
887
  * @property {OKLAB} OKLAB
60
888
  * @final
61
889
  */
62
- export const OKLAB = 'oklab';
890
+ const OKLAB = 'oklab';
63
891
  /**
64
892
  * @typedef {'oklch'} OKLCH
65
893
  * @property {OKLCH} OKLCH
66
894
  * @final
67
895
  */
68
- export const OKLCH = 'oklch';
896
+ const OKLCH = 'oklch';
69
897
  /**
70
898
  * @typedef {'rgba'} RGBA
71
899
  * @property {RGBA} RGBA
72
900
  * @final
73
901
  */
74
- export const RGBA = 'rgba';
902
+ const RGBA = 'rgba';
75
903
 
76
904
  function creatingReading(p5, fn){
77
905
  fn.RGB = RGB;
@@ -1634,8 +2462,8 @@ function creatingReading(p5, fn){
1634
2462
  };
1635
2463
  }
1636
2464
 
1637
- export default creatingReading;
1638
-
1639
2465
  if(typeof p5 !== 'undefined'){
1640
2466
  creatingReading(p5, p5.prototype);
1641
2467
  }
2468
+
2469
+ export { Color as C, HSB as H, LAB as L, OKLAB as O, RGB as R, color as a, RGBHDR as b, creatingReading as c, HSL as d, HWB as e, LCH as f, OKLCH as g, RGBA as h };