Exemplo n.º 1
0
function rgb_allocate($image, $color)
{
    list($r, $g, $b) = rgb_color($color);
    $index = imagecolorexact($image, $r, $g, $b);
    if ($index == -1) {
        return imagecolorallocate($image, $r, $g, $b);
    } else {
        return $index;
    }
}
Exemplo n.º 2
0
Arquivo: rgb.php Projeto: loopzy/my
function rgb_gradient_color($from, $to, $numcols)
{
    $fcol = rgb_color($from);
    $tcol = rgb_color($to);
    $rfactor = ($tcol[0] - $fcol[0]) / $numcols;
    $gfactor = ($tcol[1] - $fcol[1]) / $numcols;
    $bfactor = ($tcol[2] - $fcol[2]) / $numcols;
    for ($i = 0; $i < $numcols; $i++) {
        $rnum = floor($fcol[0] + $i * $rfactor);
        $gnum = floor($fcol[1] + $i * $gfactor);
        $bnum = floor($fcol[2] + $i * $bfactor);
        $colors[$i] = sprintf("#%02x%02x%02x", $rnum, $gnum, $bnum);
    }
    return $colors;
}
Exemplo n.º 3
0
 function stroke($im, $xmin, $xmax, $ymin, $ymax, $xoff, $yoff, $width, $height)
 {
     $color = rgb_allocate($im, $this->color);
     $style = $this->style;
     $ycoords = $this->coords[0];
     $end = sizeof($ycoords);
     if (!strcmp($style, "points")) {
         $style = 1;
     } elseif (!strcmp($style, "lines")) {
         $style = 2;
     } elseif (!strcmp($style, "impulse")) {
         $style = 3;
     } elseif (!strcmp($style, "circle")) {
         $style = 4;
     } elseif (!strcmp($style, "cross")) {
         $style = 5;
     } elseif (!strcmp($style, "fill")) {
         $style = 6;
         $this->dimension = 1;
     } elseif (!strcmp($style, "square")) {
         $style = 7;
     } elseif (!strcmp($style, "gradient")) {
         # Calculate the gradient.
         $style = 8;
         $gradient_style = $this->gradient_param & 1;
         $gradient_updown = $this->gradient_param & 2;
         $gradient_direction = $this->gradient_param & 4;
         if ($gradient_direction == 0) {
             $fcol = rgb_color($this->color);
             $tcol = rgb_color($this->to_color);
         } else {
             $tcol = rgb_color($this->color);
             $fcol = rgb_color($this->to_color);
         }
         # We use at most 220 different colors.
         $numcols = 110;
         $rfactor = ($tcol[0] - $fcol[0]) / $numcols;
         $gfactor = ($tcol[1] - $fcol[1]) / $numcols;
         $bfactor = ($tcol[2] - $fcol[2]) / $numcols;
         $h = $height + 2;
         $col_factor = $numcols / $h;
         $prev = -1;
         for ($i = 0; $i < $h; $i++) {
             $num = floor($col_factor * ($h - $i));
             $rnum = floor($fcol[0] + $num * $rfactor);
             $gnum = floor($fcol[1] + $num * $gfactor);
             $bnum = floor($fcol[2] + $num * $bfactor);
             if ($num == $prev) {
                 $colors[$i] = $col;
             } else {
                 $col = rgb_allocate($im, sprintf("#%02x%02x%02x", $rnum, $gnum, $bnum));
                 $colors[$i] = $col;
             }
             $prev = $num;
         }
     }
     for ($i = 0; $i < $end; $i++) {
         $y = $ycoords[$i];
         if (!is_string($y) || strcmp($y, "noplot")) {
             if ($this->dimension == 1) {
                 $x = $i;
             } else {
                 $x = $this->coords[1][$i];
             }
             $xt = $xoff + ($x - $xmin) / ($xmax - $xmin) * $width;
             $yt = $yoff + $height - ($y * 1.0 - $ymin) / ($ymax - $ymin) * $height;
             if (!isset($pxt)) {
                 $pxt = $xt;
             }
             if (!isset($pyt)) {
                 $pyt = $yt;
             }
             if ($style == 1) {
                 imageline($im, $xt, $yt, $xt, $yt, $color);
             } elseif ($style == 2) {
                 imageline($im, $pxt, $pyt, $xt, $yt, $color);
             } elseif ($style == 3) {
                 imageline($im, $xt, $yoff + $height, $xt, $yt, $color);
             } elseif ($style == 4) {
                 imagearc($im, $xt, $yt, 10, 10, 0, 360, $color);
             } elseif ($style == 5) {
                 imageline($im, $xt - 5, $yt - 5, $xt + 5, $yt + 5, $color);
                 imageline($im, $xt + 5, $yt - 5, $xt - 5, $yt + 5, $color);
             } elseif ($style == 6) {
                 if (!isset($poyt)) {
                     $poyt = $oyt;
                 }
                 $oyt = $yoff + $height - ($this->coords[1][$i] * 1.0 - $ymin) / ($ymax - $ymin) * $height;
                 for ($j = $pxt; $j <= $xt; $j++) {
                     imageline($im, $j, $oyt, $j, $yt, $color);
                 }
                 $poyt = $oyt;
             } elseif ($style == 7) {
                 imageline($im, $pxt, $pyt, $pxt, $yt, $color);
                 imageline($im, $pxt, $yt, $xt, $yt, $color);
             } elseif ($style == 8) {
                 // color-impulse
                 // We plot down from the value to the bottom of the chart.
                 // There might be several pixels width of stuff to be plotted,
                 // so we first calculate the gradient of the top of the chart
                 // between the two points.  So the top of the "color-impulse"
                 // chart will resemble the "lines" chart, not the "square"
                 // chart.
                 if ($xt == $pxt) {
                     $b = 0;
                 } else {
                     $b = ($yt - $pyt) / ($xt - $pxt);
                 }
                 $a = $yt - $b * $xt;
                 for ($x = $pxt; $x <= $xt; $x++) {
                     $firsty = (int) round($a + $b * $x);
                     if ($gradient_updown == 0) {
                         for ($y = (int) round($a + $b * $x); $y < $yoff + $height; $y++) {
                             if ($gradient_style == 1) {
                                 imagesetpixel($im, $x, $y, $colors[$y - $firsty]);
                             } else {
                                 imagesetpixel($im, $x, $y, $colors[$y - $yoff]);
                             }
                         }
                     } else {
                         for ($y = (int) round($a + $b * $x); $y > $yoff; $y--) {
                             if ($gradient_style == 1) {
                                 imagesetpixel($im, $x, $y, $colors[$firsty - $y]);
                             } else {
                                 imagesetpixel($im, $x, $y, $colors[$y - $yoff]);
                             }
                         }
                     }
                 }
             }
             $pxt = $xt;
             $pyt = $yt;
         }
     }
     return $color;
 }
