20100124

How to get a contrasting font color for a given background ?

This is a sample code to give a font color with a good contrast for a given background color.

That php code snipped could be useful in case you give your users the option to set a custom background but not the font color (to avoid too many options) and you want that your text are always visible.

It simply returns a font color white or black depending on the background, but you can modify the function get_contrast if you want something more sophisticated.

The main function is:


function get_font_color($color)
{
$ar=html2rgb($color);
return get_contrast($ar[0], $ar[1], $ar[2]);
}


Supporting functions:

function get_contrast ($r, $g, $b)
{// returns white or black depending on the background
// in red green and blue
$a = 1 - ( 0.299*$r + 0.587*$g + 0.114*$b)/255;
if ($a<0.5)
{
return '#000';
}
else
{
return '#fff';
}
}

function html2rgb($color)
{// gets an array of R,G,B from an hexadecimal
// color in html format
if ($color[0] == '#') $color = substr($color, 1);

if (strlen($color) == 6)
{
list($r, $g, $b) = array($color[0].$color[1],
$color[2].$color[3],
$color[4].$color[5]);
}
elseif (strlen($color) == 3)
{
list($r, $g, $b) = array($color[0].$color[0],
$color[1].$color[1],
$color[2].$color[2]);
}
else
{
return false;
}
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);

return array($r, $g, $b);
}


Unit testing:

$i=0;
while ($i<1600000)
{
$rgb='#'.strtoupper(str_pad(dechex($i), 6, '0', STR_PAD_LEFT));
echo '<div style="background-color:'.$rgb.'; color:'.get_font_color($rgb).'">'.$rgb.'</div>';
$i++;
}


Thanks to this web entries:
Determine font color based on background color
Convert RGB from an HTML Hex Color

20100105

Post final d'any, resum del meu 2009

Com es tradicional, poso una llista de les coses que m'han molat el 2009, no tinc temps d'editar-ho, ja em perdonareu.

Gadgets of the year (comprats o regalats):
canon ixus 85 is
roomba pet series
iplus
hacer aspire 5535

Samarretes del any:
Desert

Friky coses guays:
vixml.com
yammer
palm pre
spottify
palm ares
palm web os
android

Coses que he aprés:
drupal
cakephp
memcached

Jocs als que m'he viciat aquest any:
red alert 3
star wars galaxies

Grups guays que he descobert aquest any:
The pains of being pure at heart

Millor concert al que he anat:
sr. Chinarro al apolo2
darren hayman plays hefner songs al apolo
Sr. Chinarro al parc de la estacio del nord
The pains of being pure at heart al Primavera

Millors pelis que he vist:
watchmen
ponyo en el acantilado
paranormal activity

Llibres que he llegit i m'han agradat:
ni de eva ni de adan, amelie nothomb
nadie es mas de aqui que tu, miranda july
confesiones de un gangster economico, john perkins
cuentos completos, Amy Hempel
gomora, roberto sabiano
nocturna, guillermo del toro (encara que la traduccio es fatal)
El Club de los elegidos, david rothkopf

Comics:
macanudo 4, liniers
macanudo 5, liniers
genesis, robert crumb

Series a les que m'he enganxat (per ordre d'adicció):
lost s5
gilmore girls s1, s2, s3, s4
dexter s3, s4
how i met your mother
medium
big bang theory s1 i s2
los tudor s3
fringe s1


Sortides al Extranjer:
Mexic, Riviera Maya
Japó

Millors Hotels als que he anat:
barcelo caribe beach

Millors restaurants:
el de carn de kobe a tokyo
els pescadors
gorria