PHP: imagecolorclosest - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

imagecolorclosestalpha imagecolorat

Last updated: Fri, 26 Jan 2007

view this page in

# imagecolorclosest

(PHP 3, PHP 4, PHP 5)

imagecolorclosest -- Get the index of the closest color to the specified color

## Описание

int imagecolorclosest ( resource image, int red, int green, int blue )

Returns the index of the color in the palette of the image which is "closest" to the specified RGB value.

The "distance" between the desired color and each color in the palette is calculated as if the RGB values represented points in three-dimensional space.

If you created the image from a file, only colors used in the image are resolved. Colors present only in the pallete are not resolved.

## Список параметров

`image`

  An image resource, returned by one of the image creation functions, such as imagecreatetruecolor().

`red`

  Value of red component

`green`

  Value of green component

`blue`

  Value of blue component

The colors parameters are integers between 0 and 255 or hexadecimals between 0x00 and 0xFF.

## Возвращаемые значения

Returns the index of the closest color, in the palette of the image, to the specified one

## Смотрите также

imagecolorexact()

add a note User Contributed Notes
imagecolorclosest
MagicalTux at FF dot st
28-Jun-2005 06:22
`A way to get each time an answer :
<?php
function imagegetcolor($im, $r, $g, $b) {
$c=imagecolorexact($im, $r, $g, $b);
if ($c!=-1) return $c;
$c=imagecolorallocate($im, $r, $g, $b);
if ($c!=-1) return $c;
return imagecolorclosest($im, $r, $g, $b);
}
?>
If the *exact* color is found in the image, it will be returned. If we don't have the exact color, we try to allocate it. If we can't allocate it, we return the closest color in the image.`
Vikrant Korde <vakorde at hotmail dot com>
11-Oct-2003 08:10
`This functuion is useful when you are dealing with previously present images like .png, .jpg, etc. You can use this function for writing a text on the image.
For me the function imagecolorallocate() was returning the -1 value. after lot of RnD and site search i found a function use of imagecolorclosest(). This solved my problem of writing the text on the image with customised color.
Actually previously it was writing on the image but the color was not distinct. It was using the same color as of that background image.
The following code segment was fine with me.
header ("Content-type: image/jpeg");
$im = imagecreatefromjpeg("BlankButton.jpg");
$white = imageColorClosest($im, 255,255,255);
// this is for TTF fonts
imagettftext ($im, 20, 0, 16, 30, $white, "/usr/X11R6/lib/X11/fonts/TTF/luximb.ttf", "Vikrant");
//this is for notmal font
imagestring($im, 4, 0,0,"Korde", $white);
imagejpeg($im,"",150);
imagedestroy ($im);`
add a note
imagecolorclosestalpha imagecolorat

Last updated: Fri, 26 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 09:32:46 2007 PST