@@ -352,10 +352,10 @@ public static function parseHexColor(string $hex): array
352
352
$ tmp = substr ($ hex , 1 );
353
353
if (strlen ($ tmp ) % 3 == 0 ) {
354
354
$ mult = strlen ($ tmp ) / 3 ;
355
- $ hex += ' f ' * $ mult ;
355
+ $ hex .= str_repeat ( " f " , $ mult) ;
356
356
}
357
357
// Source: https://stackoverflow.com/a/21966100
358
- $ length = (strlen ($ hex ) - 1 ) / 3 ;
358
+ $ length = (strlen ($ hex ) - 1 ) / 4 ;
359
359
$ fact = [17 , 1 , 0.062272 ][$ length - 1 ];
360
360
return [
361
361
(int )round (hexdec (substr ($ hex , 1 , $ length )) * $ fact ),
@@ -384,28 +384,28 @@ public static function rgbToHex(array $color): string
384
384
$ result = "# " ;
385
385
if (count ($ color ) === 3 ) {
386
386
$ color [] = 255 ;
387
- } elseif (count ($ color ) !== 4 ) {
387
+ } elseif (count ($ color ) !== 4 ) {
388
388
throw Exception ("Invalid RGB number. " );
389
- }
390
- for (int $ i =0 ; $ i <count ($ color ); $ i ++) {
391
- $ result += static ::componentToHex ($ color [$ i ]);
392
389
}
390
+ for ($ i =0 ; $ i <count ($ color ); $ i ++) {
391
+ $ result .= static ::componentToHex ($ color [$ i ]);
392
+ }
393
393
return $ result ;
394
394
}
395
395
396
396
/**
397
397
* @param array{int, int, int}|array{int, int, int, int} $rgba
398
- * @param array{int, int, int} $bg
399
398
*
400
399
* @return array{int, int, int}
401
400
*/
402
- public static function blendAlphaBackground (array $ rgba , array $ bg ): array
401
+ public static function blendAlphaBackground (array $ rgba , string $ bg ): array
403
402
{
404
403
if (count ($ rgba ) === 3 ) {
405
404
return $ rgba ;
406
405
} elseif (count ($ rgba ) === 4 ) {
407
- $ result = [];
408
- for (int $ i =0 ; $ i <3 ; $ i ++) {
406
+ $ result = [];
407
+ $ bg = static ::parseHexColor ($ bg );
408
+ for ($ i =0 ; $ i <3 ; $ i ++) {
409
409
$ result [] = $ rgba [$ i ] * $ rgba [3 ] + $ bg [$ i ] * (1 - $ rgba [3 ]);
410
410
}
411
411
return $ result ;
0 commit comments