Exemplo n.º 4
0
 function stroke($im, $xmin, $xmax, $ymin, $ymax, $xoff, $yoff, $width, $height, &$chart)
 {
     $color = rgb_allocate($im, $this->color);
     $style = $this->style;
     $param = $this->param;
     $ycoords = $this->coords[0];
     $end = sizeof($ycoords);
     if (!strcmp($style, "points")) {
         $style = 1;
     } elseif (!strcmp($style, "lines")) {
         $style = 2;
     } elseif (!strcmp($style, "impulse")) {
         $style = 3;
     } elseif (!strcmp($style, "circle")) {
         $style = 4;
         if ($param) {
             if (is_array($param)) {
                 $circle_size = $param[0];
                 $position_style = $param[1];
             } else {
                 $circle_size = $param;
             }
         } else {
             $circle_size = 10;
         }
     } elseif (!strcmp($style, "cross")) {
         $style = 5;
         if ($param) {
             $cross_size = $param / 2;
         } else {
             $cross_size = 5;
         }
     } elseif (!strcmp($style, "fill")) {
         $style = 6;
         $this->dimension = 1;
     } elseif (!strcmp($style, "square")) {
         $style = 7;
     } elseif (!strcmp($style, "triangle")) {
         if ($this->to_color) {
             $dcolor = $this->to_color;
         } else {
             $dcolor = $this->color;
         }
         $dcolor = rgb_allocate($im, $dcolor);
         $style = 9;
     } elseif (!strcmp($style, "box")) {
         if ($this->to_color) {
             $to_color = $this->to_color;
         } else {
             $to_color = $this->color;
         }
         $tcolors = rgb_allocate_colors($im, rgb_gradient_color($this->color, $to_color, 3));
         $style = 10;
     } elseif (!strcmp($style, "gradient") || !strcmp($style, "fillgradient")) {
         // Calculate the gradient.
         if (!strcmp($style, "gradient")) {
             $style = 8;
         } else {
             $style = 12;
             $this->dimension = 1;
         }
         $gradient_style = $this->param & 1;
         $gradient_updown = $this->param & 2;
         $gradient_direction = $this->param & 4;
         $gradient_horizontal = $this->param & 8;
         if ($gradient_direction == 0) {
             $fcol = rgb_color($this->color);
             $tcol = rgb_color($this->to_color);
         } else {
             $tcol = rgb_color($this->color);
             $fcol = rgb_color($this->to_color);
         }
         // We use at most 220 different colors.
         $numcols = 110;
         $rfactor = ($tcol[0] - $fcol[0]) / $numcols;
         $gfactor = ($tcol[1] - $fcol[1]) / $numcols;
         $bfactor = ($tcol[2] - $fcol[2]) / $numcols;
         if ($gradient_horizontal) {
             $h = $width + 2;
         } else {
             $h = $height + 2;
         }
         $col_factor = $numcols / $h;
         $prev = -1;
         for ($i = 0; $i < $h; $i++) {
             $num = floor($col_factor * ($h - $i));
             $rnum = floor($fcol[0] + $num * $rfactor);
             $gnum = floor($fcol[1] + $num * $gfactor);
             $bnum = floor($fcol[2] + $num * $bfactor);
             if ($num == $prev) {
                 $colors[$i] = $col;
             } else {
                 $col = rgb_allocate($im, sprintf("#%02x%02x%02x", $rnum, $gnum, $bnum));
                 $colors[$i] = $col;
             }
             $prev = $num;
         }
     } elseif (!strcmp($style, "text")) {
         $style = 11;
         $textnum = 0;
         $position_style = $param;
         $circle_size = 10;
     }
     if ($end == 1 && $position_style == "last") {
         $y = $ycoords[0];
         $xt = $xoff + $width;
         $yt = $yoff + $height - ($y * 1.0 - $ymin) / ($ymax - $ymin) * $height;
         if ($style == 11) {
             imagepstext($im, $this->texts[0], $chart->font, $chart->font_size, $chart->allocate_color("red"), $chart->allocate_color("white"), (int) ($xt - $chart->real_string_pixels($this->texts[0])), (int) $yt, 0, 0, 0, 16);
         } else {
             imagearc($im, $xt, $yt, $circle_size, $circle_size, 0, 360, $color);
         }
         return $color;
     }
     for ($i = 0; $i < $end; $i++) {
         $y = $ycoords[$i];
         if (!is_string($y) || strcmp($y, "noplot")) {
             if ($this->dimension == 1) {
                 $x = $i;
             } else {
                 $x = $this->coords[1][$i];
             }
             $xt = $xoff + ($x - $xmin) / ($xmax - $xmin) * $width;
             $yt = $yoff + $height - ($y * 1.0 - $ymin) / ($ymax - $ymin) * $height;
             if (!isset($pxt)) {
                 $pxt = $xt;
             }
             if (!isset($pyt)) {
                 $pyt = $yt;
             }
             if ($style == 1) {
                 imageline($im, $xt, $yt, $xt, $yt, $color);
             } elseif ($style == 2) {
                 imageline($im, $pxt, $pyt, $xt, $yt, $color);
             } elseif ($style == 3) {
                 imageline($im, $xt, $yoff + $height, $xt, $yt, $color);
             } elseif ($style == 4) {
                 imagearc($im, $xt, $yt, $circle_size, $circle_size, 0, 360, $color);
             } elseif ($style == 5) {
                 imageline($im, $xt - $cross_size, $yt - $cross_size, $xt + $cross_size, $yt + $cross_size, $color);
                 imageline($im, $xt + $cross_size, $yt - $cross_size, $xt - $cross_size, $yt + $cross_size, $color);
             } elseif ($style == 6) {
                 // Fill
                 if (!isset($poyt)) {
                     $poyt = $oyt;
                 }
                 $oyt = $yoff + $height - ($this->coords[1][$i] * 1.0 - $ymin) / ($ymax - $ymin) * $height;
                 for ($j = $pxt; $j <= $xt; $j++) {
                     imageline($im, $j, $oyt, $j, $yt, $color);
                 }
                 $poyt = $oyt;
             } elseif ($style == 7) {
                 imageline($im, $pxt, $pyt, $pxt, $yt, $color);
                 imageline($im, $pxt, $yt, $xt, $yt, $color);
             } elseif ($style == 8) {
                 // gradient
                 // We plot down from the value to the bottom of the chart.
                 // There might be several pixels width of stuff to be plotted,
                 // so we first calculate the gradient of the top of the chart
                 // between the two points.  So the top of the "gradient"
                 // chart will resemble the "lines" chart, not the "square"
                 // chart.
                 if ($xt == $pxt) {
                     $b = 0;
                 } else {
                     $b = ($yt - $pyt) / ($xt - $pxt);
                 }
                 $a = $yt - $b * $xt;
                 for ($x = $pxt; $x <= $xt; $x++) {
                     $firsty = $a + $b * $x;
                     if ($gradient_updown == 0) {
                         for ($y = $a + $b * $x; $y < $yoff + $height; $y++) {
                             if ($gradient_style && !$gradient_horizontal) {
                                 $coff = $y - $firsty;
                             } elseif (!$gradient_style && !$gradient_horizontal) {
                                 $coff = $y - $yoff;
                             } elseif ($gradient_style && $gradient_horizontal) {
                                 $coff = $x - $firstx;
                             } elseif (!$gradient_style && $gradient_horizontal) {
                                 $coff = $x - $xoff;
                             }
                             imagesetpixel($im, $x, $y, $colors[$coff]);
                         }
                     } else {
                         for ($y = $a + $b * $x; $y > $yoff; $y--) {
                             if ($gradient_style && !$gradient_horizontal) {
                                 $coff = $firsty - $y;
                             } elseif (!$gradient_style && !$gradient_horizontal) {
                                 $coff = $y - $yoff;
                             } elseif ($gradient_style && $gradient_horizontal) {
                                 $coff = $firstx - $x;
                             } elseif (!$gradient_style && $gradient_horizontal) {
                                 $coff = $x - $xoff;
                             }
                             imagesetpixel($im, $x, $y, $colors[$coff]);
                         }
                     }
                 }
             } elseif ($style == 12) {
                 // fillgradient
                 if (!isset($poyt)) {
                     $poyt = $oyt;
                 }
                 $oyt = $yoff + $height - ($this->coords[1][$i] * 1.0 - $ymin) / ($ymax - $ymin) * $height;
                 for ($x = $pxt; $x <= $xt; $x++) {
                     if ($oyt < $yt) {
                         $miny = $oyt;
                         $maxy = $yt;
                     } else {
                         $miny = $yt;
                         $maxy = $oyt;
                     }
                     $firsty = $oyt;
                     if ($gradient_updown == 0) {
                         for ($y = $miny; $y < $maxy; $y++) {
                             if ($gradient_style == 1) {
                                 imagesetpixel($im, $x, $y, $colors[$y - $firsty]);
                             } else {
                                 imagesetpixel($im, $x, $y, $colors[$y - $yoff]);
                             }
                         }
                     } else {
                         for ($y = $maxy; $y > $miny; $y--) {
                             if ($gradient_style == 1) {
                                 imagesetpixel($im, $x, $y, $colors[$firsty - $y]);
                             } else {
                                 imagesetpixel($im, $x, $y, $colors[$y - $yoff]);
                             }
                         }
                     }
                 }
                 $poyt = $oyt;
             } elseif ($style == 9) {
                 // Triangle
                 imageline($im, $xt - 3, $yt + 3, $xt + 3, $yt + 3, $dcolor);
                 imageline($im, $xt - 3, $yt + 2, $xt + 3, $yt + 2, $color);
                 imageline($im, $xt - 2, $yt + 1, $xt + 2, $yt + 1, $color);
                 imageline($im, $xt - 2, $yt, $xt + 2, $yt, $color);
                 imageline($im, $xt - 1, $yt - 1, $xt + 1, $yt - 1, $color);
                 imageline($im, $xt - 1, $yt - 2, $xt + 1, $yt - 2, $color);
                 imagesetpixel($im, $xt, $yt - 3, $color);
                 imagesetpixel($im, $xt, $yt - 4, $color);
                 imageline($im, $xt + 4, $yt + 3, $xt + 1, $yt - 4, $dcolor);
             } elseif ($style == 10) {
                 // Box
                 imageline($im, $xt - 2, $yt - 2, $xt + 2, $yt - 2, $tcolors[0]);
                 imageline($im, $xt - 2, $yt - 1, $xt - 2, $yt + 2, $tcolors[0]);
                 imageline($im, $xt - 1, $yt - 1, $xt + 1, $yt - 1, $tcolors[1]);
                 imageline($im, $xt - 1, $yt, $xt + 1, $yt, $tcolors[1]);
                 imageline($im, $xt - 1, $yt + 1, $xt + 1, $yt + 1, $tcolors[1]);
                 imageline($im, $xt - 1, $yt + 2, $xt + 2, $yt + 2, $tcolors[2]);
                 imageline($im, $xt + 2, $yt - 1, $xt + 2, $yt + 1, $tcolors[2]);
             } elseif ($style == 11) {
                 imagestring($im, $this->font, $xt, $yt, $this->texts[$textnum++], $color);
             }
             $pxt = $xt;
             $pyt = $yt;
         }
     }
     return $color;
 }
Exemplo n.º 5
0
function shade($color1, $color2, $proportion)
{
    $color1 = rgb_color($color1);
    $color2 = rgb_color($color2);
    return array((int) ($color1[0] * $proportion + $color2[0] * (1.0 - $proportion)), (int) ($color1[1] * $proportion + $color2[1] * (1.0 - $proportion)), (int) ($color1[2] * $proportion + $color2[2] * (1.0 - $proportion)));
}