summaryrefslogtreecommitdiff
path: root/lib/webcolor.php
blob: 7f264c67410f841322a0b23592c4c3c7e356b566 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php
/**
 * StatusNet, the distributed open-source microblogging tool
 *
 * Base class for deleting things
 *
 * PHP version 5
 *
 * LICENCE: This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @category  Personal
 * @package   StatusNet
 * @author    Zach Copley <zach@status.net>
 * @copyright 2009 StatusNet, Inc.
 * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
 * @link      http://status.net/
 */

if (!defined('STATUSNET') && !defined('LACONICA')) {
     exit(1);
}

class WebColor {
    // XXX: Maybe make getters and setters for r,g,b values and tuples,
    // e.g.: to support this kinda CSS representation: rgb(255,0,0)
    // http://www.w3.org/TR/CSS21/syndata.html#color-units

    var $red   = 0;
    var $green = 0;
    var $blue  = 0;

    /**
     * Constructor
     *
     * @return nothing
     */

    function __construct($color = null)
    {
        if (isset($color)) {
            $this->parseColor($color);
        }
    }

    /**
     * Parses input to and tries to determine whether the color
     * is being specified via an integer or hex tuple and sets
     * the RGB instance variables accordingly.
     *
     * XXX: Maybe support (r,g,b) style, and array?
     *
     * @param mixed $color
     *
     * @return nothing
     */
    function parseColor($color) {

        if (is_numeric($color)) {
            $this->setIntColor($color);
        } else {

            // XXX named colors

            // XXX: probably should do even more validation

            if (preg_match('/(#([0-9A-Fa-f]{3,6})\b)/u', $color) > 0) {
                $this->setHexColor($color);
            } else {
                $errmsg = _('%s is not a valid color!');
                throw new WebColorException(sprintf($errmsg, $color));
            }
        }
    }

    /**
     * @param string $name
     *
     * @return nothing
     */
    function setNamedColor($name)
    {
        // XXX Implement this
    }

    /**
     * Sets the RGB color values from a a hex tuple
     *
     * @param string $hexcolor
     *
     * @return nothing
     */
    function setHexColor($hexcolor) {

        if ($hexcolor[0] == '#') {
            $hexcolor = substr($hexcolor, 1);
        }

        if (strlen($hexcolor) == 6) {
            list($r, $g, $b) = array($hexcolor[0].$hexcolor[1],
                                     $hexcolor[2].$hexcolor[3],
                                     $hexcolor[4].$hexcolor[5]);
        } elseif (strlen($hexcolor) == 3) {
            list($r, $g, $b) = array($hexcolor[0].$hexcolor[0],
                                     $hexcolor[1].$hexcolor[1],
                                     $hexcolor[2].$hexcolor[2]);
        } else {
            // TRANS: Validation error for a web colour.
            // TRANS: %s is the provided (invalid) text for colour.
            $errmsg = _('%s is not a valid color! Use 3 or 6 hex characters.');
            throw new WebColorException(sprintf($errmsg, $hexcolor));
        }

        $this->red   = hexdec($r);
        $this->green = hexdec($g);
        $this->blue  = hexdec($b);

    }

    /**
     * Sets the RGB color values from a 24-bit integer
     *
     * @param int $intcolor
     *
     * @return nothing
     */
    function setIntColor($intcolor)
    {
        // We could do 32 bit and have an alpha channel because
        // Sarven wants one real bad, but nah.

        $this->red   = $intcolor >> 16;
        $this->green = $intcolor >> 8 & 0xFF;
        $this->blue  = $intcolor & 0xFF;

    }

    /**
     * Returns a hex tuple of the RGB color useful for output in HTML
     *
     * @return string
     */
    function hexValue() {

        $hexcolor  = (strlen(dechex($this->red)) < 2 ? '0' : '' ) .
            dechex($this->red);
        $hexcolor .= (strlen(dechex($this->green)) < 2 ? '0' : '') .
            dechex($this->green);
        $hexcolor .= (strlen(dechex($this->blue)) < 2 ? '0' : '') .
            dechex($this->blue);

        return strtoupper($hexcolor);
    }

    /**
     * Returns a 24-bit packed integer representation of the RGB color
     * for convenient storage in the DB
     *
     * XXX: probably could just use hexdec() instead
     *
     * @return int
     */
    function intValue()
    {
        $intcolor = 256 * 256 * $this->red + 256 * $this->green + $this->blue;
        return $intcolor;
    }

}

class WebColorException extends Exception
{
}