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