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