php - 字符串php的深色十六进制颜色
问题描述
我想通过 php 中的字符串名称生成深色的十六进制背景颜色?相同字符串的十六进制颜色必须相同,我尝试使用哈希:
$backgrounColor = substr(md5('blabla')), 0, 6);
$fontColor = 'fff';
解决方案
假设当您说“深色”时,您的意思是所有通道都处于特定强度下的颜色,您可以执行以下操作:
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);
}
推荐阅读
- javascript - 如何有效地返回视口中的所有元素 ID?
- google-cloud-platform - Cloud Task 需要“字节”类型的有效负载,如何发送字典?
- reactjs - 如何使用 WebView 显示 React jsx 文件
- php - 使用 codeigniter 在数据库中获取具有相同值的列
- security - 如何保护数据免受 USB 端口嗅探?
- c# - 如何使面板中的某些组框显示为褪色而不是隐藏?
- java - 如何修复 Java 中的“ava.lang.ArrayIndexOutOfBoundsException:Index 1 out of bounds for length 1”错误
- iteration - 为什么 itertools.product 在初始化时会遍历所有元素?
- python - Python 中 Lambda 表达式与局部函数的速度测试
- parsing - 如何在jison中编写自定义函数和变量?