Skip to content

Commit 3ef8127

Browse files
perf: replace regex with string indices in toString()
1 parent b87f59b commit 3ef8127

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

src/color/p5.Color.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,15 +384,14 @@ class Color {
384384

385385
if (format === '#rrggbb') {
386386
colorString = String(colorString);
387-
388-
if (colorString.length <= 5) {
389-
const match = colorString.match(/^#?([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/);
390-
if (match) {
391-
colorString = `#${match[1]}${match[1]}${match[2]}${match[2]}${match[3]}${match[3]}`;
392-
}
387+
if (colorString.length === 4) {
388+
const r = colorString[1];
389+
const g = colorString[2];
390+
const b = colorString[3];
391+
colorString = `#${r}${r}${g}${g}${b}${b}`;
393392
}
394393
if (colorString.length > 7) {
395-
colorString = colorString.substring(0, 7);
394+
colorString = colorString.slice(0, 7);
396395
}
397396
colorString = colorString.toLowerCase();
398397
}
@@ -404,7 +403,6 @@ class Color {
404403
}
405404
return colorString;
406405
}
407-
408406
/**
409407
* Checks the contrast between two colors. This method returns a boolean
410408
* value to indicate if the two color has enough contrast. `true` means that

0 commit comments

Comments
 (0)