function test_color_range($color1, $color2, $nsteps, $expected) { $info = 'From (' . implode(', ', $color1) . ') To (' . implode(', ', $color2) . ") with {$nsteps} steps"; $colors = color_range($color1, $color2, $nsteps); if ($colors == $expected) { echo "Pass: {$info}\n"; } else { echo "FAIL: {$info}\n" . print_r($colors, True) . "\n"; } if (($n = count($colors)) != $nsteps) { echo "FAIL: Bad count {$n} expecting {$nsteps}\n"; } }
# PHPlot / contrib / color_range : Example # $Id$ # This is a bar chart with a color gradient for the bars in each group. require_once 'phplot.php'; require_once 'color_range.php'; $bars_per_group = 10; $x_values = 4; mt_srand(1); $data = array(); for ($i = 0; $i < $x_values; $i++) { $row = array($i); for ($j = 0; $j < $bars_per_group; $j++) { $row[] = mt_rand(0, 100); } $data[] = $row; } $p = new PHPlot(800, 600); $p->SetTitle('Example - Bar Chart with gradient colors'); $p->SetDataType('text-data'); $p->SetDataValues($data); $p->SetPlotAreaWorld(0, 0, $x_values, 100); # This isn't necessary, as we do know how many data sets (bars_per_group): $n_data = count_data_sets($data, 'text-data'); # Make a gradient color map: $colors = color_range($p->SetRGBColor('SkyBlue'), $p->SetRGBColor('DarkGreen'), $n_data); $p->SetDataColors($colors); $p->SetXTickLabelPos('none'); $p->SetXTickPos('none'); $p->SetPlotType('bars'); $p->DrawGraph();
# Split color "rrggbb" into separate components. Code is from PHPlot. function rgb($color) { return array(hexdec(substr($color, 1, 2)), hexdec(substr($color, 3, 2)), hexdec(substr($color, 5, 2))); } if ($_SERVER['argc'] != 4) { usage(); } $color1 = rgb($_SERVER['argv'][1]); $color2 = rgb($_SERVER['argv'][2]); $n_col = (int) $_SERVER['argv'][3]; if ($n_col < 2) { usage(); } # Build a color map from colors[0]=color1 to colors[$n_col-1]=color2. $colors = color_range($color1, $color2, $n_col); # Make a picture: $w = 800; $h = 800; $im = imagecreate($w, $h); $background = imagecolorresolve($im, 0, 0, 0); for ($col = 0; $col < $n_col; $col++) { list($r, $g, $b) = $colors[$col]; $colmap[$col] = imagecolorresolve($im, $r, $g, $b); } $margin = 20; $bar_width = (int) (($w - 2 * $margin) / $n_col); $x1 = $margin; $x2 = $x1 + $bar_width; $y1 = $margin; $y2 = $h - $margin;