All Good Things Must Come To An End

As you all know, I have been doing WordPress plugins and supporting it for the past 6 years. These 6 years of my life, I have been through my polytechnic education, my national service as well as my university education.

I just graduated from university in December 2009 and have been looking for full-time jobs. I am offered a full-time job and will be starting work on 1st February 2010.

I regret to say that I am NOT ABLE to provide support for my plugins anymore due to my full-time job commitment. I will leave this forum open and let the community help one another.

However, I WILL still update my plugins whenever I can and you still can report bugs to me via email and I will try to fix it.

Author Topic: Hex code of color between two colors  (Read 5298 times)

0 Members and 1 Guest are viewing this topic.

Offline Romik84

  • Jr. Member
  • **
  • Posts: 99
    • View Profile
Hex code of color between two colors
« on: 08 June 2009, 08:51 »
Hi again,

I've searched on the web but could find any solution for my problem. I would need a php script which would generate hex code of color between two colors.

For example I have two colors: #007ca5 and #005b79. The hex code between theme would be #018ab6.

Is possible to write a function which could print that average hex code? Thanks.

Offline imvain2

  • Expert
  • ****
  • Posts: 68
  • WP-DownloadManager Expert
    • View Profile
Re: Hex code of color between two colors
« Reply #1 on: 04 January 2010, 00:30 »
So, I was curious if I could do this and it is a rather easy process.

1st: You need to convert hex to rgb of both colors
2nd: Grab the average of the r,g and the b values of both colors
3rd: Convert the new rgb to next

Code: [Select]
//functions
function rgb2html($r, $g=-1, $b=-1)
{
    if (is_array($r) && sizeof($r) == 3)
        list($r, $g, $b) = $r;

    $r = intval($r); $g = intval($g);
    $b = intval($b);

    $r = dechex($r<0?0:($r>255?255:$r));
    $g = dechex($g<0?0:($g>255?255:$g));
    $b = dechex($b<0?0:($b>255?255:$b));

    $color = (strlen($r) < 2?'0':'').$r;
    $color .= (strlen($g) < 2?'0':'').$g;
    $color .= (strlen($b) < 2?'0':'').$b;
    return $color;
}

function average($input) {
return array_sum($input) / count($input);
}

function html2rgb($color)
{
    if ($color[0] == '#')
        $color = substr($color, 1);

    if (strlen($color) == 6)
        list($r, $g, $b) = array($color[0].$color[1],
                                 $color[2].$color[3],
                                 $color[4].$color[5]);
    elseif (strlen($color) == 3)
        list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
    else
        return false;

    $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);

    return array($r, $g, $b);
}

function avg_hex($hex_from,$hex_to){
$rgb_from = html2rgb($hex_from);
$rgb_to = html2rgb($hex_to);
$avg_r = average(array($rgb_from[0],$rgb_to[0]));
$avg_g = average(array($rgb_from[1],$rgb_to[1]));
$avg_b = average(array($rgb_from[2],$rgb_to[2]));
$hex_avg = rgb2html($avg_r, $avg_g, $avg_b);
return $hex_avg;
}
$hex_from = "007ca5";
$hex_to = "005b79";
$avg = avg_hex($hex_from, $hex_to);
echo "<div style=\"width:300px;height:50px;background-color:#$hex_from;\">Hex From</div>";
echo "<div style=\"width:300px;height:50px;background-color:#$avg;\">Hex Average</div>";
echo "<div style=\"width:300px;height:50px;background-color:#$hex_to;\">Hex To</div>";
//sample