Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
98 - 1
<?php
2
# PHPlot / contrib / color_range : Test 1, make a picture
3
# $Id: color_range.test1.php 451 2009-12-09 03:45:49Z lbayuk $
4
# This creates a PNG file on output with a color gradient.
5
 
6
require_once 'color_range.php';
7
 
8
function usage()
9
{
10
   fwrite(STDERR, "Usage: color_range.test1.php color1 color2 number_of_colors
11
Each color is of the form rrggbb with 2 digit hex color components.
12
");
13
   exit(1);
14
}
15
 
16
# Split color "rrggbb" into separate components. Code is from PHPlot.
17
function rgb($color)
18
{
19
  return array(hexdec(substr($color, 1, 2)),
20
               hexdec(substr($color, 3, 2)),
21
               hexdec(substr($color, 5, 2)));
22
}
23
 
24
if ($_SERVER['argc'] != 4) usage();
25
 
26
$color1 = rgb($_SERVER['argv'][1]);
27
$color2 = rgb($_SERVER['argv'][2]);
28
$n_col = (int)$_SERVER['argv'][3];
29
if ($n_col < 2) usage();
30
 
31
# Build a color map from colors[0]=color1 to colors[$n_col-1]=color2.
32
$colors = color_range($color1, $color2, $n_col);
33
 
34
# Make a picture:
35
$w = 800;
36
$h = 800;
37
$im = imagecreate($w, $h);
38
$background = imagecolorresolve($im, 0, 0, 0);
39
for ($col = 0; $col < $n_col; $col++) {
40
    list($r, $g, $b) = $colors[$col];
41
    $colmap[$col] = imagecolorresolve($im, $r, $g, $b);
42
}
43
 
44
$margin = 20;
45
$bar_width = (int)(($w - 2 * $margin) / $n_col);
46
$x1 = $margin;
47
$x2 = $x1 + $bar_width;
48
$y1 = $margin;
49
$y2 = $h - $margin;
50
for ($col = 0; $col < $n_col; $col++) {
51
    imagefilledrectangle($im, $x1, $y1, $x2, $y2, $colmap[$col]);
52
    $x1 = $x2;
53
    $x2 += $bar_width;
54
}
55
imagepng($im);