【问题标题】:How to display rgb value of an image as percentage [duplicate]如何将图像的rgb值显示为百分比[重复]
【发布时间】:2017-01-16 04:19:07
【问题描述】:

我想将图像的 RGB 值显示为百分比。

例如,如果十六进制颜色为#000000,我如何将其显示为 % 代码(百分比),如 rgb(0%,0%,0%)?

【问题讨论】:

标签: php image colors rgb


【解决方案1】:

试试这个,

function 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));
   }
   $rgb = array($r, $g, $b);
   //return implode(",", $rgb); // returns the rgb values separated by commas
   return $rgb; // returns an array with the rgb values
}

输出:

Array ( [0] => 204 [1] => 204 [2] => 0 )
//rgb(204,204,0)

DEMO

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 2017-12-14
    • 2016-05-01
    • 2017-12-07
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多