Skip to content

Commit 361cace

Browse files
committed
added color from hex method
1 parent ddfd0b7 commit 361cace

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

js/engine.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,10 +531,25 @@ class Color {
531531
* @param {number} b Blue value
532532
* @static
533533
*/
534-
fromRGB(r, g, b) {
534+
static fromRGB(r, g, b) {
535535
return new Color(r, g, b, 1, true);
536536
}
537537

538+
static fromHEX(hex) {
539+
// regex to extract r, g, b, a values from hex string
540+
const regex = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})?$/i;
541+
// extract values from hex string
542+
const [, r, g, b, a] = regex.exec(hex);
543+
544+
// convert values to decimal
545+
const dr = parseInt(r, 16);
546+
const dg = parseInt(g, 16);
547+
const db = parseInt(b, 16);
548+
const da = a ? parseInt(a, 16) : 1;
549+
550+
// return color
551+
return new Color(dr, dg, db, da, true);
552+
}
538553
/**
539554
* Converts a color from RGB to HSL
540555
* @private

0 commit comments

Comments
 (0)