Jump to content

User:Tpatru/vector.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
<?php
/*
Color Selection By Contrast
Mark C. Roduner, Jr.
*/
function color_contrast($color_1, $color_2) {
$red_1 = ($color_1 & 0xff0000) >16;
$red_2 = ($color_2 & 0xff0000) >16;
$grn_1 = ($color_1 & 0xff00) >8;
$grn_2 = ($color_2 & 0xff00) >8;
$blu_1 = ($color_1 & 0xff);
$blu_2 = ($color_2 & 0xff);
return (abs($red_1 - $red_2) + abs($grn_1 - $grn_2) + abs($blu_1 -
$blu_2));
}

$source = 0xff00ff;
$seeker = $marker = 0xffffff;
$target = 500;
$best_contrast = 0;
$best_color = 0;

$contrast = color_contrast($source, $seeker);
while(($contrast < $target) and ($marker != 0)) {
$marker = intval($marker / 2);
if($contrast $target) {
$seeker -= $marker;
} else if($contrast < $target) {
$seeker += $marker;
}
$contrast = color_contrast($source, $seeker);
if($contrast $best_contrast) {
$best_contrast = $contrast;
$best_color = $seeker;
}
}
echo("\n Color: " . dechex($best_color) . " Contrast $best_contrast");
?>