Home › Forums › Feature Requests › Color Picker values › Reply To: Color Picker values
Here’s a custom PHP solution that we’ve used for one of our projects, so that we don’t require another additional plugin. You should only need to replace ‘final_section_text_background’ with your own custom field name. Opacity value can be adjusted using $rgba = hex2rgba($color, 0.8); – replacing 0.8 with your own preferred opacity level.
<?php if( get_field('final_section_text_background') ): ?>
<?php /* Convert hexdec color string to rgb(a) string */
function hex2rgba($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 (strlen($color) == 6) {
$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
} elseif ( strlen( $color ) == 3 ) {
$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
} else {
return $default;
}
//Convert hexadec to rgb
$rgb = array_map('hexdec', $hex);
//Check if opacity is set(rgba or rgb)
if($opacity){
if(abs($opacity) > 1)
$opacity = 1.0;
$output = 'rgba('.implode(",",$rgb).','.$opacity.')';
} else {
$output = 'rgb('.implode(",",$rgb).')';
}
//Return rgb(a) color string
return $output;
}
/* Here's a usage example how to use this function for dynamicaly created CSS */
$setColor = get_field('final_section_text_background');
$color = $setColor;
$rgb = hex2rgba($color);
$rgba = hex2rgba($color, 0.8);
?>
.final-section .valign-middle {background:<?php echo $rgba ?>;}
<?php endif; ?>
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.