-
Da esadecimale a rgb
Salve a tutti,
ho un problemino, dovrei trasformare un colore esdecimale il rgb con php.
io avevo trovato su internet questo codice ma non funziona...
Codice PHP:
$red = 100;
$green = 100;
$blue = 100;
$col=array();
if( eregi( "[#]?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $hex_color, $ret ) )
{
$red = hexdec( $ret[1] );
$green = hexdec( $ret[2] );
$blue = hexdec( $ret[3] );
}
qualcuno sa aiutarmi... grazie in anticipo
-
Metti il codice tra [php e /php]
Prova questo:
Codice PHP:
// Prepara l'array di colori hex
$hex_colors = array('#0066B2',
'#6CAAD9', '#B1D3EC', '#285577',
'#4C7899','#2C83E7', '#7C94A5',
'#B8C7D2','#D7E8F5', '#607C90');
while (list($key,$val) = each($hex_colors)){
$red = hexdec(substr($val, 1 , 2));
$green = hexdec(substr($val, 3 , 2));
$blue = hexdec(substr($val, 5 , 2));
$rgb_colors[$key] = "$red,$green,$blue";
}
E guarda la funzione hexdec, è completa di tanti begli esempi :lolloso:
-
Se no prova cosi :
Codice PHP:
$red = 100;
$green = 100;
$blue = 100;
$col=array();
if( eregi( "[#]?([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})", $hex_color, $ret ) )
{
$red = hexdec( $ret[0] ) . hexdec( $ret[0];
$green = hexdec( $ret[1] ) . hexdec( $ret[1];
$blue = hexdec( $ret[2] ) . hexdec( $ret[2];
}
-
grazie ho risolto...
Codice PHP:
$hex_color = str_replace("#", '', $hex_color);
$red = hexdec(substr($hex_color, 0, 2));
$green = hexdec(substr($hex_color, 2, 2));
$blue = hexdec(substr($hex_color, 4, 2));