Skip to content

Commit 3afdbbb

Browse files
committed
build: small refactoring
1 parent ad0d00a commit 3afdbbb

2 files changed

Lines changed: 28 additions & 12 deletions

File tree

engine/src/Utils/HexColorManager.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,26 @@
11
import type { IColor, IRangeColor, IRgb, IRgba } from "../Core/Interfaces/Colors.js";
22
import type { IColorManager } from "../Core/Interfaces/IColorManager.js";
33

4+
/**
5+
* Indexes for accessing color components from regex capture groups.
6+
* Uses 1-based indexing as index 0 contains the full match.
7+
*/
48
enum RgbIndexes {
59
r = 1,
610
g = 2,
711
b = 3,
812
a = 4,
913
}
1014

15+
const shorthandHexRegex = /^#?([a-f\d])([a-f\d])([a-f\d])([a-f\d])?$/i,
16+
hexRegex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i,
17+
hexRadix = 16,
18+
defaultAlpha = 1,
19+
alphaFactor = 0xff;
20+
1121
/**
22+
* Manages hexadecimal color string parsing and conversion to RGB/RGBA format.
23+
* Implements the IColorManager interface for handling hex color values.
1224
*/
1325
export class HexColorManager implements IColorManager {
1426
readonly key;
@@ -40,25 +52,20 @@ export class HexColorManager implements IColorManager {
4052
return;
4153
}
4254

43-
const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])([a-f\d])?$/i,
44-
hexFixed = hexColor.replace(shorthandRegex, (_, r: string, g: string, b: string, a: string) => {
55+
const hexFixed = hexColor.replace(shorthandHexRegex, (_, r: string, g: string, b: string, a: string) => {
4556
return r + r + g + g + b + b + (a !== undefined ? a + a : "");
4657
}),
47-
regex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i,
48-
result = regex.exec(hexFixed),
49-
radix = 16,
50-
defaultAlpha = 1,
51-
alphaFactor = 0xff;
58+
result = hexRegex.exec(hexFixed);
5259

5360
return result
5461
? {
5562
a:
5663
result[RgbIndexes.a] !== undefined
57-
? parseInt(result[RgbIndexes.a], radix) / alphaFactor
64+
? parseInt(result[RgbIndexes.a], hexRadix) / alphaFactor
5865
: defaultAlpha,
59-
b: parseInt(result[RgbIndexes.b], radix),
60-
g: parseInt(result[RgbIndexes.g], radix),
61-
r: parseInt(result[RgbIndexes.r], radix),
66+
b: parseInt(result[RgbIndexes.b], hexRadix),
67+
g: parseInt(result[RgbIndexes.g], hexRadix),
68+
r: parseInt(result[RgbIndexes.r], hexRadix),
6269
}
6370
: undefined;
6471
}

plugins/colors/namedColor/src/NamedColorManager.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
import { type IColor, type IColorManager, type IRangeColor, type IRgb, type IRgba } from "@tsparticles/engine";
1+
import {
2+
type IColor,
3+
type IColorManager,
4+
type IRangeColor,
5+
type IRgb,
6+
type IRgba,
7+
getLogger,
8+
} from "@tsparticles/engine";
29

310
const namedColors = new Map<string, IRgb>([
411
["aliceblue", { r: 240, g: 248, b: 255 }],
@@ -180,6 +187,8 @@ export class NamedColorManager implements IColorManager {
180187
const rgbColor = namedColors.get(input.toLowerCase());
181188

182189
if (!rgbColor) {
190+
getLogger().error("Color not found", input);
191+
183192
return undefined;
184193
}
185194

0 commit comments

Comments
 (0)