2024 日本 PHP 研討會

imagecolorclosestalpha

(PHP 4 >= 4.0.6, PHP 5, PHP 7, PHP 8)

imagecolorclosestalpha取得與指定顏色 + alpha 最接近的顏色的索引

說明

imagecolorclosestalpha(
    GdImage $image,
    int $red,
    int $green,
    int $blue,
    int $alpha
): int

傳回影像調色盤中最接近指定 RGB 值和 alpha 級別的顏色索引。

參數

image

一個 GdImage 物件,由其中一個影像建立函式(例如 imagecreatetruecolor())傳回。

red

紅色成分的值。

green

綠色成分的值。

blue

藍色成分的值。

alpha

介於 0127 之間的值。 0 表示完全不透明,而 127 表示完全透明。

顏色參數是介於 0 和 255 之間的整數,或是介於 0x00 和 0xFF 之間的十六進位值。

傳回值

傳回調色盤中最接近顏色的索引。

範例

範例 #1 在影像中搜尋一組顏色

<?php
// Start with an image and convert it to a palette-based image
$im = imagecreatefrompng('figures/imagecolorclosest.png');
imagetruecolortopalette($im, false, 255);

// Search colors (RGB)
$colors = array(
array(
254, 145, 154, 50),
array(
153, 145, 188, 127),
array(
153, 90, 145, 0),
array(
255, 137, 92, 84)
);

// Loop through each search and find the closest color in the palette.
// Return the search number, the search RGB and the converted RGB match
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: Search ($rgb[0], $rgb[1], $rgb[2], $rgb[3]); Closest match: $result.\n";
}

imagedestroy($im);
?>

上述範例將輸出類似以下的內容

#0: Search (254, 145, 154, 50); Closest match: (252, 150, 148, 0).
#1: Search (153, 145, 188, 127); Closest match: (148, 150, 196, 0).
#2: Search (153, 90, 145, 0); Closest match: (148, 90, 156, 0).
#3: Search (255, 137, 92, 84); Closest match: (252, 150, 92, 0).

另請參閱

新增註記

使用者貢獻的註記

此頁面沒有使用者貢獻的註記。
To Top