note 96780 added to function.hexdec

From: Date: Tue, 16 Mar 2010 09:03:07 +0000
Subject: note 96780 added to function.hexdec
Groups: php.notes 
Request: Send a blank email to php-notes+get-167081@lists.php.net to get a copy of this message
Someone might find this helpful. /** * Return an array of RGB color values from either a HEX string, * an array (R,G,B) or a comma deliminiated string (255, 255, 255). * @since 1.0.0 * @param string|array $value * @return array * @access public */ public function toRgb($value) { if (is_string($value)) { $value = str_replace('#', '', $value); $value = explode(',', $value); if (sizeof($value) == 1) { $value = $value[0]; if (strlen($value) == 6) { list($r, $g, $b) = array($value[0].$value[1], $value[2].$value[3], $value[4].$value[5]); } else if (strlen($value) == 3) { list($r, $g, $b) = array($value[0].$value[0], $value[1].$value[1], $value[2].$value[2]); } else { list($r, $g, $b) = array('FF', 'FF', 'FF'); } $value = array(hexdec($r), hexdec($g), hexdec($b)); } else if (sizeof($value) == 3) { $value = $this->toRgb($value); } } else if (is_array($value) && sizeof($value) == 3) { $colors = array(); foreach ($value as $color) { $color = intval($color); if ($color < 0) { array_push($colors, 0); } else if ($color > 255) { array_push($colors, 255); } else { array_push($colors, $color); } } $value = $colors; } else { $value = array(255, 255, 255); } return $value; } // Use-case: list($r, $g, $b) = $this->toRgb('#FF9900'); echo 'R: '.$r."\n"; echo 'G: '.$g."\n"; echo 'B: '.$b."\n"; // Outputs // R: 255 // G: 153 // B: 0 // Either combinations should work. // $this->toRgb('#FF9900'); // $this->toRgb(array(255, 153, 0)); // $this->toRgb('255, 153, 0') ---- Server IP: 69.147.83.197 Probable Submitter: 220.253.28.248 ---- Manual Page -- http://www.php.net/manual/en/function.hexdec.php Edit -- https://master.php.net/note/edit/96780 Del: integrated -- https://master.php.net/note/delete/96780/integrated Del: useless -- https://master.php.net/note/delete/96780/useless Del: bad code -- https://master.php.net/note/delete/96780/bad+code Del: spam -- https://master.php.net/note/delete/96780/spam Del: non-english -- https://master.php.net/note/delete/96780/non-english Del: in docs -- https://master.php.net/note/delete/96780/in+docs Del: other reasons-- https://master.php.net/note/delete/96780 Reject -- https://master.php.net/note/reject/96780 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#167081) next »