imagecolorclosestalpha — Gets the index of the color closest to the specified color plus transparency.
int imagecolorclosestalpha ( resource $image , int $red , int $green , int $blue , int $alpha )
Returns the color in the image palette that is "closest" to the specified RGB value and alpha depth.
imageThe image resource returned by the image creation function (such as imagecreatetruecolor()).
redThe value of the red component.
greenThe value of the green component.
blueThe value of the blue component.
alphaA value between 0 and 127. 0 means fully opaque, 127 means fully transparent.
The color parameter is an integer between 0 and 255, or a hexadecimal number between 0x00 and 0xFF.
Returns the index of the closest color in the palette.
Search an image for a set of colors.
<?php// Start with an image and convert it to a palette-based image $im = imagecreatefrompng('figures/imagecolorclosest.png');imagetruecolortopalette($im, false, 255);// Search for 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 and find the closest color in the palette // Return the number of searches, the RGB and closest matching RGBforeach($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 );?>
The output of the above example is similar to:
#0: Search for (254, 145, 154, 50); Closest match: (252, 150, 148, 0). #1: Search for (153, 145, 188, 127); Closest match: (148, 150, 196, 0). #2: Search for (153, 90, 145, 0); Closest match: (148, 90, 156, 0). #3: Search for (255, 137, 92, 84); Closest match: (252, 150, 92, 0).
imagecolorexactalpha() Gets the index value of the specified color plus transparency.
imagecolorclosest() Gets the index value of the color closest to the specified color.
imagecolorclosesthwb() Gets the black and white index of the tint closest to the given color.