Skip to content

Commit 093b2e9

Browse files
authored
Merge branch 'dev-2.0' into fix/simplifythreshold
2 parents 00ed93f + 65a8008 commit 093b2e9

File tree

17 files changed

+190
-28
lines changed

17 files changed

+190
-28
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
"test/**/*.js": "eslint",
2020
"utils/**/*.{js,mjs}": "eslint"
2121
},
22-
"version": "2.1.0-rc.0",
22+
"version": "2.1.0-rc.1",
2323
"dependencies": {
2424
"@davepagurek/bezier-path": "^0.0.2",
2525
"@japont/unicode-range": "^1.0.0",
@@ -82,6 +82,7 @@
8282
"./core": {
8383
"default": "./dist/core/main.js"
8484
},
85+
"./color": "./dist/color/index.js",
8586
"./shape": "./dist/shape/index.js",
8687
"./accessibility": "./dist/accessibility/index.js",
8788
"./friendlyErrors": "./dist/core/friendlyErrors/index.js",

src/color/p5.Color.js

Lines changed: 125 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import { RGB, RGBHDR, HSL, HSB, HWB, LAB, LCH, OKLAB, OKLCH } from './creating_reading';
1010

11+
1112
import {
1213
ColorSpace,
1314
to,
@@ -25,7 +26,8 @@ import {
2526

2627
OKLab,
2728
OKLCH as OKLCHSpace,
28-
29+
contrastWCAG21,
30+
contrastAPCA,
2931
P3
3032
} from 'colorjs.io/fn';
3133
import HSBSpace from './color_spaces/hsb.js';
@@ -41,6 +43,9 @@ const map = (n, start1, stop1, start2, stop2, clamp) => {
4143

4244
const serializationMap = {};
4345

46+
47+
48+
4449
class Color {
4550
// Reference to underlying color object depending on implementation
4651
// Not meant to be used publicly unless the implementation is known for sure
@@ -326,6 +331,125 @@ class Color {
326331
return colorString;
327332
}
328333

334+
/**
335+
* Checks the contrast between two colors. This method returns a boolean
336+
* value to indicate if the two color has enough contrast. `true` means that
337+
* the colors has enough contrast to be used as background color and body
338+
* text color. `false` means there is not enough contrast.
339+
*
340+
* A second argument can be passed to the method, `options` , which defines
341+
* the algorithm to be used. The algorithms currently supported are
342+
* WCAG 2.1 (`'WCAG21'`) or APCA (`'APCA'`). The default is WCAG 2.1. If a
343+
* value of `'all'` is passed to the `options` argument, an object containing
344+
* more details is returned. The details object will include the calculated
345+
* contrast value of the colors and different passing criteria.
346+
*
347+
* For more details about color contrast, you can check out
348+
* <a href="https://colorjs.io/docs/contrast">this page from color.js</a>, and the
349+
* <a href="https://webaim.org/resources/contrastchecker/">WebAIM color contrast checker.</a>
350+
*
351+
* @param {Color} other
352+
* @returns {boolean|object}
353+
* @example
354+
* <div>
355+
* <code>
356+
* let bgColor, fg1Color, fg2Color, msg1, msg2;
357+
* function setup() {
358+
* createCanvas(100, 100);
359+
* bgColor = color(0);
360+
* fg1Color = color(100);
361+
* fg2Color = color(220);
362+
*
363+
* if(bgColor.contrast(fg1Color)){
364+
* msg1 = 'good';
365+
* }else{
366+
* msg1 = 'bad';
367+
* }
368+
*
369+
* if(bgColor.contrast(fg2Color)){
370+
* msg2 = 'good';
371+
* }else{
372+
* msg2 = 'bad';
373+
* }
374+
*
375+
* describe('A black canvas with a faint grey word saying "bad" at the top left and a brighter light grey word saying "good" in the middle of the canvas.');
376+
* }
377+
*
378+
* function draw(){
379+
* background(bgColor);
380+
*
381+
* textSize(18);
382+
*
383+
* fill(fg1Color);
384+
* text(msg1, 10, 30);
385+
*
386+
* fill(fg2Color);
387+
* text(msg2, 10, 60);
388+
* }
389+
* </code>
390+
* </div>
391+
*
392+
* <div>
393+
* <code>
394+
* let bgColor, fgColor, contrast;
395+
* function setup() {
396+
* createCanvas(100, 100);
397+
* bgColor = color(0);
398+
* fgColor = color(200);
399+
* contrast = bgColor.contrast(fgColor, 'all');
400+
*
401+
* describe('A black canvas with four short lines of grey text that respectively says: "WCAG 2.1", "12.55", "APCA", and "-73.30".');
402+
* }
403+
*
404+
* function draw(){
405+
* background(bgColor);
406+
*
407+
* textSize(14);
408+
*
409+
* fill(fgColor);
410+
* text('WCAG 2.1', 10, 25);
411+
* text(nf(contrast.WCAG21.value, 0, 2), 10, 40);
412+
*
413+
* text('APCA', 10, 70);
414+
* text(nf(contrast.APCA.value, 0, 2), 10, 85);
415+
* }
416+
* </code>
417+
* </div>
418+
*/
419+
contrast(other_color, options='WCAG21') {
420+
if(options !== 'all'){
421+
let contrastVal, minimum;
422+
switch(options){
423+
case 'WCAG21':
424+
contrastVal = contrastWCAG21(this._color, other_color._color);
425+
minimum = 4.5;
426+
break;
427+
case 'APCA':
428+
contrastVal = Math.abs(contrastAPCA(this._color, other_color._color));
429+
minimum = 75;
430+
break;
431+
default:
432+
return null;
433+
}
434+
435+
return contrastVal >= minimum;
436+
}else{
437+
const wcag21Value = contrastWCAG21(this._color, other_color._color);
438+
const apcaValue = contrastAPCA(this._color, other_color._color);
439+
return {
440+
WCAG21: {
441+
value: wcag21Value,
442+
passedMinimum: wcag21Value >= 4.5,
443+
passedAAA: wcag21Value >= 7
444+
},
445+
APCA: {
446+
value: apcaValue,
447+
passedMinimum: Math.abs(apcaValue) >= 75
448+
}
449+
};
450+
}
451+
};
452+
329453
/**
330454
* Sets the red component of a color.
331455
*

src/core/friendly_errors/fes_core.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ function fesCore(p5, fn){
593593

594594
// get the function just above the topmost frame in the friendlyStack.
595595
// i.e the name of the library function called from user's code
596-
const func = stacktrace[friendlyStack[0].frameIndex - 2].functionName
596+
const func = stacktrace[friendlyStack[0].frameIndex - 1].functionName
597597
.split('.')
598598
.slice(-1)[0];
599599

src/data/local_storage.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ function storage(p5, fn){
147147
value = value.toString();
148148
} else if (value instanceof p5.Vector) {
149149
type = 'p5.Vector';
150-
const coord = [value.x, value.y, value.z];
150+
const coord = value.values;
151151
value = coord;
152152
}
153153
value = JSON.stringify(value);

src/dom/p5.MediaElement.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,13 @@ class MediaElement extends Element {
935935

936936
/*** CONNECT TO WEB AUDIO API / p5.sound.js ***/
937937

938+
_getAudioContext() {
939+
return undefined;
940+
}
941+
_getSoundOut() {
942+
return undefined;
943+
}
944+
938945
/**
939946
* Sends the element's audio to an output.
940947
*
@@ -954,9 +961,9 @@ class MediaElement extends Element {
954961
let audioContext, mainOutput;
955962

956963
// if p5.sound exists, same audio context
957-
if (typeof fn.getAudioContext === 'function') {
958-
audioContext = fn.getAudioContext();
959-
mainOutput = p5.soundOut.input;
964+
if (this._getAudioContext() && this._getSoundOut()) {
965+
audioContext = this._getAudioContext();
966+
mainOutput = this._getSoundOut().input;
960967
} else {
961968
try {
962969
audioContext = obj.context;
@@ -1792,6 +1799,19 @@ function media(p5, fn){
17921799
*/
17931800
p5.MediaElement = MediaElement;
17941801

1802+
// Patch MediaElement to give it access to fn, which p5.sound may attach things to
1803+
// if present in a sketch
1804+
MediaElement.prototype._getSoundOut = function() {
1805+
return p5.soundOut;
1806+
}
1807+
MediaElement.prototype._getAudioContext = function() {
1808+
if (typeof fn.getAudioContext === 'function') {
1809+
return fn.getAudioContext();
1810+
} else {
1811+
return undefined;
1812+
}
1813+
}
1814+
17951815
/**
17961816
* Path to the media element's source as a string.
17971817
*

src/math/p5.Vector.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -265,14 +265,14 @@ class Vector {
265265
* function setup() {
266266
* let v = createVector(20, 30);
267267
*
268-
* // Prints 'p5.Vector Object : [20, 30, 0]'.
268+
* // Prints 'vector[20, 30, 0]'.
269269
* print(v.toString());
270270
* }
271271
* </code>
272272
* </div>
273273
*/
274274
toString() {
275-
return `[${this._values.join(', ')}]`;
275+
return `vector[${this._values.join(', ')}]`;
276276
}
277277

278278
/**

src/webgl/p5.RendererGL.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1352,8 +1352,8 @@ class RendererGL extends Renderer {
13521352
fbo,
13531353
0,
13541354
0,
1355-
this.width,
1356-
this.height,
1355+
fbo.width,
1356+
fbo.height,
13571357
-target.width / 2,
13581358
-target.height / 2,
13591359
target.width,

test/manual-test-examples/async/loadJSON_callback/sketch.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,8 @@ function getWeather() {
3636

3737
var cityName = userInput.value();
3838
var URL =
39-
'http://api.openweathermap.org/data/2.5/weather?q=' +
40-
cityName +
41-
'&units=metric';
39+
'https://wttr.in/' + encodeURIComponent(cityName) + '?format=j1';
40+
4241
result = loadJSON(URL, displayWeather); // displayWeather is the callback
4342
}
4443

@@ -47,9 +46,9 @@ function displayWeather() {
4746
print(result); // result is ready!
4847

4948
var location = result.name;
50-
var currentTemp = result.main.temp;
49+
var avgTemp = result.weather[0].avgtempC;
5150
text(
52-
'Current temperature in ' + location + ' is ' + currentTemp + ' celsius',
51+
'Today\'s average temperature in ' + location + ' is ' + avgTemp + ' celsius',
5352
width / 2,
5453
height / 2
5554
);

test/manual-test-examples/async/loadJSON_options/sketch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function setup() {
22
noCanvas();
33
loadJSON(
4-
'http://api.openweathermap.org/data/2.5/station?id=5091',
4+
'https://wttr.in/Berlin?format=j1',
55
parseStuff,
66
'json'
77
);

0 commit comments

Comments
 (0)