2 /* Libchart - PHP chart library
3 * Copyright (C) 2005-2008 Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
35 * @param integer red [0..255]
36 * @param integer green [0..255]
37 * @param integer blue [0..255]
38 * @param integer alpha [0..255]
40 public function Color($red, $green, $blue, $alpha = 0) {
41 $this->red = (int) $red;
42 $this->green = (int) $green;
43 $this->blue = (int) $blue;
44 $this->alpha = (int) round($alpha * 127.0 / 255);
46 $this->gdColor = null;
52 * @param $img GD image resource
54 public function getColor($img) {
55 // Checks if color has already been allocated
56 if (!$this->gdColor) {
57 if ($this->alpha == 0 || !function_exists('imagecolorallocatealpha')) {
58 $this->gdColor = imagecolorallocate($img, $this->red, $this->green, $this->blue);
60 $this->gdColor = imagecolorallocatealpha($img, $this->red, $this->green, $this->blue, $this->alpha);
65 return $this->gdColor;
69 * Clip a color component in the interval [0..255]
71 * @param integer Component
72 * @return Clipped component
74 public function clip($component) {
77 } else if ($component > 255) {
85 * Return a new color, which is a shadow of this one.
87 * @param double Multiplication factor
88 * @return Shadow color
90 public function getShadowColor($shadowFactor) {
91 $red = $this->clip($this->red * $shadowFactor);
92 $green = $this->clip($this->green * $shadowFactor);
93 $blue = $this->clip($this->blue * $shadowFactor);
94 $shadowColor = new Color($red, $green, $blue);