Here is a function that compares two HEX colors for similarity. This can be useful if you want to detect colors that are not different enough to see for the naked eye. It returns a bool: TRUE if the colors are similar to each other (within tolerance) or FALSE if they are different enough.
I tested a few colors and came up with a tolerance of 35 (rgb value - should be between 0 and 255) but you can change that tolerance by passing a third parameter to the function.
<?php
function compareColors ($col1, $col2, $tolerance=35) {
$col1Rgb = array(
"r" => hexdec(substr($col1, 0, 2)),
"g" => hexdec(substr($col1, 2, 2)),
"b" => hexdec(substr($col1, 4, 2))
);
$col2Rgb = array(
"r" => hexdec(substr($col2, 0, 2)),
"g" => hexdec(substr($col2, 2, 2)),
"b" => hexdec(substr($col2, 4, 2))
);
return ($col1Rgb['r'] >= $col2Rgb['r'] - $tolerance && $col1Rgb['r'] <= $col2Rgb['r'] + $tolerance) && ($col1Rgb['g'] >= $col2Rgb['g'] - $tolerance && $col1Rgb['g'] <= $col2Rgb['g'] + $tolerance) && ($col1Rgb['b'] >= $col2Rgb['b'] - $tolerance && $col1Rgb['b'] <= $col2Rgb['b'] + $tolerance);
}
?>
imagecolorclosest
(PHP 4, PHP 5)
imagecolorclosest — Получение индекса цвета ближайшего к заданному
Описание
$image
, int $red
, int $green
, int $blue
)Возвращает индекс цвета в палитре изображения, "ближайшего" к заданному RGB значению.
"Расстояние" между цветами в палитре рассчитывается геометрически, как если бы RGB значения были представлены в виде точек в трехмерном пространстве.
Если изображение было создано из файла, то будут распознаны только цвета, используемые в изображении. Цвета, которые используются только в палитре, распознаны не будут.
Список параметров
-
image -
Ресурс изображения, полученный одной из функций создания изображений, например, такой как imagecreatetruecolor().
-
red -
Значение красного компонента цвета.
-
green -
Значение зеленого компонента цвета.
-
blue -
Значение синего компонента цвета.
Возвращаемые значения
Возвращает индекс цвета в палитре изображения, ближайшего к заданному.
Примеры
Пример #1 Поиск набора цветов изображения
<?php
// Создание изображения и преобразование его в палитровое
$im = imagecreatefrompng('figures/imagecolorclosest.png');
imagetruecolortopalette($im, false, 255);
// Цвета для поиска (RGB)
$colors = array(
array(254, 145, 154),
array(153, 145, 188),
array(153, 90, 145),
array(255, 137, 92)
);
// Проход по каждому цвету и поиск ближайшего к нему в палитре.
// Возврат номера по порядку, RGB искомого цвета и найденное RGB соответствие
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).
Смотрите также
- imagecolorexact() - Получение индекса заданного цвета
- imagecolorclosestalpha() - Получение индекса цвета ближайшего к заданному с учетом прозрачности
- imagecolorclosesthwb() - Получение индекса цвета, имеющего заданный тон, белизну и затемнение
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.
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);
