百度 | 神马 | 搜狗 | 技术文档 | 学习资料分享 - 记录帝国CMS及运维技术的点点滴滴
你的位置:首页 > 实用IT技术 » 正文

【Wordpress相关】使用 PHP 把 16 进制的颜色代码转换成 RGB 数组

发布时间:2020-09-17

作品分类:Wordpress相关  数组  格式  转成  数组  转换成  颜色代码  PHP  RGB

使用 PHP 把 16 进制的颜色代码转换成 RGB 数组,

在 PHP 中,如果获取的颜色代码是 16 进制的格式,怎么转成 RGB 数组格式呢?

function wpjam_hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }

   return array($r, $g, $b);
}

同样也可以将 RBG 数组格式转成 16 进制格式。

<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>

function wpjam_rgb2hex($rgb) {
   $hex = "#";
   $hex .= str_pad(dechex($rgb[0]), 2, "0", STR_PAD_LEFT);
   $hex .= str_pad(dechex($rgb[1]), 2, "0", STR_PAD_LEFT);
   $hex .= str_pad(dechex($rgb[2]), 2, "0", STR_PAD_LEFT);

   return $hex;
}

本站推荐使用的主机:,国外主机建议使用

Top