@@ -176,3 +176,63 @@ export function blendWithBackground(
176176 // Combine back to a single color value
177177 return ( resultR << 16 ) | ( resultG << 8 ) | resultB ;
178178}
179+
180+ /**
181+ * Parse CSS color string to PixiJS numeric color (opaque).
182+ * If the color has alpha < 1, it will be pre-blended with the background
183+ * to produce an opaque result for better GPU performance.
184+ *
185+ * Supported formats:
186+ * - #RGB (3 hex digits)
187+ * - #RGBA (4 hex digits)
188+ * - #RRGGBB (6 hex digits)
189+ * - #RRGGBBAA (8 hex digits)
190+ * - rgb(r, g, b)
191+ * - rgba(r, g, b, a)
192+ *
193+ * @param cssColor - CSS color string
194+ * @returns Opaque PixiJS numeric color (0xRRGGBB)
195+ */
196+ export function cssColorToPixi ( cssColor : string ) : number {
197+ let color = 0x000000 ;
198+ let alpha = 1 ;
199+
200+ if ( cssColor . startsWith ( '#' ) ) {
201+ const hex = cssColor . slice ( 1 ) ;
202+ if ( hex . length === 8 ) {
203+ const rgb = hex . slice ( 0 , 6 ) ;
204+ alpha = parseInt ( hex . slice ( 6 , 8 ) , 16 ) / 255 ;
205+ color = parseInt ( rgb , 16 ) ;
206+ } else if ( hex . length === 6 ) {
207+ color = parseInt ( hex , 16 ) ;
208+ } else if ( hex . length === 4 ) {
209+ const r = hex [ 0 ] ! ;
210+ const g = hex [ 1 ] ! ;
211+ const b = hex [ 2 ] ! ;
212+ const a = hex [ 3 ] ! ;
213+ color = parseInt ( r + r + g + g + b + b , 16 ) ;
214+ alpha = parseInt ( a + a , 16 ) / 255 ;
215+ } else if ( hex . length === 3 ) {
216+ const r = hex [ 0 ] ! ;
217+ const g = hex [ 1 ] ! ;
218+ const b = hex [ 2 ] ! ;
219+ color = parseInt ( r + r + g + g + b + b , 16 ) ;
220+ }
221+ } else {
222+ const rgbMatch = cssColor . match ( / r g b a ? \( ( \d + ) , \s * ( \d + ) , \s * ( \d + ) (?: , \s * ( \d * (?: \. \d + ) ? ) ) ? \) / ) ;
223+ if ( rgbMatch ) {
224+ const r = parseInt ( rgbMatch [ 1 ] ?? '0' , 10 ) ;
225+ const g = parseInt ( rgbMatch [ 2 ] ?? '0' , 10 ) ;
226+ const b = parseInt ( rgbMatch [ 3 ] ?? '0' , 10 ) ;
227+ alpha = rgbMatch [ 4 ] ? parseFloat ( rgbMatch [ 4 ] ) : 1 ;
228+ color = ( r << 16 ) | ( g << 8 ) | b ;
229+ }
230+ }
231+
232+ // Pre-blend with background if color has alpha < 1
233+ if ( alpha < 1 ) {
234+ return blendWithBackground ( color , alpha ) ;
235+ }
236+
237+ return color ;
238+ }
0 commit comments