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!

完成!

更多相关文章

  1. 在php中检测浏览器,版本和平台的可靠方法
  2. centos7 升级php版本
  3. 在rhel5版本下安装mysql+apache+php实战攻略
  4. mac下安装多版本PHP及切换
  5. 在WAMPSERVER下增加多版本的PHP(PHP5.3,PHP5.4,PHP5.5)支持。
  6. mysql数据版本控制系统的最佳实践
  7. 30分钟安装linux版本mysql5.7.21版本,没坑,高效,必会
  8. MySQL5.7以上版本root用户空密码修改(windows系统、zip版MySQL)
  9. mysql的zip版本安装填坑

随机推荐

  1. asp.net core封装layui组件的示例详解
  2. UWP中设置控件样式四种方法
  3. C#中关于DBNULL的解释
  4. ASP.NET Core中用户登录验证实现最低配置
  5. 有关ASP.NET中Config文件的读写讲解
  6. C#中foreach与yield的实例详解
  7. asp.net实现生成缩略图及加水印的方法示
  8. asp.net mvc中实现Forms身份验证身份验证
  9. C#中强制转换与尝试转换的实现方法
  10. C#中实现复制与删除文件的方法