我想通过php中的字符串名称生成深色的十六进制背景颜色吗? 对于相同的字符串,十六进制颜色必须相同,我尝试使用哈希值:
$backgrounColor = substr(md5('blabla')), 0, 6);
$fontColor = 'fff';
答案 0 :(得分:0)
假设您说“深色”是指一种颜色,其中所有通道都处于特定强度下,您可以执行以下操作:
function hashColor($srcString) {
$maxItensity = 0x80;
$hash = crc32($srcString);
$red = ($hash & 0xFF0000) >> 16;
$green = ($hash & 0x00FF00) >> 8;
$blue = ($hash & 0x0000FF);
$red -= ($red > $maxItensity) ? 0xFF - $maxItensity : 0;
$green -= ($green > $maxItensity) ? 0xFF - $maxItensity : 0;
$blue -= ($blue > $maxItensity) ? 0xFF - $maxItensity : 0;
$color = ($red << 16) + ($green << 8) + ($blue);
return "#" . str_pad(dechex($color), 6, "0", STR_PAD_LEFT);
}