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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
|
<?php
/**
* File containing the ezcGraphColor class
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @package Graph
* @version //autogentag//
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
*/
/**
* ezcGraphColor
*
* Struct for representing colors in ezcGraph. A color is defined using the
* common RGBA model with integer values between 0 and 255. An alpha value
* of zero means full opacity, while 255 means full transparency.
*
* @property integer $red
* Red RGBA value of color.
* @property integer $green
* Green RGBA value of color.
* @property integer $blue
* Blue RGBA value of color.
* @property integer $alpha
* Alpha RGBA value of color.
*
* @version //autogentag//
* @package Graph
*/
class ezcGraphColor extends ezcBaseOptions
{
/**
* Constructor
*
* @param array $options Default option array
* @return void
* @ignore
*/
public function __construct( array $options = array() )
{
$this->properties['red'] = 0;
$this->properties['green'] = 0;
$this->properties['blue'] = 0;
$this->properties['alpha'] = 0;
parent::__construct( $options );
}
/**
* __set
*
* @param mixed $propertyName
* @param mixed $propertyValue
* @throws ezcBaseValueException
* If a submitted parameter was out of range or type.
* @throws ezcBasePropertyNotFoundException
* If a the value for the property options is not an instance of
* @return void
* @ignore
*/
public function __set( $propertyName, $propertyValue )
{
switch ( $propertyName )
{
case 'red':
case 'green':
case 'blue':
case 'alpha':
if ( !is_numeric( $propertyValue ) ||
( $propertyValue < 0 ) ||
( $propertyValue > 255 ) )
{
throw new ezcBaseValueException( $propertyName, $propertyValue, '0 <= int <= 255' );
}
$this->properties[$propertyName] = (int) $propertyValue;
break;
default:
throw new ezcBasePropertyNotFoundException( $propertyName );
break;
}
}
/**
* Creates an ezcGraphColor object from a hexadecimal color representation
*
* @param mixed $string Hexadecimal color representation
* @return ezcGraphColor
*/
static public function fromHex( $string )
{
// Remove trailing #
if ( $string[0] === '#' )
{
$string = substr( $string, 1 );
}
// Iterate over chunks and convert to integer
$color = new ezcGraphColor();
$keys = array( 'red', 'green', 'blue', 'alpha' );
foreach ( str_split( $string, 2) as $nr => $hexValue )
{
if ( isset( $keys[$nr] ) )
{
$key = $keys[$nr];
$color->$key = hexdec( $hexValue ) % 256;
}
}
// Set missing values to zero
for ( ++$nr; $nr < count( $keys ); ++$nr )
{
$key = $keys[$nr];
$color->$key = 0;
}
return $color;
}
/**
* Creates an ezcGraphColor object from an array of integers
*
* @param array $array Array of integer color values
* @return ezcGraphColor
*/
static public function fromIntegerArray( array $array )
{
// Iterate over array elements
$color = new ezcGraphColor();
$keys = array( 'red', 'green', 'blue', 'alpha' );
$nr = 0;
foreach ( $array as $colorValue )
{
if ( isset( $keys[$nr] ) )
{
$key = $keys[$nr++];
$color->$key = ( (int) $colorValue ) % 256;
}
}
// Set missing values to zero
for ( $nr; $nr < count( $keys ); ++$nr )
{
$key = $keys[$nr];
$color->$key = 0;
}
return $color;
}
/**
* Creates an ezcGraphColor object from an array of floats
*
* @param array $array Array of float color values
* @return ezcGraphColor
*/
static public function fromFloatArray( array $array )
{
// Iterate over array elements
$color = new ezcGraphColor();
$keys = array( 'red', 'green', 'blue', 'alpha' );
$nr = 0;
foreach ( $array as $colorValue )
{
if ( isset( $keys[$nr] ) )
{
$key = $keys[$nr++];
$color->$key = ( (float) $colorValue * 255 ) % 256;
}
}
// Set missing values to zero
for ( $nr; $nr < count( $keys ); ++$nr )
{
$key = $keys[$nr];
$color->$key = 0;
}
return $color;
}
/**
* Tries to parse provided color value
*
* This method can be used to create a color struct from arbritrary color
* representations. The following values are accepted
*
* - Hexadecimal color definitions, like known from HTML, CSS and SVG
*
* Color definitions like #FF0000, with and and without number sign,
* where each pair of bytes is interpreted as a color value for the
* channels RGB(A). These color values may contain up to 4 values, where
* the last value is considered as the alpha channel.
*
* - Array of integers
*
* If an array of integers is provided as input teh value in each channel
* may be in the span [0 - 255] and is assigned to the color channels
* RGB(A). Up to four values are used from the array.
*
* - Array of floats
*
* If an array of floats is provided as input teh value in each channel
* may be in the span [0 - 1] and is assigned to the color channels
* RGB(A). Up to four values are used from the array.
*
* @param mixed $color Some kind of color definition
* @return ezcGraphColor
*/
static public function create( $color )
{
if ( $color instanceof ezcGraphColor )
{
return $color;
}
elseif ( is_string( $color ) )
{
return ezcGraphColor::fromHex( $color );
}
elseif ( is_array( $color ) )
{
$testElement = reset( $color );
if ( is_int( $testElement ) )
{
return ezcGraphColor::fromIntegerArray( $color );
}
else
{
return ezcGraphColor::fromFloatArray( $color );
}
}
else
{
throw new ezcGraphUnknownColorDefinitionException( $color );
}
}
/**
* Returns a copy of the current color made more transparent by the given
* factor
*
* @param mixed $value Percent to make color mor transparent
* @return ezcGraphColor New color
*/
public function transparent( $value )
{
$color = clone $this;
$color->alpha = 255 - (int) round( ( 255 - $this->alpha ) * ( 1 - $value ) );
return $color;
}
/**
* Inverts and returns a copy of the current color
*
* @return ezcGraphColor New Color
*/
public function invert()
{
$color = new ezcGraphColor();
$color->red = 255 - $this->red;
$color->green = 255 - $this->green;
$color->blue = 255 - $this->blue;
$color->alpha = $this->alpha;
return $color;
}
/**
* Returns a copy of the current color darkened by the given factor
*
* @param float $value Percent to darken the color
* @return ezcGraphColor New color
*/
public function darken( $value )
{
$color = clone $this;
$value = 1 - $value;
$color->red = min( 255, max( 0, (int) round( $this->red * $value ) ) );
$color->green = min( 255, max( 0, (int) round( $this->green * $value ) ) );
$color->blue = min( 255, max( 0, (int) round( $this->blue * $value ) ) );
return $color;
}
}
?>
|