astra_hex_to_rgba( string $color, boolean $opacity = false )
Convert colors from HEX to RGBA
Description
Parameters
- $color
-
(string) (Required) Color code in HEX.
- $opacity
-
(boolean) (Optional) Color code opacity.
Default value: false
Return
(string) Color code in RGB or RGBA.
Source
File: classes/helper-functions.php
function astra_hex_to_rgba( $color, $opacity = false ) { $default = 'rgb(0,0,0)'; // Return default if no color provided. if ( empty( $color ) ) { return $default; } // Sanitize $color if "#" is provided. if ( '#' == $color[0] ) { $color = substr( $color, 1 ); } // Check if color has 6 or 3 characters and get values. if ( 6 == strlen( $color ) ) { $hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] ); } elseif ( 3 == strlen( $color ) ) { $hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] ); } else { return $default; } // Convert HEX to RGB. $rgb = array_map( 'hexdec', $hex ); // Check if opacity is set(RGBA or RGB). if ( $opacity ) { if ( 1 < abs( $opacity ) ) { $opacity = 1.0; } $output = 'rgba(' . implode( ',', $rgb ) . ',' . $opacity . ')'; } else { $output = 'rgb(' . implode( ',', $rgb ) . ')'; } // Return RGB(a) color string. return $output; }
Expand full source code Collapse full source code View on Trac