Home > Article > Backend Development > imagecolorclosest() function in PHP
imagecolorclosest() function gets the index of the color closest to the specified color.
imagecolorallocatealpha (img, red, green, blue)
img: Image resource created using imagecreatetruecolor().
red: Red component
green: Green component
blue: Blue component
The imagecolorclosest() function returns the closest color in the image palette index of.
The following is an example:
Demonstration
<?php $img = imagecreatefrompng('https://www.tutorialspoint.com/assets/videos/courses/19/images/course_19_image.png'); imagetruecolortopalette($img, false, 255); $val = imagecolorclosest($img, 20, 90, 140); $val = imagecolorsforindex($img, $val); $val = "({$val['red']}, {$val['green']}, {$val['blue']})"; echo "Closest = " . $val; imagedestroy($img); ?>
The following is the output:
Closest = (44, 118, 140)
Let’s see another example where we have different image and color components:
Live Demo
<?php $img = imagecreatefrompng('http://www.tutorialspoint.com/images/Swift.png'); imagetruecolortopalette($img, false, 255); $val = imagecolorclosest($img, 10, 130, 80); $val = imagecolorsforindex($img, $val); $val = "({$val['red']}, {$val['green']}, {$val['blue']})"; echo "Closest = " . $val; imagedestroy($img); ?>
Below is the output Result:
Closest = (228, 74, 76)
The above is the detailed content of imagecolorclosest() function in PHP. For more information, please follow other related articles on the PHP Chinese website!