用php语句怎样跳色,如何用PHP生成更浅/更深的颜色?

function color_blend_by_opacity( $foreground, $opacity, $background=null )

{

static $colors_rgb=array(); // stores colour values already passed through the hexdec() functions below.

$foreground = str_replace('#','',$foreground);

if( is_null($background) )

$background = 'FFFFFF'; // default background.

$pattern = '~^[a-f0-9]{6,6}$~i'; // accept only valid hexadecimal colour values.

if( !@preg_match($pattern, $foreground) or !@preg_match($pattern, $background) )

{

trigger_error( "Invalid hexadecimal colour value(s) found", E_USER_WARNING );

return false;

}

$opacity = intval( $opacity ); // validate opacity data/number.

if( $opacity>100 || $opacity<0 )

{

trigger_error( "Opacity percentage error, valid numbers are between 0 - 100", E_USER_WARNING );

return false;

}

if( $opacity==100 ) // $transparency == 0

return strtoupper( $foreground );

if( $opacity==0 ) // $transparency == 100

return strtoupper( $background );

// calculate $transparency value.

$transparency = 100-$opacity;

if( !isset($colors_rgb[$foreground]) )

{ // do this only ONCE per script, for each unique colour.

$f = array( 'r'=>hexdec($foreground[0].$foreground[1]),

'g'=>hexdec($foreground[2].$foreground[3]),

'b'=>hexdec($foreground[4].$foreground[5]) );

$colors_rgb[$foreground] = $f;

}

else

{ // if this function is used 100 times in a script, this block is run 99 times. Efficient.

$f = $colors_rgb[$foreground];

}

if( !isset($colors_rgb[$background]) )

{ // do this only ONCE per script, for each unique colour.

$b = array( 'r'=>hexdec($background[0].$background[1]),

'g'=>hexdec($background[2].$background[3]),

'b'=>hexdec($background[4].$background[5]) );

$colors_rgb[$background] = $b;

}

else

{ // if this FUNCTION is used 100 times in a SCRIPT, this block will run 99 times. Efficient.

$b = $colors_rgb[$background];

}

$add = array( 'r'=>( $b['r']-$f['r'] ) / 100,

'g'=>( $b['g']-$f['g'] ) / 100,

'b'=>( $b['b']-$f['b'] ) / 100 );

$f['r'] += intval( $add['r'] * $transparency );

$f['g'] += intval( $add['g'] * $transparency );

$f['b'] += intval( $add['b'] * $transparency );

return sprintf( '%02X%02X%02X', $f['r'], $f['g'], $f['b'] );

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值