note 106432 deleted from function.imagecolorat by danbrown

From: Date: Tue, 15 Nov 2011 16:43:13 +0000
Subject: note 106432 deleted from function.imagecolorat by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-184493@lists.php.net to get a copy of this message
Note Submitter: info at codeworx dot ch ---- 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); } ?>

« previous php.notes (#184493) next »