imgcolorallocate包装器,允许十六进制值而不是RGB

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了imgcolorallocate包装器,允许十六进制值而不是RGB相关的知识,希望对你有一定的参考价值。

This function will allow you to use '#ABCDEF' or '0xEEFF33' instead of the RGB values required for imagecolorallocate() by stripping away any non hex characters. It will also check and make sure that it's a valid 6 digit hex value after the stripping of non-hex characters takes place.

it simply uses the function hexdec() to convert each R, G, and B hex values to decimal, and then passes them to imagecolorallocate() individually.

See the page: http://us3.php.net/imagecolorallocate
to learn how to use this wrapper function.
  1. function img_colorallocate($img, $color) {
  2. // strip away any non hex characters, to allow flexibility with the function
  3. $color = ereg_replace('[^A-Fa-f0-9]|(0x)|(0X)', '', $color);
  4. if ( strlen($color) != 6 || !preg_match('/[0-9a-fA-F]{6}/', $color) ) {
  5. die("The value provided does not contain a valid 6 digit hex value.");
  6. }
  7. $img,
  8. hexdec($color[0] . $color[1]),
  9. hexdec($color[2] . $color[3]),
  10. hexdec($color[4] . $color[5])
  11. );
  12. return $color;
  13. }

以上是关于imgcolorallocate包装器,允许十六进制值而不是RGB的主要内容,如果未能解决你的问题,请参考以下文章

十六进制颜色到等效的 RGBa,允许透明度

Mac 有啥好的十六进制编辑器/查看器? [关闭]

像十六进制查看器一样使用 ListView

计算十六进制偏移量

如何在 Autodesk forge 查看器 setTheming 颜色方法中使用十六进制颜色代码?

如何使用十六进制字符限制输入掩码范围