Mostrando entradas con la etiqueta algorithm. Mostrar todas las entradas
Mostrando entradas con la etiqueta algorithm. Mostrar todas las entradas

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

20091015

Create the first bracket of a tournament


Bueno tetes, no tinc molt de temps però si algun cop us heu trobat en el cas de com crear un bracket per un torneig, no es del tot trivial.

Us deixo un troç de codi que el seu output es una array on cada element s'ha d'emparellar amb el següent per construir un bracket inicial d'un torneig ben balancejadet (com els de dragonball, vamos)

Evidentment $inicial es el primer jugador i $final es l'ultim, segur que no funciona si no son números de jugadors correctes, com 8,16,32,64... etc.


$inicial=1;
$final=32;
$num_jugadores=$final-($inicial-1);
$emparejamientos=array(1,4,2,3);
$num_jugadores_tmp=4;
while ($num_jugadores_tmp!=$num_jugadores)
{// desdoblabiento
$num_jugadores_tmp=$num_jugadores_tmp*2;
$emparejamientos_tmp=array();
foreach($emparejamientos as $emparejamiento)
{
array_push($emparejamientos_tmp, $emparejamiento);
array_push($emparejamientos_tmp
, ($num_jugadores_tmp+1)-$emparejamiento);
}
$emparejamientos=$emparejamientos_tmp;
}
print_r($emparejamientos);


Resultat per 16 jugadors:

Array ( [0] => 1 [1] => 16 [2] => 8 [3] => 9 [4] => 4
[5] => 13 [6] => 5 [7] => 12 [8] => 2 [9] => 15
[10] => 7 [11] => 10 [12] => 3 [13] => 14 [14] => 6
[15] => 11 )


I després de tractar-ho i simular resultats:

Ronda 1
1: 1 vs. 16 winner=1
2: 8 vs. 9 winner=9
3: 4 vs. 13 winner=13
4: 5 vs. 12 winner=12
5: 2 vs. 15 winner=2
6: 7 vs. 10 winner=7
7: 3 vs. 14 winner=14
8: 6 vs. 11 winner=6