]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/webcolor.php
Merged stuff from upstream/master
[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') && !defined('LACONICA')) {
31      exit(1);
32 }
33
34 class WebColor {
35     // XXX: Maybe make getters and setters for r,g,b values and tuples,
36     // e.g.: to support this kinda CSS representation: rgb(255,0,0)
37     // http://www.w3.org/TR/CSS21/syndata.html#color-units
38
39     var $red   = 0;
40     var $green = 0;
41     var $blue  = 0;
42
43     /**
44      * Constructor
45      *
46      * @return nothing
47      */
48     function __construct($color = null)
49     {
50         if (isset($color)) {
51             $this->parseColor($color);
52         }
53     }
54
55     /**
56      * Parses input to and tries to determine whether the color
57      * is being specified via an integer or hex tuple and sets
58      * the RGB instance variables accordingly.
59      *
60      * XXX: Maybe support (r,g,b) style, and array?
61      *
62      * @param mixed $color
63      *
64      * @return nothing
65      */
66     function parseColor($color) {
67
68         if (is_numeric($color)) {
69             $this->setIntColor($color);
70         } else {
71
72             // XXX named colors
73
74             // XXX: probably should do even more validation
75
76             if (preg_match('/(#([0-9A-Fa-f]{3,6})\b)/u', $color) > 0) {
77                 $this->setHexColor($color);
78             } else {
79                 // TRANS: Web color exception thrown when a hexadecimal color code does not validate.
80                 // TRANS: %s is the provided (invalid) color code.
81                 $errmsg = _('%s is not a valid color! Use 3 or 6 hex characters.');
82                 throw new WebColorException(sprintf($errmsg, $color));
83             }
84         }
85     }
86
87     /**
88      * @param string $name
89      *
90      * @return nothing
91      */
92     function setNamedColor($name)
93     {
94         // XXX Implement this
95     }
96
97     /**
98      * Sets the RGB color values from a a hex tuple
99      *
100      * @param string $hexcolor
101      *
102      * @return nothing
103      */
104     function setHexColor($hexcolor) {
105
106         if ($hexcolor[0] == '#') {
107             $hexcolor = substr($hexcolor, 1);
108         }
109
110         if (strlen($hexcolor) == 6) {
111             list($r, $g, $b) = array($hexcolor[0].$hexcolor[1],
112                                      $hexcolor[2].$hexcolor[3],
113                                      $hexcolor[4].$hexcolor[5]);
114         } elseif (strlen($hexcolor) == 3) {
115             list($r, $g, $b) = array($hexcolor[0].$hexcolor[0],
116                                      $hexcolor[1].$hexcolor[1],
117                                      $hexcolor[2].$hexcolor[2]);
118         } else {
119             // TRANS: Web color exception thrown when a hexadecimal color code does not validate.
120             // TRANS: %s is the provided (invalid) color code.
121             $errmsg = _('%s is not a valid color! Use 3 or 6 hex characters.');
122             throw new WebColorException(sprintf($errmsg, $hexcolor));
123         }
124
125         $this->red   = hexdec($r);
126         $this->green = hexdec($g);
127         $this->blue  = hexdec($b);
128
129     }
130
131     /**
132      * Sets the RGB color values from a 24-bit integer
133      *
134      * @param int $intcolor
135      *
136      * @return nothing
137      */
138     function setIntColor($intcolor)
139     {
140         // We could do 32 bit and have an alpha channel because
141         // Sarven wants one real bad, but nah.
142
143         $this->red   = $intcolor >> 16;
144         $this->green = $intcolor >> 8 & 0xFF;
145         $this->blue  = $intcolor & 0xFF;
146
147     }
148
149     /**
150      * Returns a hex tuple of the RGB color useful for output in HTML
151      *
152      * @return string
153      */
154     function hexValue() {
155
156         $hexcolor  = (strlen(dechex($this->red)) < 2 ? '0' : '' ) .
157             dechex($this->red);
158         $hexcolor .= (strlen(dechex($this->green)) < 2 ? '0' : '') .
159             dechex($this->green);
160         $hexcolor .= (strlen(dechex($this->blue)) < 2 ? '0' : '') .
161             dechex($this->blue);
162
163         return strtoupper($hexcolor);
164     }
165
166     /**
167      * Returns a 24-bit packed integer representation of the RGB color
168      * for convenient storage in the DB
169      *
170      * XXX: probably could just use hexdec() instead
171      *
172      * @return int
173      */
174     function intValue()
175     {
176         $intcolor = 256 * 256 * $this->red + 256 * $this->green + $this->blue;
177         return $intcolor;
178     }
179
180 }
181
182 class WebColorException extends Exception
183 {
184 }