]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/webcolor.php
change LACONICA to STATUSNET
[quix0rs-gnu-social.git] / lib / webcolor.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Base class for deleting things
6  *
7  * PHP version 5
8  *
9  * LICENCE: This program is free software: you can redistribute it and/or modify
10  * it under the terms of the GNU Affero General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU Affero General Public License for more details.
18  *
19  * You should have received a copy of the GNU Affero General Public License
20  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21  *
22  * @category  Personal
23  * @package   StatusNet
24  * @author    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31      exit(1);
32 }
33
34 class WebColor {
35
36     // XXX: Maybe make getters and setters for r,g,b values and tuples,
37     // e.g.: to support this kinda CSS representation: rgb(255,0,0)
38     // http://www.w3.org/TR/CSS21/syndata.html#color-units
39
40     var $red   = 0;
41     var $green = 0;
42     var $blue  = 0;
43
44     /**
45      * Constructor
46      *
47      * @return nothing
48      */
49
50     function __construct($color = null)
51     {
52         if (isset($color)) {
53             $this->parseColor($color);
54         }
55     }
56
57     /**
58      * Parses input to and tries to determine whether the color
59      * is being specified via an integer or hex tuple and sets
60      * the RGB instance variables accordingly.
61      *
62      * XXX: Maybe support (r,g,b) style, and array?
63      *
64      * @param mixed $color
65      *
66      * @return nothing
67      */
68
69     function parseColor($color) {
70
71         if (is_numeric($color)) {
72             $this->setIntColor($color);
73         } else {
74
75             // XXX named colors
76
77             // XXX: probably should do even more validation
78
79             if (preg_match('/(#([0-9A-Fa-f]{3,6})\b)/u', $color) > 0) {
80                 $this->setHexColor($color);
81             } else {
82                 $errmsg = _('%s is not a valid color!');
83                 throw new WebColorException(sprintf($errmsg, $color));
84             }
85         }
86     }
87
88     /**
89      * @param string $name
90      *
91      * @return nothing
92      */
93
94     function setNamedColor($name)
95     {
96         // XXX Implement this
97     }
98
99
100     /**
101      * Sets the RGB color values from a a hex tuple
102      *
103      * @param string $hexcolor
104      *
105      * @return nothing
106      */
107
108     function setHexColor($hexcolor) {
109
110         if ($hexcolor[0] == '#') {
111             $hexcolor = substr($hexcolor, 1);
112         }
113
114         if (strlen($hexcolor) == 6) {
115             list($r, $g, $b) = array($hexcolor[0].$hexcolor[1],
116                                      $hexcolor[2].$hexcolor[3],
117                                      $hexcolor[4].$hexcolor[5]);
118         } elseif (strlen($hexcolor) == 3) {
119             list($r, $g, $b) = array($hexcolor[0].$hexcolor[0],
120                                      $hexcolor[1].$hexcolor[1],
121                                      $hexcolor[2].$hexcolor[2]);
122         } else {
123             $errmsg = _('%s is not a valid color! Use 3 or 6 hex chars.');
124             throw new WebColorException(sprintf($errmsg, $hexcolor));
125         }
126
127         $this->red   = hexdec($r);
128         $this->green = hexdec($g);
129         $this->blue  = hexdec($b);
130
131     }
132
133     /**
134      * Sets the RGB color values from a 24-bit integer
135      *
136      * @param int $intcolor
137      *
138      * @return nothing
139      */
140
141     function setIntColor($intcolor)
142     {
143         // We could do 32 bit and have an alpha channel because
144         // Sarven wants one real bad, but nah.
145
146         $this->red   = $intcolor >> 16;
147         $this->green = $intcolor >> 8 & 0xFF;
148         $this->blue  = $intcolor & 0xFF;
149
150     }
151
152     /**
153      * Returns a hex tuple of the RGB color useful for output in HTML
154      *
155      * @return string
156      */
157
158     function hexValue() {
159
160         $hexcolor  = (strlen(dechex($this->red)) < 2 ? '0' : '' ) .
161             dechex($this->red);
162         $hexcolor .= (strlen(dechex($this->green)) < 2 ? '0' : '') .
163             dechex($this->green);
164         $hexcolor .= (strlen(dechex($this->blue)) < 2 ? '0' : '') .
165             dechex($this->blue);
166
167         return strtoupper($hexcolor);
168
169     }
170
171     /**
172      * Returns a 24-bit packed integer representation of the RGB color
173      * for convenient storage in the DB
174      *
175      * XXX: probably could just use hexdec() instead
176      *
177      * @return int
178      */
179
180     function intValue()
181     {
182         $intcolor = 256 * 256 * $this->red + 256 * $this->green + $this->blue;
183         return $intcolor;
184     }
185
186 }
187
188 class WebColorException extends Exception
189 {
190 }
191
192 ?>