我知道自PHP 7.4.0起不推荐使用此方法,它会生成警告,但我不知道如何更改它以表明它是正确的。
public function hex_lighter( $color, $factor = 30 ) {
$color = str_replace( '#', '', $color );
$base['R'] = hexdec( $color{0} . $color{1} );
$base['G'] = hexdec( $color{2} . $color{3} );
$base['B'] = hexdec( $color{4} . $color{5} );
$color = '#';
foreach ( $base as $k => $v ) {
$amount = 255 - $v;
$amount = $amount / 100;
$amount = round( $amount * $factor );
$new_decimal = $v + $amount;
$new_hex_component = dechex( $new_decimal );
if ( strlen( $new_hex_component ) < 2 ) {
$new_hex_component = "0" . $new_hex_component;
}
$color .= $new_hex_component;
}
只需将$color{0}
替换为$color[0]
,这将起作用。为每个颜色变量执行此操作