note 106432 added to function.imagecolorat

From: Date: Wed, 09 Nov 2011 10:19:39 +0000
Subject: note 106432 added to function.imagecolorat
Groups: php.notes 
Request: Send a blank email to php-notes+get-184347@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: 69.147.83.197 Probable Submitter: 178.198.39.226 ---- Manual Page -- http://www.php.net/manual/en/function.imagecolorat.php Edit -- https://master.php.net/note/edit/106432 Del: integrated -- https://master.php.net/note/delete/106432/integrated Del: useless -- https://master.php.net/note/delete/106432/useless Del: bad code -- https://master.php.net/note/delete/106432/bad+code Del: spam -- https://master.php.net/note/delete/106432/spam Del: non-english -- https://master.php.net/note/delete/106432/non-english Del: in docs -- https://master.php.net/note/delete/106432/in+docs Del: other reasons-- https://master.php.net/note/delete/106432 Reject -- https://master.php.net/note/reject/106432 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#184347) next »