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://www.codercto.com/courses/l/5.html

查看所有标签

An Introduction to Genetic Algorithms

An Introduction to Genetic Algorithms

Melanie Mitchell / MIT Press / 1998-2-6 / USD 45.00

Genetic algorithms have been used in science and engineering as adaptive algorithms for solving practical problems and as computational models of natural evolutionary systems. This brief, accessible i......一起来看看 《An Introduction to Genetic Algorithms》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

在线进制转换器
在线进制转换器

各进制数互转换器

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具