Home › Forums › Feature Requests › Colorpicker get darkness of the color › Reply To: Colorpicker get darkness of the color
I created a function based on a Stack overflow answer (can’t find the page anymore otherwise I would have linked to it).
function get_darker_or_light_class_based_on_hex ( $hex_color ) {
$hex = str_replace( '#', '', $hex_color );
$r = hexdec(substr($hex,0,2));
$g = hexdec(substr($hex,2,2));
$b = hexdec(substr($hex,4,2));
if($r + $g + $b < 450){
$class = 'dark-bg';
}else{
$class = 'light-bg';
};
return $class;
}
I had to change the 450
value to get it to work with my colors. And in my template part I use <?php echo get_darker_or_light_class_based_on_hex(get_sub_field('bg-color'); ?>
to get the right background class.
Hope someone finds this usefull.
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!
Are you ready to start building block themes with WordPress 5.9? ACF Blocks in ACF PRO 5.12 helps you bridge the gap. Learn how: https://t.co/ViL7VURG1M
— Advanced Custom Fields (@wp_acf) May 3, 2022
© 2022 Advanced Custom Fields.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.