-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.ts
76 lines (62 loc) · 2.41 KB
/
benchmark.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import b from "benny";
import { closest } from "color-2-name";
import { colord, extend } from "colord";
import namesPlugin from "colord/plugins/names";
extend([namesPlugin]);
const randomHex = () =>
`#${[1, 2, 3]
.map(() =>
Math.floor(Math.random() * 255)
.toString(16)
.padStart(2, "0"),
)
.join("")}`;
/**
* Generate a random RGB or RGBA color.
* @param {boolean} includeAlpha - Whether to include alpha value (opacity).
* @returns {string} A string representing the generated color.
*/
function getRandomColor(includeAlpha = false) {
// Generate random values for red, green, and blue components
const red = Math.floor(Math.random() * 256);
const green = Math.floor(Math.random() * 256);
const blue = Math.floor(Math.random() * 256);
// If includeAlpha is true, generate a random alpha value between 0 and 1
const alpha = includeAlpha ? Math.random().toFixed(2) : 1;
// If includeAlpha is true, return RGBA format, otherwise return RGB format
return includeAlpha
? `rgba(${red}, ${green}, ${blue}, ${alpha})`
: `rgb(${red}, ${green}, ${blue})`;
}
/**
* Generate a random HSL color.
* @param {boolean} includeAlpha - Whether to include alpha value (opacity).
* @returns {string} A string representing the generated color.
*/
function getRandomHSLColor(includeAlpha = false) {
// Generate random values for hue, saturation, and lightness components
const hue = Math.floor(Math.random() * 360); // Hue ranges from 0 to 360
const saturation = Math.floor(Math.random() * 101); // Saturation ranges from 0% to 100%
const lightness = Math.floor(Math.random() * 101); // Lightness ranges from 0% to 100%
// If includeAlpha is true, generate a random alpha value between 0 and 1
const alpha = includeAlpha ? Math.random().toFixed(2) : 1;
// If includeAlpha is true, return HSLA format, otherwise return HSL format
return includeAlpha
? `hsla(${hue}, ${saturation}%, ${lightness}%, ${alpha})`
: `hsl(${hue}, ${saturation}%, ${lightness}%)`;
}
const randoms = new Array(50).fill(0).map(() => randomHex());
console.log("colors used for tests", randoms);
b.suite(
"Convert a random color to a name",
b.add("color2name", () => {
// biome-ignore lint/complexity/noForEach: <explanation>
randoms.forEach((i) => closest(i));
}),
b.add("colord", () => {
// biome-ignore lint/complexity/noForEach: <explanation>
randoms.forEach((i) => colord(i).toName({ closest: true }));
}),
b.cycle(),
b.complete(),
);