ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Values between Colors
Posted by John Broussard on Jun-22-2015 07:57
I have two colors in php

$start = 0xff0000;
$end = 0x0000ff;

I want to find the 5 colors between these two colors in php
5 is actually an input x and I want to find the x colors between these two

I can solve this problem if it is written this way

$start= "#ff0000";
$end = "#0000ff";
        $steps = 5;
        list_colors($start, $end, $steps)

but have not found the hex format solution

Can you show a way ??

Or equivalently

Is there a way to convert a php variable that is "#ff0000" to
a php variable that is 0xff0000 ?

function list_colors($start, $end, $steps)
{
    $ret = array();

    $start_r = hexdec(substr($start, 1, 2));
    $start_g = hexdec(substr($start, 3, 2));
    $start_b = hexdec(substr($start, 5, 2));
    $end_r = hexdec(substr($end, 1, 2));
    $end_g = hexdec(substr($end, 3, 2));
    $end_b = hexdec(substr($end, 5, 2));

    $shift_r = ($end_r - $start_r) / ($steps-1);
    $shift_g = ($end_g - $start_g) / ($steps-1);
    $shift_b = ($end_b - $start_b) / ($steps-1);
    for ($i = 0; $i < $steps; $i++) {
$dog = round($start_r + $i * $shift_r);
$dogr = dechex($dog);
if( $dog<16 ) { $dogr = "0" . $dogr; }
$dog = round($start_g + $i * $shift_g);
$dogg = dechex($dog);
if( $dog<16 ) { $dogg = "0" . $dogg; }
$dog = round($start_b + $i * $shift_b);
$dogb = dechex($dog);
if( $dog<16 ) { $dogb = "0" . $dogb; }
              $ret[$i] = "#" . $dogr . $dogg . $dogb;
    }

    return $ret;
}

  Re: Values between Colors
Posted by Peter Kwan on Jun-22-2015 19:15
Hi John,

According to PHP documentation, you can use hexdec to convert from a text string
representing hex digits to a number. For example:

$aaa = "#ff0000";
$bbb = hexdec($aaa);

if ($bbb == 0xff0000)
   echo "bbb is equal to 0xff0000";
else
   echo "bbb is not equal to 0xff0000";

In the above code, it should echo "bbb is equal to 0xff0000";

Hope this can help.

Regards
Peter Kwan

  Re: Values between Colors
Posted by John Broussard on Jun-24-2015 21:20
Thanks

Discovered that I had to put $ret
in the subroutine in the call list, &$ret,
to get it to return hex values in the array correctly