【问题标题】:How to get the rgb colors from an already allocated color in PHP GD如何从 PHP GD 中已分配的颜色中获取 rgb 颜色
【发布时间】:2011-04-01 17:04:56
【问题描述】:

如何从分配的颜色中获取颜色 类似...

$col = imagecolorallocate($im, 255, 50, 5);
//A fake function - rgbfromallocate (that I wish I knew)

$rgb = rgbfromallocate($col);
print $rgb['r'];//255
print $rgb['g'];//50
print $rgb['b'];//5

【问题讨论】:

  • 如果你只是 print_r $col 会发生什么?尽管未经测试,imagecolourallocate 页面上的第一条评论表明该函数实际上只是简单地返回一个由 4 对组成的十六进制值 - alpha、red、green、blue - 也许它的存储非常简单?
  • Stephen:$col 是一个数字,表示图像调色板中的颜色。如果此图像具有 RGB 调色板,就像许多图像一样,那么您所说的就是这种情况。但是,如果图像使用其他调色板,则必须使用imagecolorsforindex() 来获取颜色的 RGB 值。

标签: php colors gd rgb


【解决方案1】:

也许imagecolorsforindex() 就是您要找的东西?

例如:

// $img is an image
$color = imagecolorallocate($img, 255, 50, 5);

$rgb = imagecolorsforindex($img, $color);
print $rgb['red']; // 255
print $rgb['green']; // 50
print $rgb['blue']; // 5
print $rgb['alpha']; // 0, but if the image uses the alpha channel,
// this would have a value of up to 127, which is fully transparent

【讨论】:

  • 完美运行,我原以为该功能只能与 imagecolorat 一起使用,但事实就是如此。
猜你喜欢
  • 2011-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-21
  • 1970-01-01
  • 2016-08-10
  • 2013-07-03
相关资源
最近更新 更多