十六进制颜色到rgb颜色不准确


Hex color to rgb color not accurate?

我使用php将十六进制颜色值转换为rgb颜色值。然而,当我看到这两种颜色时,它们看起来不一样?

我在文本到图像脚本中使用rgb值作为背景色。使用php。

知道如何在rgb中获得精确的颜色吗?

代码已包含在下面。

    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
      }

问题是颜色不匹配。

感谢

下面的代码可以将#dfdfdf转换为(239,239,239)

<?php
     function hex2rgb( $colour ) {
     if ( $colour[0] == '#' ) {
    $colour = substr( $colour, 1 );
     }
     if ( strlen( $colour ) == 6 ) {
    list( $r, $g, $b ) = array( $colour[0] . $colour[1], $colour[2] . $colour[3], $colour[4] . $colour[5] );
     } elseif ( strlen( $colour ) == 3 ) {
    list( $r, $g, $b ) = array( $colour[0] . $colour[0], $colour[1] . $colour[1], $colour[2] . $colour[2] );
     } else {
     return false;
     }
    $r = hexdec( $r );
    $g = hexdec( $g );
    $b = hexdec( $b );
     return array( 'red' => $r, 'green' => $g, 'blue' => $b );
     }
     ?>