获取十六进制背景色的反向值-PHP
我有一个日历在我的网页与给定的颜色作为事件背景色(深蓝色或栗色)。背景色应用得很好。(如图所示) 现在,我想应用前景|文本颜色,以便阅读。(可以与我的“黑色”=>“白色”或“深棕色”=>“白色”完全相反) 以下是我迄今为止所做的工作获取十六进制背景色的反向值-PHP,php,colors,hex,Php,Colors,Hex,我有一个日历在我的网页与给定的颜色作为事件背景色(深蓝色或栗色)。背景色应用得很好。(如图所示) 现在,我想应用前景|文本颜色,以便阅读。(可以与我的“黑色”=>“白色”或“深棕色”=>“白色”完全相反) 以下是我迄今为止所做的工作 public static function GenerateInverseColor($color){ $color = trim($color); $prependHash = FALSE; i
public static function GenerateInverseColor($color){
$color = trim($color);
$prependHash = FALSE;
if(strpos($color,'#')!==FALSE) {
$prependHash = TRUE;
$color = str_replace('#',NULL,$color);
}
switch($len=strlen($color)) {
case 3:
$color=preg_replace("/(.)(.)(.)/","\\1\\1\\2\\2\\3\\3",$color);
break;
case 6:
break;
default:
trigger_error("Invalid hex length ($len). Must be a minimum length of (3) or maxium of (6) characters", E_USER_ERROR);
}
if(!preg_match('/^[a-f0-9]{6}$/i',$color)) {
$color = htmlentities($color);
trigger_error( "Invalid hex string #$color", E_USER_ERROR );
}
$r = dechex(255-hexdec(substr($color,0,2)));
$r = (strlen($r)>1)?$r:'0'.$r;
$g = dechex(255-hexdec(substr($color,2,2)));
$g = (strlen($g)>1)?$g:'0'.$g;
$b = dechex(255-hexdec(substr($color,4,2)));
$b = (strlen($b)>1)?$b:'0'.$b;
return ($prependHash?'#':NULL).$r.$g.$b;
}
我也试过了
// provide balck|white color for any color
public static function GenerateInverseColor($hexcolor){
$hexcolor = trim($hexcolor);
$r = hexdec(substr($hexcolor,0,2));
$g = hexdec(substr($hexcolor,2,2));
$b = hexdec(substr($hexcolor,4,2));
$yiq = (($r*299)+($g*587)+($b*114))/1000;
return ($yiq >= 128) ? 'black' : 'white';
}
而且
public static function GenerateInverseColor($hexcolor){
if (strlen($color) != 6){ return '000000'; }
$rgb = '';
for ($x=0;$x<3;$x++){
$c = 255 - hexdec(substr($color,(2*$x),2));
$c = ($c < 0) ? 0 : dechex($c);
$rgb .= (strlen($c) < 2) ? '0'.$c : $c;
}
return '#'.$rgb;
}
公共静态函数GenerateInverseColor($hexcolor){
如果(strlen($color)!=6{返回'000000';}
$rgb='';
对于($x=0;$x),我过去曾在PHP中使用该库来处理颜色。它应该有计算文本/背景内容的方法,我记得我在这类任务中使用过它
请注意,纯粹从数学角度处理随机颜色根本不能保证良好的用户界面效果。颜色感知比其组成更复杂。更不用说,最终的颜色会形成对比,但在美学上看起来像垃圾。你确定你的问题不是重复的吗?是的,我有chec同样,它也没有帮助。它提供了与背景几乎相同的颜色(几乎,不精确)。对不起,我无权使用库来完成此任务