PHP分配没有图像资源的颜色

问题描述 投票:3回答:4

你可以在没有PHP GD的情况下在image resource中分配颜色吗?它应该是可能的,因为实际上分配的颜色是一个数字,对吧?

$im = imagecreatetruecolor(100, 100);
$col = imagecolorallocate($im, 255, 0, 0);
print $col."<br/>";
$col2 = imagecolorallocate($im, 255, 0, 0);
print $col2."<br/>";
$im2 = imagecreatetruecolor(600, 100);
$col3 = imagecolorallocate($im, 255, 0, 0);
print $col3;

打印出:

16711680

16711680

16711680

我想真正的问题是如何将255,0和0制成16711680。

php resources colors gd rgb
4个回答
4
投票

16711680(十进制)是0x00FF0000(十六进制)

00 - 阿尔法值(0 dec)

FF - 红色(255 dec)

00 - 绿色(0月12日)

00 - 蓝色(0 dec)

请参阅http://www.php.net/manual/en/function.imagecolorallocatealpha.php以设置字母字节

编辑:

另外,要回答您的第一个问题 - 是的,您可以创建没有图像资源的颜色(因此无需调用imagecolorallocate):

$ col1 = 0x00FF0000; //红色

$ col2 = 0x0000FF00; // 绿色

//等...


1
投票

它应该是可能的,因为实际上分配的颜色是一个数字,对吧?

不,这不对。 GD也可能必须在图像的调色板中注册该颜色(想想非真彩色图像)。

所以你需要一个图像资源。


0
投票

使用此功能

function img_color($r, $g, $b, $a=0) {
  // prepare red color
  if (is_string($r)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $r)) return false;
    $r = hexdec($r);
  }
  elseif (is_int($r)) {if ($r<0 or $r>255) return false;}
  else return false;

  // prepare green color
  if (is_string($g)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $g)) return false;
    $g = hexdec($g);
  }
  elseif (is_int($g)) {if ($g<0 or $g>255) return false;}
  else return false;

  // prepare blue color
  if (is_string($b)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $b)) return false;
    $b = hexdec($b);
  }
  elseif (is_int($b)) {if ($b<0 or $b>255) return false;}
  else return false;

  // prepare alpha channel
  if (is_string($a)) {
    if (!preg_match('/^[a-f0-9]{1,2}$/i', $a)) return false;
    $a = hexdec($a);
  }
  elseif (!is_int($a)) return false;
  if ($a<0 or $a>127) return false;

  $result = unpack('I', chr($b) . chr($g) . chr($r) . chr($a));
  return $result[1];
}

0
投票

颜色索引或颜色id可以通过几种方式确定。这应该有助于发现之路。

更新***我不知道为什么,但在看到这个问题后,我编写了以下函数将<many>类型的颜色值转换为<many>类型的颜色表达式。希望它对某人有用。 *添加CMYK只是因为。 ...

选项-->'int','rgba','rgb','cmyk','hex','rgbaCSS','rgbCSS','hexCSS','hexCSS4'

如果有人想要变得非常严肃,他们可以编写可选参数来将格式转换为any;使用sprintf。

