note 58219 added to function.dechex
| From: | cory at lavacube dot com | Date: | Thu, 27 Oct 2005 23:45:08 +0000 |
| Subject: | note 58219 added to function.dechex | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-97714@lists.php.net to get a copy of this message | ||
A handy little function to convert HEX colour codes to "web safe" colours...
<?php
function color_mkwebsafe ( $in )
{
// put values into an easy-to-use array
$vals['r'] = hexdec( substr($in, 0, 2) );
$vals['g'] = hexdec( substr($in, 2, 2) );
$vals['b'] = hexdec( substr($in, 4, 2) );
// loop through
foreach( $vals as $val )
{
// convert value
$val = ( round($val/51) * 51 );
// convert to HEX
$out .= str_pad(dechex($val), 2, '0', STR_PAD_LEFT);
}
return $out;
}
?>
Example: color_mkwebsafe('0e5c94');
Produces: 006699
Hope this helps someone out... Happy coding. :-)
----
Manual Page -- http://www.php.net/manual/en/function.dechex.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+58219
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58219&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58219&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58219&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58219&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58219&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58219&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58219&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+58219&report=yes
Search -- http://master.php.net/manage/user-notes.php