【问题标题】:PHP allocate color without image resourcePHP在没有图像资源的情况下分配颜色
【发布时间】:2011-04-07 02:16:45
【问题描述】:

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

$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。

【问题讨论】:

  • $col = imagecolorallocate($im, 255, 0, 0);这将图像 $im 的背景设置为红色。返回值是一个颜色标识符,表示由给定 RGB 分量组成的颜色。
  • @PramendraGupta 仅在第一次通话时。引用docs:第一次调用imagecolorallocate() 填充基于调色板的图像中的背景颜色 - 使用imagecreate() 创建的图像。

标签: php resources colors gd rgb


【解决方案1】:

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

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

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

【讨论】:

【解决方案2】:

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

00 - Alpha 值 (0 dec)

FF - 红色(12 月 255 日)

00 - 绿色(0 dec)

00 - 蓝色(0 dec)

查看http://www.php.net/manual/en/function.imagecolorallocatealpha.php设置alpha字节

编辑:

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

$col1 = 0x00FF0000; // 红色

$col2 = 0x0000FF00; // 绿色

// 等等...

【讨论】:

  • @Artefacto True。对于基于调色板的图像,将返回从 0 到 255 的颜色 id(在调色板中)。调色板图像只能分配 256 种颜色,并且它不检查之前是否分配了颜色,因此优化 (@如果调色板颜色空间用完,可能需要 987654322@)。
【解决方案3】:

使用此功能

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];
}

【讨论】:

    【解决方案4】:

    颜色索引或颜色 ID 可以通过多种方式确定。这应该有助于探索之路。

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

    选项--&gt; 'int', 'rgba', 'rgb', 'cmyk', 'hex', 'rgbaCSS', 'rgbCSS', 'hexCSS', 'hexCSS4'

    如果有人想认真一点,他们可以编写可选参数来将格式转换为任意格式;使用 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);
    }
    

    【讨论】:

    • 当我在自己的脚本 sn-ps 中更新它时,我一直在尝试更新它,但它越来越烦人。这个 GIT 在 Elleus 项目的“图像”类here 中具有上述功能(可能已更新)。
    • 您的链接指向 404
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2014-08-19
    • 2023-03-10
    • 2011-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多