如何使用PHP生成较浅/较深的颜色?
我有某种颜色的十六进制值,例如如何使用PHP生成较浅/较深的颜色?,php,html,css,colors,Php,Html,Css,Colors,我有某种颜色的十六进制值,例如#202010 如何在PHP中生成以百分比(即暗20%)表示的较浅或较深的新颜色?以下是一个示例: <?php $color = '#aabbcc'; // The color we'll use 现在我们在$parts[1]中有红色,在$parts[2]中有绿色,在$parts[3]中有蓝色。现在,让我们将它们从十六进制转换为整数: $out = ""; // Prepare to fill with the results for($i = 1; $i
#202010
如何在PHP中生成以百分比(即暗20%)表示的较浅或较深的新颜色?以下是一个示例:
<?php
$color = '#aabbcc'; // The color we'll use
现在我们在$parts[1]
中有红色,在$parts[2]
中有绿色,在$parts[3]
中有蓝色。现在,让我们将它们从十六进制转换为整数:
$out = ""; // Prepare to fill with the results
for($i = 1; $i <= 3; $i++) {
$parts[$i] = hexdec($parts[$i]);
现在,我们将把它们转换回十六进制,并将它们添加到输出字符串中
$out .= str_pad(dechex($parts[$i]), 2, '0', STR_PAD_LEFT);
}
然后在字符串的开头加一个“#”,就这样了 如Frxstrem给出的示例所示,按百分比调整颜色并不理想 如果您的颜色是黑色(RGB中为0,0,0),您将乘以0,这将不会产生任何变化。如果您的颜色是深灰色(例如RGB中的2,2,2),则必须将亮度提高50%才能升到(3,3,3)。另一方面,如果您的RGB颜色为(100100100),50%的调整将使您上升到(150150),相比之下,这是一个更大的变化 更好的解决方案是按步长/数字(0-255)而不是按百分比进行调整,例如(PHP代码): 编辑2014-01-06:稍微清理了代码
功能调整亮度($hex,$steps){
//步长应介于-255和255之间。负=暗,正=亮
$steps=max(-255,min(255,$steps));
//标准化为六个字符长的十六进制字符串
$hex=str#u替换('#',''$hex);
如果(strlen($hex)==3){
$hex=str_repeat(substr($hex,0,1),2)。str_repeat(substr($hex,1,1),2)。str_repeat(substr($hex,2,1),2);
}
//分为三部分:R、G和B
$color\u parts=str\u split($hex,2);
$return='#';
foreach($color\u零件为$color){
$color=hexdec($color);//转换为十进制
$color=max(0,min(255,$color+$steps));//调整颜色
$return.=str_pad(dechex($color),2,'0',str_pad_LEFT);//生成两个字符的十六进制代码
}
return$return;
}
答案是错误的
使用RGB模型是一个概念错误
您需要将颜色从RGB(或十六进制形式)转换为HSL
那就是色调,饱和度,亮度
一旦你将它从RGB转换成HSL,要使颜色变亮,你只需将L值(亮度)调整10%。然后,一旦你完成了,你将从HSL转换回RGB,你就完成了
瞧
我对此很感兴趣,但我的问题是如何在颜色中添加不透明度 我想要一种褪色的颜色,而不是更亮的颜色。我发现: 而且它工作得很好——从原始站点发布的代码适合SO读者
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'] );
}
function color\u blend\u by\u opacity($前台,$opacity,$background=null)
{
静态$colors_rgb=array();//存储已通过以下hexdec()函数传递的颜色值。
$foreground=str#u replace('#','$foreground);
如果(为空($background))
$background='FFFFFF';//默认背景。
$pattern='~^[a-f0-9]{6,6}$~i';//只接受有效的十六进制颜色值。
如果(!@preg_匹配($pattern,$foreground)或!@preg_匹配($pattern,$background))
{
触发错误(“发现无效十六进制颜色值”,E用户警告);
返回false;
}
$opacity=intval($opacity);//验证不透明度数据/数字。
如果($opacity>100 | |$opacityhexdec($foreground[0]。$foreground[1]),
'g'=>hexdec($前台[2]。$前台[3]),
'b'=>hexdec($foreground[4]。$foreground[5]);
$colors_rgb[$foreground]=$f;
}
其他的
{//如果此函数在脚本中使用100次,则此块将运行99次。高效。
$f=$colors_rgb[$foreground];
}
如果(!isset($colors\u rgb[$background]))
{//对于每个脚本,对于每个独特的颜色,只执行一次。
$b=数组('r'=>hexdec($background[0]。$background[1]),
'g'=>hexdec($background[2]。$background[3]),
'b'=>hexdec($background[4]。$background[5]);
$colors_rgb[$background]=$b;
}
其他的
{//如果此函数在脚本中使用100次,则此块将运行99次。高效。
$b=$colors_rgb[$background];
}
$add=数组('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);
返回sprintf(“%02X%02X%02X',$f['r'],$f['g'],$f['b']);
}
允许转换为HSL,更改亮度组件并转换回RGB。如果您想要一个简单的实现,并且您不太关心亮度值特别高于50%(或任何阈值),您可以使用我的解决方案来选择更亮的颜色:
$color = sprintf('#%06X', mt_rand(0xFFFFFF / 1.5, 0xFFFFFF));
其想法是在调色板的较高部分生成随机颜色。通过更改“1.5”值,可以将结果调整为或多或少暗:
- 较大的颜色会将调色板扩展为较深的颜色
- 较小的颜色会使其颜色更浅
$color = sprintf('#%06X', mt_rand(0x000000, 0xFFFFFF / 1.5));
我知道这并不精确,但对我来说很有效。Torkil Johnsen的答案是基于固定步长的,该步长不仅控制亮度,还轻微改变色调。正如托基尔·约翰森指出的那样,Frxstrem的方法也有缺陷 我采用了一种新的方法,并改进了代码。它适用于任何情况
/**
*按当前亮度的百分比增加或减少颜色的亮度。
*
*@param string$hexCode支持的格式:`FFF`、`ffffffff`、`FFF`、`ffffffff``
*@param float$adjustPercent一个介于-1之间的数字
$color = sprintf('#%06X', mt_rand(0xFFFFFF / 1.5, 0xFFFFFF));
$color = sprintf('#%06X', mt_rand(0x000000, 0xFFFFFF / 1.5));