如何在php中找到更轻或更暗版本的十六进制代码的十六进制代码
I'm trying to achieve what this guy here is doing, only in PHP or jQuery. Basically I have a hex color code, say #FF0000, which is red. How would I find darker or lighter hex color codes of this color.
我试图通过PHP或jQuery来实现这个人在做什么。基本上我有一个十六进制颜色代码,比如#FF0000,它是红色的。如何找到这种颜色的深色或浅色十六进制颜色代码。
To clearify: I want to take a hex color code (#FF0000), and find the correct hex color code of lighter or darker shades of that color code.
要清除:我想采用十六进制颜色代码(#FF0000),并找到该颜色代码更浅或更深色调的正确十六进制颜色代码。
Either done in PHP, or jQuery, something that I can change the color via PHP, as the server processes the page.
可以用PHP或jQuery完成,我可以通过PHP改变颜色,因为服务器处理页面。
I prefer not to use third party jQuery plugins to achieve this, but I will if its super duper complicated.
我不想使用第三方jQuery插件来实现这一点,但是如果它超级复杂的话,我会这样做。
3 个解决方案
#1
6
When you say "a lighter version" (or "a darker version") there are a very large number of possibilities. For instance, you could take #ff0000
and have 253 "darker versions" ranging from #010000
to #fe0000
. Similarly, you can have 253 "lighter versions" ranging from #ff0101
to #fffefe
. So your question is not very well defined.
当你说“更轻的版本”(或“更黑的版本”)时,有很多可能性。例如,您可以使用#ff0000并拥有253个“较暗版本”,范围从#010000到#fe0000。同样,你可以拥有253个“更轻的版本”,范围从#ff0101到#fffefe。所以你的问题定义不明确。
I will assume in this answer that by "lighter version", you mean the result of overying a 50% transparent white on the colour, and by "darker" the same but black.
我将在这个答案中假设,“较轻的版本”,你的意思是在颜色上涂上50%透明白色的结果,而在颜色上涂上“深色”的结果,但是黑色。
In any case, you should always start by extracting the numbers from the hex code:
无论如何,您应该始终从十六进制代码中提取数字:
// assuming input of form "#RRGGBB"
$col = Array(
hexdec(substr($input,1,2)),
hexdec(substr($input,3,2)),
hexdec(substr($input,5,2))
);
Now that you have that, you can easily apply the "overlay":
现在您已经拥有了它,您可以轻松应用“叠加层”:
$darker = Array(
$col[0]/2,
$col[1]/2,
$col[2]/2
);
$lighter = Array(
255-(255-$col[0])/2,
255-(255-$col[1])/2,
255-(255-$col[2])/2
);
Then it's a simple matter to convert them back into hex codes:
然后将它们转换回十六进制代码是一件简单的事情:
$darker = "#".sprintf("%02X%02X%02X", $darker[0], $darker[1], $darker[2]);
$lighter = "#".sprintf("%02X%02X%02X", $lighter[0], $lighter[1], $lighter[2]);
Done!
完成!
更多相关文章
- 在php中检测浏览器,版本和平台的可靠方法
- centos7 升级php版本
- 在rhel5版本下安装mysql+apache+php实战攻略
- mac下安装多版本PHP及切换
- 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持。
- mysql数据版本控制系统的最佳实践
- 30分钟安装linux版本mysql5.7.21版本,没坑,高效,必会
- MySQL5.7以上版本root用户空密码修改(windows系统、zip版MySQL)
- mysql的zip版本安装填坑