I play a lot with the code and I feel that I had the results that I want.
add_action( 'manage_pages_custom_column' , 'background_add_custom_column_data', 10, 2 );
add_action( 'manage_posts_custom_column' , 'background_add_custom_column_data', 10, 2 );
function background_add_custom_column_data( $column, $post_id ) {
switch ( $column ) {
case 'colors' :
if( have_rows('colors') ):
while ( have_rows('colors') ) : the_row(); ?>
<div style="background-color:<?php the_sub_field('hex_color');?>;">
<div style="width:80px;height:80px;"></div>
<?php
endwhile;
endif;
break;
}
}
@jarvis thank you so much. It seems that it works.
I want to convert the HEX value to HSL. From the following function, when I passed directly the value fff000
, then the outcome hsl value is correct, but when I passed #fff000
then the outcome hsl value is incorrect.
The problem came from #
. I wonder how I remove the #
from the get_sub_field. As an instance <?php $hex_color = get_sub_field('hex_color');?>
<?php
function hexToHsl($hex){
$red = hexdec(substr($hex, 0, 2)) / 255;
$green = hexdec(substr($hex, 2, 2)) / 255;
$blue = hexdec(substr($hex, 4, 2)) / 255;
$cmin = min($red, $green, $blue);
$cmax = max($red, $green, $blue);
$delta = $cmax - $cmin;
if ($delta == 0) {
$hue = 0;
} elseif ($cmax === $red) {
$hue = (($green - $blue) / $delta);
} elseif ($cmax === $green) {
$hue = ($blue - $red) / $delta + 2;
} else {
$hue = ($red - $green) / $delta + 4;
}
$hue = round($hue * 60);
if ($hue < 0) {
$hue += 360;
}
$lightness = (($cmax + $cmin) / 2);
$saturation = $delta === 0 ? 0 : ($delta / (1 - abs(2 * $lightness - 1)));
if ($saturation < 0) {
$saturation += 1;
}
$lightness = round($lightness*100);
$saturation = round($saturation*100);
return "hsl(${hue}, ${saturation}%, ${lightness}%)";
//return array($hue, $saturation, $lightness);
}
$hex_color = 'fff000';
echo hexToHsl($hex_color);
@jarvis thank you for your reply. The pain point is the #
, if it’s included then the outcome value is wrong.
How exactly I can remove the #
from the hex value? Sorry but I’m confused. π Can you provide me a solution with full code?
Kind regards.
@jarvis returns hsl(0,0,0)
π
@hube2 also I tried this part of function :
function hexToHsl($hex){
$red = hexdec(substr($hex, 0, 2)) / 255;
$green = hexdec(substr($hex, 2, 2)) / 255;
$blue = hexdec(substr($hex, 4, 2)) / 255;
$cmin = min($red, $green, $blue);
$cmax = max($red, $green, $blue);
$delta = $cmax - $cmin;
if ($delta == 0) {
$hue = 0;
} elseif ($cmax === $red) {
$hue = (($green - $blue) / $delta);
} elseif ($cmax === $green) {
$hue = ($blue - $red) / $delta + 2;
} else {
$hue = ($red - $green) / $delta + 4;
}
$hue = round($hue * 60);
if ($hue < 0) {
$hue += 360;
}
$lightness = (($cmax + $cmin) / 2);
$saturation = $delta === 0 ? 0 : ($delta / (1 - abs(2 * $lightness - 1)));
if ($saturation < 0) {
$saturation += 1;
}
$lightness = round($lightness*100);
$saturation = round($saturation*100);
// return "hsl(${hue}, ${saturation}%, ${lightness}%)";
return array($hue, $saturation, $lightness);
}
I called as <?php echo hexToHsl($hex_color); ?>
and it returns me an Array
without any kind of result.
Every part of the solution is welcome.
Thanks.
define( 'WP_DEBUG', true );
I take a message :
Invalid characters passed for attempted conversion, these have been ignored in line
$red = hexdec(substr($hex, 0, 2)) / 255;
Let’s say that turn of the ‘WP_DEBUG'
then it gave the same results. hsl(137, 99%, 42%)
instead of hsl(258, 83%, 45%)
I believe that the function gave me wrong results.. π
@hube2 I am back after a while.
I believed that code worked, but it’s something missing right here..
function hexToHsl($hex){
$red = hexdec(substr($hex, 0, 2)) / 255;
$green = hexdec(substr($hex, 2, 2)) / 255;
$blue = hexdec(substr($hex, 4, 2)) / 255;
$cmin = min($red, $green, $blue);
$cmax = max($red, $green, $blue);
$delta = $cmax - $cmin;
if ($delta === 0) {
$hue = 0;
} elseif ($cmax === $red) {
$hue = (($green - $blue) / $delta) % 6;
} elseif ($cmax === $green) {
$hue = ($blue - $red) / $delta + 2;
} else {
$hue = ($red - $green) / $delta + 4;
}
$hue = round($hue * 60);
if ($hue < 0) {
$hue += 360;
}
$lightness = (($cmax + $cmin) / 2) * 100;
$saturation = $delta === 0 ? 0 : ($delta / (1 - abs(2 * $lightness - 1))) * 100;
if ($saturation < 0) {
$saturation += 100;
}
$lightness = round($lightness);
$saturation = round($saturation);
return "hsl(${hue}, ${saturation}%, ${lightness}%)";
}
usage
<?php echo '',print_r(hexToHsl($hex_color)).'';?>
Returns : hsl(137, 99%, 42%)1
but the correct is hsl(258, 83%, 45%)
. What exactly I’m doing wrong? Any kind of help is much appreciated.
Sincerely,
Mike
Thank you both for the help!
@jarvis good point.
It returns me..
Array ( [0] => 208.125 [1] => 84.210526315789 [2] => 74.509803921569 )
I wonder how I can have the format of : hsv(208,84,75)
Kind regards,
Mike
Hello @jarvis thank you for your reply. When I add these parts of codes,then I have the following results.
Basically, I want to convert HEX to HSV in a normal format, as an instance HEX = #1e73be / hsv(208,84,75)
Thanks.
No problem at all.
I use this part of the code to convert HEX to RGB.
function getsub_acf_rgb($field, $id) {
if ( function_exists('get_sub_field') ) {
$hexx = get_sub_field($field, $id);
// extra sanity check that we're dealing with the hex flatuicolorpicker
// in #NNNNNN format that ACF/WordPress/Iris colorpicker returns
if (strlen($hexx) == 7 && substr($hexx,0,1) == '#') {
$rr = hexdec(substr($hexx,1,2));
$gg = hexdec(substr($hexx,3,2));
$bb = hexdec(substr($hexx,5,2));
return $rr . ', ' . $gg . ', ' . $bb;
}
else {
// provide a default in an emergency
return "128,128,128" ;
}
}
}
function acsf_rgb($field, $id) {
echo getsub_acf_rgb($field, $id);
}
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;
}
but I wonder how I can pass the value of RGB to HSV
referral link https://stackoverflow.com/questions/1773698/rgb-to-hsv-in-php?answertab=votes#tab-top
Thanks for the anwser. I have already found this, but I wonder how I can pass the attribute of RGB since I want to convert it from the HEX color value.
This is my code. 2276 is the block ID. When I make the changes on the block from the first page (homepage), then the changes are not passed to the second page (services). I wonder what I am doing wrong.
<?php
$block_ID=2276;
$services_header_title = get_field('services_header_title', $block_ID);
?>
<div class="col-lg-12">
<h2><?php echo $services_header_title; ?></h2>
</div>
<?php if( have_rows('services') ): ?>
<?php while( have_rows('services') ): the_row();
// Get sub field values.
$service_title = get_sub_field('service_title', $block_ID);
$service_content = get_sub_field('service_content', $block_ID);
?>
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
<h3><?php echo $service_title; ?></h3>
<p><?php echo $service_content; ?></p>
</div>
<?php endwhile; ?>
<?php endif; ?>
@John thank you for your help. In my case, I don’t think that I need the foreach ($cmyk as $i => $v)
, because returns the value a lot of times and I want this value, only one time. π
As an example the value: #060e5b returns me
c=0.93406593406593
m=0.84615384615385
y=0
k=164
But I want to return : cmyk(93,85,0,64)
Any idea? π
Regards
Basically when I choose a hex color from the color picker, I want to return this converted value in CMYK format π
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?
Thank you @John!
I have found this piece of code, but I wonder how can I adjust the ‘sub_field’ in my code.
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!
π€ Curious about the ACF user experience? So are we! Help guide the evolution of ACF by taking part in our first ever Annual Survey and guarantee youβre represented in the results. https://t.co/0cgr9ZFOJ5
— Advanced Custom Fields (@wp_acf) May 8, 2023
© 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.