<?php
function convertColorValue($colorValue, $toType = 'hex', $recurse = 0) {
    if ($recurse > 2) { return $colorValue; }
    if (!is_array($colorValue)) {
        // OUT --> {Mixed} :: Options --> rgba || rgb || cmyk
        // If not an array then we have some form of Number - Valid values int || hex<string>
        // Please note that hex numbers such as 0x000000 that are not strings '0x000000' are interpreted by php as their number representation.
        // Shortcode hex values.
        if (is_string($colorValue) && $colorValue{0} === '#') {
            $start = 0;
            if (strlen($colorValue) === 4) {
                $rgb['alpha'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
            } else {
                $rgb['alpha'] = 0;
            }
            $rgb['r'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
            $rgb['g'] = hexdec(str_repeat(substr($colorValue, $start++, 1), 2));
            $rgb['b'] = hexdec(str_repeat(substr($colorValue, $start, 1), 2));
            // We need to make sure this follows some rules before we send it back even if it is the type we are being asked for.
            return convertColorValue($rgb, $toType);
        }
        // Make sure we have a clean sensible string for conversion.
        if (preg_match("/[^0-9a-f]/i", $colorValue) !== false) {
            if (($colorValue{0} === 0 && $colorValue{1} !== 'x'))
                $colorValue = '0x' . $colorValue;
        }
        $colorValue = preg_replace("/[^0-9a-fx]/i", '', $colorValue); // 4294967295 === 0xFFFFFFFF, the maximum color value.
        $colorValue = strlen((string) $colorValue) >= 10 ? (intval(substr($colorValue, 0, 10), 0) > 4294967295 ? 0 : substr($colorValue, 0, 10)) : $colorValue;
        // CONVERT our number to base 10 from whatever base it is in.
        // **** TODO: Hopefully int or hex, not sure what happens in other bases. ¯\_(ツ)_/¯
        // Hex strings should always be prepended with 0x, otherwise confusion happens 11110000 !=? 0x11110000
        // If not prepended here with 0x, I can't fix/predict which you are trying to represent int or hex. Int is naturally assumed.
        $color = [];
        $colorValue = intval(intval($colorValue, 0), 10);
        $color['r'] = ($colorValue >> 16) & 0xFF;
        $color['g'] = ($colorValue >> 8) & 0xFF;
        $color['b'] = $colorValue & 0xFF;
        $color['alpha'] = ($colorValue >> 24) & 0x7F;
        if ($toType === 'cmyk') {
            $c = (255 - $color['r']) / 255.0 * 100;
            $m = (255 - $color['g']) / 255.0 * 100;
            $y = (255 - $color['b']) / 255.0 * 100;
            $b = min([$c,$m,$y]);
            $c = $c - $b; $m = $m - $b; $y = $y - $b;
            return ['c' => $c, 'm' => $m, 'y' => $y, 'k' => $b, 'alpha' => $color['alpha']];
        }
        if ($toType === 'rgba' || $toType === 'rgb') {
            return $color;
        }
        return convertColorValue($color, $toType, ++$recurse);
    }
    // OUT --> {Mixed} :: Options --> int || hex || hexCSS || rgbCSS || rgbaCSS || hexCSS4
    // If they've thrown us a c, we are going to assume they gave us CMYK.
    if (isset($colorValue['c'])) {
        $colorValue['c'] = empty($colorValue['c']) ? 0 : $colorValue['c'] / 100;
        $colorValue['m'] = empty($colorValue['m']) ? 0 : $colorValue['m'] / 100;
        $colorValue['y'] = empty($colorValue['y']) ? 0 : $colorValue['y'] / 100;
        $colorValue['k'] = empty($colorValue['k']) ? 0 : $colorValue['k'] / 100;
        $colorValue['r'] = round((1 - ($colorValue['c'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
        $colorValue['g'] = round((1 - ($colorValue['m'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);
        $colorValue['b'] = round((1 - ($colorValue['y'] * (1 - $colorValue['k'])) - $colorValue['k']) * 255);

    } else {
        $colorValue['r'] = empty($colorValue['r']) ? 0 : $colorValue['r'];
        $colorValue['g'] = empty($colorValue['g']) ? 0 : $colorValue['g'];
        $colorValue['b'] = empty($colorValue['b']) ? 0 : $colorValue['b'];
    }
    $colorValue['alpha'] = (empty($colorValue['alpha']) || $colorValue['alpha'] > 0x7f) ? 0 : $colorValue['alpha'];
    if ($toType === 'rgbaCSS') {
        $color['alpha'] = empty($color['alpha']) ? 1 :
            (round(1 * $color['alpha'] / 127, 3, PHP_ROUND_HALF_DOWN));
        return "rgba({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']}, {$colorValue['alpha']})";
    }
    if ($toType === 'rgbCSS') {
        return "rgb({$colorValue['r']}, {$colorValue['g']}, {$colorValue['b']})";
    }
    // Just return the rgb value if opaque since '==' color-values.
    if (empty($colorValue['alpha'])) {
        $hex = sprintf("%02x%02x%02x", $colorValue['r'], $colorValue['g'], $colorValue['b']);
    } else {
        $hex = sprintf("%02x%02x%02x%02x", $colorValue['alpha'], $colorValue['r'], $colorValue['g'], $colorValue['b']);
    }
    $isCSS = ($toType === 'hexCSS4' || $toType === 'hexCSS');
    if ($toType === 'hex' || $isCSS) {
        return $isCSS ? ("#".$hex) : ("0x".$hex);
    }
    $hex = "0x".$hex;
    if ($toType === 'int') { return hexdec($hex); }
    // Is now a hex string. Going back UP^
    return convertColorValue($hex, $toType, ++$recurse);
}
© www.soinside.com 2019 - 2024. All rights reserved.