如何用PHP编写16进制颜色值转RGB函数?

更新于
2026-09-27 13:16:07
1阅读来源:SEO资讯
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计139个文字,预计阅读时间需要1分钟。

如何用PHP编写16进制颜色值转RGB函数?

php/** * 将16进制颜色值转换为RGB * * @param string $hex 16进制颜色值 * @return array RGB颜色值数组 */function hex2rgb($hex) { $hex=str_replace('#', '', $hex); if (strlen($hex)==3) { $r=hexdec(str_repeat(substr($hex, 0, 1), 2)); $g=hexdec(str_repeat(substr($hex, 1, 1), 2)); $b=hexdec(str_repeat(substr($hex, 2, 1), 2)); } else { $r=hexdec(substr($hex, 0, 2)); $g=hexdec(substr($hex, 2, 2)); $b=hexdec(substr($hex, 4, 2)); } return array($r, $g, $b);}

//16进制转RGB (PHP代码函数) //代码来源:Monxin ./config/functions.php function hex2rgb($hex){ $hex=str_replace('#','',$hex); $length=strlen($hex); $rgb=''; if($length==3){ $rgb.=hexdec($hex[0].$hex[0]).','; $rgb.=hexdec($hex[1].$hex[1]).','; $rgb.=hexdec($hex[2].$hex[2]); } if($length==6){ $rgb.=hexdec($hex[0].$hex[1]).','; $rgb.=hexdec($hex[2].$hex[3]).','; $rgb.=hexdec($hex[4].$hex[5]); } return $rgb; }

如何用PHP编写16进制颜色值转RGB函数?

本文共计139个文字,预计阅读时间需要1分钟。

如何用PHP编写16进制颜色值转RGB函数?

php/** * 将16进制颜色值转换为RGB * * @param string $hex 16进制颜色值 * @return array RGB颜色值数组 */function hex2rgb($hex) { $hex=str_replace('#', '', $hex); if (strlen($hex)==3) { $r=hexdec(str_repeat(substr($hex, 0, 1), 2)); $g=hexdec(str_repeat(substr($hex, 1, 1), 2)); $b=hexdec(str_repeat(substr($hex, 2, 1), 2)); } else { $r=hexdec(substr($hex, 0, 2)); $g=hexdec(substr($hex, 2, 2)); $b=hexdec(substr($hex, 4, 2)); } return array($r, $g, $b);}

//16进制转RGB (PHP代码函数) //代码来源:Monxin ./config/functions.php function hex2rgb($hex){ $hex=str_replace('#','',$hex); $length=strlen($hex); $rgb=''; if($length==3){ $rgb.=hexdec($hex[0].$hex[0]).','; $rgb.=hexdec($hex[1].$hex[1]).','; $rgb.=hexdec($hex[2].$hex[2]); } if($length==6){ $rgb.=hexdec($hex[0].$hex[1]).','; $rgb.=hexdec($hex[2].$hex[3]).','; $rgb.=hexdec($hex[4].$hex[5]); } return $rgb; }

如何用PHP编写16进制颜色值转RGB函数?