10
10
*/
11
11
namespace gdjs {
12
12
const logger = new gdjs . Logger ( 'Engine runtime' ) ;
13
+ const hexStringRegex = / ^ ( # { 0 , 1 } [ A - F a - f 0 - 9 ] { 6 } ) $ / ;
14
+ const shorthandHexStringRegex = / ^ ( # { 0 , 1 } [ A - F a - f 0 - 9 ] { 3 } ) $ / ;
15
+ const rgbStringRegex = / ^ ( \d { 1 , 3 } ; \d { 1 , 3 } ; \d { 1 , 3 } ) / ;
13
16
14
17
/**
15
18
* Contains functions used by events (this is a convention only, functions can actually
@@ -61,7 +64,7 @@ namespace gdjs {
61
64
} ;
62
65
63
66
/**
64
- * Convert a Hex string to an RGB color array [r, g, b], where each component is in the range [0, 255].
67
+ * Convert a Hex string (#124FE4) to an RGB color array [r, g, b], where each component is in the range [0, 255].
65
68
*
66
69
* @param {string } hex Color hexadecimal
67
70
*/
@@ -74,24 +77,53 @@ namespace gdjs {
74
77
: [ 0 , 0 , 0 ] ;
75
78
} ;
76
79
80
+ /**
81
+ * Convert a shorthand Hex string (#1F4) to an RGB color array [r, g, b], where each component is in the range [0, 255].
82
+ *
83
+ * @param {string } hex Color hexadecimal
84
+ */
85
+ export const shorthandHexToRGBColor = function (
86
+ hexString : string
87
+ ) : [ number , number , number ] {
88
+ const hexNumber = parseInt ( hexString . replace ( '#' , '' ) , 16 ) ;
89
+ return Number . isFinite ( hexNumber )
90
+ ? [
91
+ 17 * ( ( hexNumber >> 8 ) & 0xf ) ,
92
+ 17 * ( ( hexNumber >> 4 ) & 0xf ) ,
93
+ 17 * ( hexNumber & 0xf ) ,
94
+ ]
95
+ : [ 0 , 0 , 0 ] ;
96
+ } ;
97
+
77
98
/**
78
99
* Convert a RGB string ("rrr;ggg;bbb") or a Hex string ("#rrggbb") to a RGB color array ([r,g,b] with each component going from 0 to 255).
79
100
* @param value The color as a RGB string or Hex string
80
101
*/
81
102
export const rgbOrHexToRGBColor = function (
82
103
value : string
83
104
) : [ number , number , number ] {
84
- const splitValue = value . split ( ';' ) ;
85
- // If a RGB string is provided, return the RGB object.
86
- if ( splitValue . length === 3 ) {
87
- return [
88
- parseInt ( splitValue [ 0 ] , 10 ) ,
89
- parseInt ( splitValue [ 1 ] , 10 ) ,
90
- parseInt ( splitValue [ 2 ] , 10 ) ,
91
- ] ;
105
+ const rgbColor = extractRGBString ( value ) ;
106
+ if ( rgbColor ) {
107
+ const splitValue = rgbColor . split ( ';' ) ;
108
+ // If a RGB string is provided, return the RGB object.
109
+ if ( splitValue . length === 3 ) {
110
+ return [
111
+ Math . min ( 255 , parseInt ( splitValue [ 0 ] , 10 ) ) ,
112
+ Math . min ( 255 , parseInt ( splitValue [ 1 ] , 10 ) ) ,
113
+ Math . min ( 255 , parseInt ( splitValue [ 2 ] , 10 ) ) ,
114
+ ] ;
115
+ }
116
+ }
117
+
118
+ const hexColor = extractHexString ( value ) ;
119
+ if ( hexColor ) {
120
+ return hexToRGBColor ( hexColor ) ;
121
+ }
122
+ const shorthandHexColor = extractShorthandHexString ( value ) ;
123
+ if ( shorthandHexColor ) {
124
+ return shorthandHexToRGBColor ( shorthandHexColor ) ;
92
125
}
93
- // Otherwise, convert the Hex to RGB.
94
- return hexToRGBColor ( value ) ;
126
+ return [ 0 , 0 , 0 ] ;
95
127
} ;
96
128
97
129
/**
@@ -146,6 +178,23 @@ namespace gdjs {
146
178
] ;
147
179
} ;
148
180
181
+ export const extractHexString = ( str : string ) : string | null => {
182
+ const matches = str . match ( hexStringRegex ) ;
183
+ if ( ! matches ) return null ;
184
+ return matches [ 0 ] ;
185
+ } ;
186
+ export const extractShorthandHexString = ( str : string ) : string | null => {
187
+ const matches = str . match ( shorthandHexStringRegex ) ;
188
+ if ( ! matches ) return null ;
189
+ return matches [ 0 ] ;
190
+ } ;
191
+
192
+ export const extractRGBString = ( str : string ) : string | null => {
193
+ const matches = str . match ( rgbStringRegex ) ;
194
+ if ( ! matches ) return null ;
195
+ return matches [ 0 ] ;
196
+ } ;
197
+
149
198
/**
150
199
* Get a random integer between 0 and max.
151
200
* @param max The maximum value (inclusive).
0 commit comments