PHP imagecolorclosestalpha - 取得與指定的顏色加透明度最接近的顏色的索引

PHP 圖像處理PHP 圖像處理

imagecolorclosestalpha — 取得與指定的顏色加透明度最接近的顏色的索引。

語(yǔ)法

int imagecolorclosestalpha ( resource $image , int $red , int $green , int $blue , int $alpha )

返回圖像調(diào)色板中與指定的 RGB 值以及 alpha 深度最"接近"的顏色。

參數(shù)

  • image 由圖像創(chuàng)建函數(shù)(例如 imagecreatetruecolor())返回的圖像資源。
  • red 紅色成分的值。
  • green 綠色成分的值。
  • blue 藍(lán)色成分的值。
  • alpha 一個(gè)介于 0 和 127 之間的值。0 表示完全不透明,127 表示完全透明。

顏色參數(shù)是介于 0 和 255 之間的整數(shù),或者是介于 0x00 和 0xFF 之間的十六進(jìn)制數(shù)。

返回值

返回調(diào)色板中最接近的顏色的索引。

實(shí)例

搜索圖像中的一組顏色。

<?php
// 從一個(gè)圖像開(kāi)始,并將其轉(zhuǎn)換為一個(gè)基于調(diào)色板的圖像
$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)
);

// 循環(huán)遍歷,查找調(diào)色板中最接近的顏色
// 返回搜索次數(shù),搜索的 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);
?>

以上實(shí)例的輸出類似于:

#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)。

相關(guān)文章

PHP 圖像處理PHP 圖像處理