PHP imagecolorclosestalpha - 取得与指定的颜色加透明度最接近的颜色的索引
PHP 教程
· 2019-01-31 10:59:43
imagecolorclosestalpha — 取得与指定的颜色加透明度最接近的颜色的索引。
语法
int imagecolorclosestalpha ( resource $image , int $red , int $green , int $blue , int $alpha )
返回图像调色板中与指定的 RGB 值以及 alpha 深度最"接近"的颜色。
参数
- image 由图像创建函数(例如 imagecreatetruecolor())返回的图像资源。
- red 红色成分的值。
- green 绿色成分的值。
- blue 蓝色成分的值。
- alpha 一个介于 0 和 127 之间的值。0 表示完全不透明,127 表示完全透明。
颜色参数是介于 0 和 255 之间的整数,或者是介于 0x00 和 0xFF 之间的十六进制数。
返回值
返回调色板中最接近的颜色的索引。
实例
搜索图像中的一组颜色。
<?php
// 从一个图像开始,并将其转换为一个基于调色板的图像
$im = imagecreatefrompng('figures/imagecolorclosest.png');
imagetruecolortopalette($im, false, 255);
// 搜索颜色 (RGB)
$colors = array(
array(254, 145, 154, 50),
array(153, 145, 188, 127),
array(153, 90, 145, 0),
array(255, 137, 92, 84)
);
// 循环遍历,查找调色板中最接近的颜色
// 返回搜索次数,搜索的 RGB 和最接近的匹配的 RGB
foreach($colors as $id => $rgb)
{
$result = imagecolorclosestalpha($im, $rgb[0], $rgb[1], $rgb[2], $rgb[3]);
$result = imagecolorsforindex($im, $result);
$result = "({$result['red']}, {$result['green']}, {$result['blue']}, {$result['alpha']})";
echo "#$id: 搜索 ($rgb[0], $rgb[1], $rgb[2], $rgb[3]); 最接近的匹配: $result。\n";
}
imagedestroy($im);
?>
以上实例的输出类似于:
#0: 搜索 (254, 145, 154, 50); 最接近的匹配: (252, 150, 148, 0)。 #1: 搜索 (153, 145, 188, 127); 最接近的匹配: (148, 150, 196, 0)。 #2: 搜索 (153, 90, 145, 0); 最接近的匹配: (148, 90, 156, 0)。 #3: 搜索 (255, 137, 92, 84); 最接近的匹配: (252, 150, 92, 0)。
相关文章
- imagecolorexactalpha() 取得指定的颜色加透明度的索引值。
- imagecolorclosest() 取得与指定的颜色最接近的颜色的索引值。
- imagecolorclosesthwb() 取得与给定颜色最接近的色度的黑白色的索引。
点击查看所有 PHP 教程 文章: https://codercto.com/courses/l/5.html
Ajax开发精要
柯自聪 / 电子工业出版社 / 2006 / 45.00
书籍目录: 概念篇 第1章 Ajax介绍 2 1.1 Ajax的由来 2 1.2 Ajax的定义 3 1.3 Web应用程序的解决方案 5 1.4 Ajax的工作方式 7 1.5 小结 8 第2章 B/S请求响应机制与Web开发模式 9 2.1 HTTP请求响应模型 9 2.2 B/S架构的请求响应机......一起来看看 《Ajax开发精要》 这本书的介绍吧!