Current Location: Home> Latest Articles> How to Use the imagecolorclosest Function to Find the Closest Color Index in an Image?

How to Use the imagecolorclosest Function to Find the Closest Color Index in an Image?

gitbox 2025-08-29

Step-by-Step Guide

  1. Call imagecolorclosest()

    • Pass in the RGB values of the target color. The function will return the closest color index from the image's palette.

  2. Use the returned color index

    • You can use this index to draw, replace colors, or retrieve color information.


Code Example

<span><span><span class="hljs-comment">// Load a palette-based image</span></span><span>  
</span><span><span class="hljs-variable">$image</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecreatefromgif</span></span><span>(</span><span><span class="hljs-string">&#039;palette_image.gif&#039;</span></span><span>);  
</span><span><span class="hljs-comment">// Define the target color to match, e.g., light blue</span></span><span>  
</span><span><span class="hljs-variable">$targetRed</span></span><span> = </span><span><span class="hljs-number">100</span></span><span>;  
</span><span><span class="hljs-variable">$targetGreen</span></span><span> = </span><span><span class="hljs-number">150</span></span><span>;  
</span><span><span class="hljs-variable">$targetBlue</span></span><span> = </span><span><span class="hljs-number">200</span></span><span>;  
</span><span><span class="hljs-comment">// Get the closest color index</span></span><span>  
</span><span><span class="hljs-variable">$closestColorIndex</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecolorclosest</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-variable">$targetRed</span></span><span>, </span><span><span class="hljs-variable">$targetGreen</span></span><span>, </span><span><span class="hljs-variable">$targetBlue</span></span>);  
</span><span><span class="hljs-comment">// Get color components from the color index</span></span><span>  
</span><span><span class="hljs-variable">$colors</span></span><span> = </span><span><span class="hljs-title function_ invoke__">imagecolorsforindex</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>, </span><span><span class="hljs-variable">$closestColorIndex</span></span>);  
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The closest color index is: "</span></span><span> . </span><span><span class="hljs-variable">$closestColorIndex</span></span><span> . </span><span><span class="hljs-string">"\n"</span></span><span>;  
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"The RGB values of the corresponding color are: R=<span class="hljs-subst">{$colors[&#039;red&#039;]}</span></span></span><span> G=</span><span><span class="hljs-subst">{$colors[&#039;green&#039;]}</span></span><span> B=</span><span><span class="hljs-subst">{$colors[&#039;blue&#039;]}</span></span><span>\n";  
</span><span><span class="hljs-comment">// Free up image resources</span></span><span>  
</span><span><span class="hljs-title function_ invoke__">imagedestroy</span></span><span>(</span><span><span class="hljs-variable">$image</span></span><span>);  
</span></span>

Notes

  • imagecolorclosest only works for palette-based images. For true color images, color matching is not very meaningful because true color images do not have a fixed palette.

  • If there is no exact match for the color in the image's palette, the function returns the closest color index, ensuring the smallest color difference.

  • This function is efficient and suitable for real-time color lookup needs.