1. 首页
  2. 技术知识

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

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

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

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

functiоn 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>

functiоn 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;
}

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

原创文章,作者:starterknow,如若转载,请注明出处:https://www.starterknow.com/32531.html

联系我们