PHP 8.3.4 Released!

imagecolorclosestalpha

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

imagecolorclosestalpha指定した色+アルファ値に最も近い色のインデックスを取得する

説明

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

指定した RGB 値と alpha レベルに 「近い」画像パレット中の色のインデックスを返します。

パラメータ

image

imagecreatetruecolor()のような画像作成関数が返す GdImage オブジェクト。

red

赤コンポーネントの値。

green

緑コンポーネントの値。

blue

青コンポーネントの値。

alpha

0 から 127 までの値。 0 は完全に不透明な状態。 127 は完全に透明な状態を表します。

色のパラメータは、0 から 255 までの整数値か 0x00 から 0xFF までの十六進値を指定します。

戻り値

画像パレット内で、指定した色にいちばん近い色のインデックスを返します。

例1 画像内での色セットの検索

<?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: 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).

参考

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top