Home › Forums › Front-end Issues › Convert hexadecimal value to CMYK › Reply To: Convert hexadecimal value to CMYK
in function.php
:
function hex2rgb($hex) {
$color = str_replace('#','',$hex);
$rgb = array(
'r' => hexdec(substr($color,0,2)),
'g' => hexdec(substr($color,2,2)),
'b' => hexdec(substr($color,4,2)),
);
return $rgb;
}
function rgb2cmyk($var1,$g=0,$b=0) {
if (is_array($var1)) {
$r = $var1['r'];
$g = $var1['g'];
$b = $var1['b'];
} else {
$r = $var1;
}
$cyan = 255 - $r;
$magenta = 255 - $g;
$yellow = 255 - $b;
$black = min($cyan, $magenta, $yellow);
$cyan = @(($cyan - $black) / (255 - $black));
$magenta = @(($magenta - $black) / (255 - $black));
$yellow = @(($yellow - $black) / (255 - $black));
return array(
'c' => $cyan,
'm' => $magenta,
'y' => $yellow,
'k' => $black,
);
}
echo rgb2cmyk(hex2rgb(get_sub_field('color_hex')));
page template :
<span class="cmyk"><?php rgb2cmyk('color_hex', $post->ID);?></span>
And I have some errors: Array to string conversion. What I am doing wrong?
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!
🚀 ACF & ACF PRO 6.0.7 are now available.
— Advanced Custom Fields (@wp_acf) January 18, 2023
✨This release contains bug fixes and improvements while we continue to work on the next major release of ACF.https://t.co/wQgAOpwmUI
© 2023 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.