이미지 처리 및 GD imagecolorclosest

(PHP 4, PHP 5, PHP 7, PHP 8)

imagecolorclosest — 지정된 색상에 가장 가까운 색상의 인덱스를 가져옵니다.


설명

imagecolorclosest(
    GdImage $image,
    int $red,
    int $green,
    int $blue
): int
                

지정된 RGB 값에 "가장 가까운" 이미지 팔레트의 색상 인덱스를 반환합니다.

원하는 색상과 팔레트의 각 색상 사이의 "distance"는 RGB 값이 3차원 공간의 점을 나타내는 것처럼 계산됩니다.

파일에서 이미지를 만든 경우 이미지에 사용된 색상만 해석됩니다. 팔레트에만 있는 색상은 해결되지 않습니다.


매개변수

image
imagecreatetruecolor()와 같은 이미지 생성 함수 중 하나에서 반환되는 GdImage 객체.
red
빨간색 구성 요소의 값입니다.
green
녹색 구성 요소의 가치.
blue
파란색 구성 요소의 값입니다.

색상 매개변수는 0에서 255 사이의 정수 또는 0x00에서 0xFF 사이의 16진수입니다.


반환 값

이미지 팔레트에서 지정된 색상에 가장 가까운 색상의 인덱스를 반환합니다.


변경 로그

버전 설명
8.0.0 image는 이제 href="class.gdimage.php">GdImage 인스턴스를 예상합니다. 이전에는 리소스가 필요했습니다.

Examples

예제 #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),
    array(153, 145, 188),
    array(153, 90, 145),
    array(255, 137, 92)
);

// 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 = imagecolorclosest($im, $rgb[0], $rgb[1], $rgb[2]);
    $result = imagecolorsforindex($im, $result);
    $result = "({$result['red']}, {$result['green']}, {$result['blue']})";

    echo "#$id: Search ($rgb[0], $rgb[1], $rgb[2]); Closest match: $result.\n";
}

imagedestroy($im);
?>
                  
                

위의 예는 다음과 유사한 결과를 출력합니다.

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

기타