Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- function hex2rgba($hex, $a = 0)
- {
- $hex = ltrim($hex, '#');
- if (strlen($hex) == 3) {
- $hex = $this->completeHex($hex);
- }
- $this->checkHexFormat($hex);
- $hexArray = str_split($hex,2); //for get r-g-b values
- $hexArray = array_map(function ($item) {
- return hexdec($item);
- }, $hexArray);
- $hexArray['alpha'] = $a;
- return "rgb($hexArray[0], $hexArray[1], $hexArray[2], $hexArray[alpha])";
- }
- function completeHex($hex)
- {
- return $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
- }
- function checkHexFormat($hex)
- {
- if (strlen($hex) !== 6) {
- throw new Exception('hex code can be 6 digit');
- }
- if (ctype_xdigit($hex) == false)
- {
- throw new Exception('unknown format');
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement