note 106433 added to function.imagecolorclosest
| From: | info at codeworx dot ch | Date: | Wed, 09 Nov 2011 10:21:21 +0000 |
| Subject: | note 106433 added to function.imagecolorclosest | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-184348@lists.php.net to get a copy of this message | ||
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);
}
?>
----
Server IP: 194.153.94.10
Probable Submitter: 178.198.39.226
----
Manual Page -- http://www.php.net/manual/en/function.imagecolorclosest.php
Edit -- https://master.php.net/note/edit/106433
Del: integrated -- https://master.php.net/note/delete/106433/integrated
Del: useless -- https://master.php.net/note/delete/106433/useless
Del: bad code -- https://master.php.net/note/delete/106433/bad+code
Del: spam -- https://master.php.net/note/delete/106433/spam
Del: non-english -- https://master.php.net/note/delete/106433/non-english
Del: in docs -- https://master.php.net/note/delete/106433/in+docs
Del: other reasons-- https://master.php.net/note/delete/106433
Reject -- https://master.php.net/note/reject/106433
Search -- https://master.php.net/manage/user-notes.php