1: <?php
2:
3: /**
4: * @package image
5: *
6: * @author Антон Кургузенков <kurguzenkov@list.ru>
7: *
8: * @version 0.01
9: * @since 2012-11-11
10: */
11:
12: require_once 'geometry/exceptions.php';
13:
14: /**
15: * Класс, описывающий цвет в RGB формате
16: */
17:
18: class AcColor
19: {
20: /**
21: * Интенсивность красного канала.
22: * @var int
23: */
24:
25: private $red;
26:
27: /**
28: * Интенсивность зелёного канала.
29: * @var int
30: */
31:
32: private $green;
33:
34: /**
35: * Интенсивность синего канала.
36: * @var int
37: */
38:
39: private $blue;
40:
41: const WHITE = 16777215; // pow(2, 8 * 3) || 0xFFFFFF
42: const BLACK = 0;
43:
44: public function __construct() // $r, $g, $b || $code
45: {
46: $a = func_get_args();
47: if (count($a) == 3)
48: {
49: $this->setRed($a[0]);
50: $this->setGreen($a[1]);
51: $this->setBlue($a[2]);
52: }
53: else if (count($a) == 1)
54: {
55: if (!self::isValidCode($a[0]))
56: throw new IllegalArgumentException();
57:
58: $hexCode = dechex($a[0]);
59: $r = hexdec(substr($hexCode, 0, 2));
60: $g = hexdec(substr($hexCode, 2, 2));
61: $b = hexdec(substr($hexCode, 4, 2));
62: $this->__construct($r, $g, $b);
63: }
64: }
65:
66: /**
67: * Устанавливает, валидна ли интенсивность цветового канала.
68: *
69: * @param $channel иненсивность канала
70: * @return boolean
71: */
72:
73: public static function isValidChanel($channel)
74: {
75: return is_integer($channel) && $channel >=0 && $channel < 256;
76: }
77:
78: /**
79: * Устанавливает, валиден ли код цвета.
80: *
81: * @param int $code
82: * @return boolean
83: */
84:
85: public static function isValidCode($code)
86: {
87: return is_integer($code) && $code >= 0 && $code <= self::WHITE;
88: }
89:
90: /**
91: * @ignore
92: */
93:
94: private function convert10To16($channel)
95: {
96: if ($channel < 16)
97: {
98: return "0".dechex($channel);
99: }
100: return dechex($channel);
101: }
102:
103: /**
104: * Возвращает шестнадцатиричный код цвета в формате 0xRRGGBB
105: * @return string
106: */
107:
108: public function getHexCode()
109: {
110: $r = $this->convert10To16($this->red);
111: $g = $this->convert10To16($this->green);
112: $b = $this->convert10To16($this->blue);
113:
114: return "0x$r$g$b";
115: }
116:
117: // getters and setters
118:
119: public function getCode()
120: {
121: return hexdec($this->getHexCode());
122: }
123:
124: /**
125: * @param int
126: * @throws InvalidChannelException
127: */
128:
129: public function setRed($red)
130: {
131: if (self::isValidChanel($red))
132: $this->red = $red;
133: else
134: throw new InvalidChannelException('red');
135: }
136:
137: /**
138: * @param int
139: * @throws InvalidChannelException
140: */
141:
142: public function setGreen($green)
143: {
144: if (self::isValidChanel($green))
145: $this->green = $green;
146: else
147: throw new InvalidChannelException('green');
148: }
149:
150: /**
151: * @param int
152: * @throws InvalidChannelException
153: */
154:
155: public function setBlue($blue)
156: {
157: if (self::isValidChanel($blue))
158: $this->blue = $blue;
159: else
160: throw new InvalidChannelException('blue');
161: }
162:
163: public function getRed()
164: {
165: return $this->red;
166: }
167:
168: public function getGreen()
169: {
170: return $this->green;
171: }
172:
173: public function getBlue()
174: {
175: return $this->blue;
176: }
177: }
178: ?>