Initial repo created
[timetracker.git] / WEB-INF / lib / libchart / classes / view / color / Color.php
1 <?php
2         /* Libchart - PHP chart library
3          * Copyright (C) 2005-2008 Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
4          * 
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.
9          * 
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.
14          *
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/>.
17          * 
18          */
19         
20         /**
21          * Color.
22          *
23          * @author Jean-Marc Trémeaux (jm.tremeaux at gmail.com)
24          */
25         class Color {
26                 private $red;
27                 private $green;
28                 private $blue;
29                 private $alpha;
30                 private $gdColor;
31         
32                 /**
33                  * Creates a new color
34                  *
35                  * @param integer red [0..255]
36                  * @param integer green [0..255]
37                  * @param integer blue [0..255]
38                  * @param integer alpha [0..255]
39                  */
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);
45                         
46                         $this->gdColor = null;
47                 }
48                 
49                 /**
50                  * Get GD color.
51                  *
52                  * @param $img GD image resource
53                  */
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);
59                                 } else {
60                                         $this->gdColor = imagecolorallocatealpha($img, $this->red, $this->green, $this->blue, $this->alpha);
61                                 }
62                         }
63                         
64                         // Returns GD color
65                         return $this->gdColor;
66                 }
67                 
68                 /**
69                  * Clip a color component in the interval [0..255]
70                  *
71                  * @param integer Component
72                  * @return Clipped component
73                  */
74                 public function clip($component) {
75                         if ($component < 0) {
76                                 $component = 0;
77                         } else if ($component > 255) {
78                                 $component = 255;
79                         }
80                         
81                         return $component;
82                 }
83                 
84                 /**
85                  * Return a new color, which is a shadow of this one.
86                  *
87                  * @param double Multiplication factor
88                  * @return Shadow color
89                  */
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);
95                         
96                         return $shadowColor;
97                 }
98         }
99 ?>