Contrast Color
Moderator: General Moderators
-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
Contrast Color
I cant find a function to take a background color and generate a foreground color that is the optimum contrast and brightness according to http://www.w3.org/TR/AERT#color-contrast. Can anyone create such a function or know where one might be?
-
chris12295
- Forum Contributor
- Posts: 113
- Joined: Sun Jun 09, 2002 10:28 pm
- Location: USA
- Contact:
this is my code so far but it doesnt seem to be working any ideas?
Code: Select all
<?php
class csscolor {
function __construct($color1, $color2, $steps) {
$this->makeShades($color1, $color2, $steps);
}
private function makeShades($c1, $c2, $steps) {
$start['r'] = hexdec(substr($c1, 0, 2));
$start['g'] = hexdec(substr($c1, 2, 2));
$start['b'] = hexdec(substr($c1, 4, 2));
$end['r'] = hexdec(substr($c2, 0, 2));
$end['g'] = hexdec(substr($c2, 2, 2));
$end['b'] = hexdec(substr($c2, 4, 2));
$step['r'] = ($start['r'] - $end['r']) / ($steps);
$step['g'] = ($start['g'] - $end['g']) / ($steps);
$step['b'] = ($start['b'] - $end['b']) / ($steps);
for($i=0;$i<$steps;$i++):
$rgb['r'] = round($start['r'] - ($step['r'] * $i));
$rgb['g'] = round($start['g'] - ($step['g'] * $i));
$rgb['b'] = round($start['b'] - ($step['b'] * $i));
$hex['r'] = sprintf('%02x', ($rgb['r']));
$hex['g'] = sprintf('%02x', ($rgb['g']));
$hex['b'] = sprintf('%02x', ($rgb['b']));
if($i==$steps-1):
$this->bg[$i] = strtoupper($c2);
break;
endif;
$this->bg[$i] = strtoupper(implode(NULL, $hex));
$this->fg[$i] = $this->calcfg($this->bg[$i]);
endfor;
}
private function calcfg($bg) {
for($i=0;$i<1;$i+=0.1):
$darker = $this->darken($bg, $i);
$lighter = $this->lighten($bg, $i);
$lightBrightDiff = $this->brightnessDiff($bg, $lighter);
$darkBrightDiff = $this->brightnessDiff($bg, $darker);
if($lightBrightDiff > $darkBrightDiff):
$newfg = $lighter;
$newFGBrightDiff = $lightBrightDiff;
else:
$newfg = $darker;
$newFGBrightDiff = $darkBrightDiff;
endif;
$newColorDiff = $this->colorDiff($bg, $newfg);
if($newFGBrightDiff >= 126 && $newColorDiff >= 500) break;
endfor;
return $newfg;
}
private function lighten($hex, $percent) {
$dec['r'] = hexdec(substr($hex, 0, 2));
$dec['g'] = hexdec(substr($hex, 2, 2));
$dec['b'] = hexdec(substr($hex, 4, 2));
$rhex['r'] = dechex(round($dec['r'] * $percent) + $dec['r']);
$rhex['g'] = dechex(round($dec['g'] * $percent) + $dec['g']);
$rhex['b'] = dechex(round($dec['b'] * $percent) + $dec['b']);
return implode(NULL, $rhex);
}
private function darken($hex, $percent) {
$dec['r'] = hexdec(substr($hex, 0, 2));
$dec['g'] = hexdec(substr($hex, 2, 2));
$dec['b'] = hexdec(substr($hex, 4, 2));
$rhex['r'] = dechex($dec['r']-round($dec['r'] * $percent));
$rhex['g'] = dechex($dec['g']-round($dec['g'] * $percent));
$rhex['b'] = dechex($dec['b']-round($dec['b'] * $percent));
return implode(NULL, $rhex);
}
private function brightness($hex) {
$dec['r'] = hexdec(substr($hex, 0, 2));
$dec['g'] = hexdec(substr($hex, 2, 2));
$dec['b'] = hexdec(substr($hex, 4, 2));
return ( ($dec['r']*299) + ($dec['g']*587) + ($dec['b']*114)/1000 );
}
private function brightnessDiff($hex1, $hex2) {
$b1 = $this->brightness($hex1);
$b2 = $this->brightness($hex2);
return abs($b1 - $b2);
}
private function colorDiff($color1, $color2) {
$hex1['r'] = hexdec(substr($color1, 0, 2));
$hex1['g'] = hexdec(substr($color1, 2, 2));
$hex1['b'] = hexdec(substr($color1, 4, 2));
$hex2['r'] = hexdec(substr($color2, 0, 2));
$hex2['g'] = hexdec(substr($color2, 2, 2));
$hex2['b'] = hexdec(substr($color2, 4, 2));
return (abs($hex1['r']-$hex2['r']) + abs($hex1['g']-$hex2['g']) + abs($hex1['b']-$hex2['b']));
}
}
?>