|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class for converting colortypes |
|
4
|
|
|
* |
|
5
|
|
|
* The class includes the following colors formats and types: |
|
6
|
|
|
* |
|
7
|
|
|
* - CMYK |
|
8
|
|
|
* - RGB |
|
9
|
|
|
* - Pantone (seperate include file: pantone.color.class.php) |
|
10
|
|
|
* - HEX Codes for HTML |
|
11
|
|
|
* |
|
12
|
|
|
* @author Sven Wagener <wagener_at_indot_dot_de> |
|
13
|
|
|
* @copyright Sven Wagener |
|
14
|
|
|
* @include Funktion:_include_ |
|
15
|
|
|
* @url http://phpclasses.ehd.com.br/browse/package/804.html |
|
16
|
|
|
*/ |
|
17
|
|
|
class color { |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var array $rgb |
|
21
|
|
|
* @access private |
|
22
|
|
|
* @desc array for RGB colors |
|
23
|
|
|
*/ |
|
24
|
|
|
var $rgb=array('r'=>0,'g'=>0,'b'=>0); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var string $hex |
|
28
|
|
|
* @access private |
|
29
|
|
|
* @desc variable for HTML HEX color |
|
30
|
|
|
*/ |
|
31
|
|
|
var $hex=''; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var array $cmyk |
|
35
|
|
|
* @access private |
|
36
|
|
|
* @desc array for cmyk colors |
|
37
|
|
|
*/ |
|
38
|
|
|
var $cmyk=array('c'=>0,'m'=>0,'y'=>0,'b'=>0); |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Sets the RGB values |
|
42
|
|
|
* @param int $red number from 0-255 for blue color value |
|
43
|
|
|
* @param int $green number from 0-255 for green color value |
|
44
|
|
|
* @param int $blue number from 0-255 for blue color value |
|
45
|
|
|
* @access public |
|
46
|
|
|
* @desc Sets the RGB values |
|
47
|
|
|
*/ |
|
48
|
|
|
function set_rgb($red,$green,$blue){ |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
$this->rgb['r']=$red; |
|
51
|
|
|
$this->rgb['g']=$green; |
|
52
|
|
|
$this->rgb['b']=$blue; |
|
53
|
|
|
|
|
54
|
|
|
$this->convert_rgb_to_cmyk(); |
|
55
|
|
|
$this->convert_rgb_to_hex(); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Sets the HEX HTML color value |
|
60
|
|
|
* @param string $hex 6,3,2, or 1 characters long. |
|
61
|
|
|
* @access public |
|
62
|
|
|
* @desc Sets the HEX HTML color value like ffff00. It will convert shorthand to a 6 digit hex. |
|
63
|
|
|
*/ |
|
64
|
|
|
function set_hex($hex){ |
|
|
|
|
|
|
65
|
|
|
//$hex = settype($hex, 'string'); |
|
66
|
|
|
$hex = strtolower($hex); |
|
67
|
|
|
$hex = preg_replace('/#/', '', $hex); //Strips out the # character |
|
68
|
|
|
$hexlength = strlen($hex); |
|
69
|
|
|
$input = $hex; |
|
70
|
|
|
switch($hexlength) { |
|
71
|
|
|
case 1: |
|
72
|
|
|
$hex = $input.$input.$input.$input.$input.$input; |
|
73
|
|
|
break; |
|
74
|
|
|
case 2: |
|
75
|
|
|
$hex = $input[0].$input[1].$input[0].$input[1].$input[0].$input[1]; |
|
76
|
|
|
break; |
|
77
|
|
|
case 3: |
|
78
|
|
|
$hex = $input[0].$input[0].$input[1].$input[1].$input[2].$input[2]; |
|
79
|
|
|
break; |
|
80
|
|
|
} |
|
81
|
|
|
$this->hex=$hex; |
|
82
|
|
|
|
|
83
|
|
|
$this->convert_hex_to_rgb(); |
|
84
|
|
|
$this->convert_rgb_to_cmyk(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* Sets the HTML color name, converting it to a 6 digit hex code. |
|
89
|
|
|
* @param string $name The name of the color. |
|
90
|
|
|
* @access public |
|
91
|
|
|
* @desc Sets the HTML color name, converting it to a 6 digit hex code. |
|
92
|
|
|
*/ |
|
93
|
|
|
function set_name($name){ |
|
|
|
|
|
|
94
|
|
|
$this->hex = $this->convert_name_to_hex($name); |
|
95
|
|
|
|
|
96
|
|
|
$this->convert_hex_to_rgb(); |
|
97
|
|
|
$this->convert_rgb_to_cmyk(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Sets the CMYK color values |
|
102
|
|
|
* @param int $c number from 0-100 for c color value |
|
103
|
|
|
* @param int $m number from 0-100 for m color value |
|
104
|
|
|
* @param int $y number from 0-100 for y color value |
|
105
|
|
|
* @param int $b number from 0-100 for b color value |
|
106
|
|
|
* @access public |
|
107
|
|
|
* @desc Sets the CMYK color values |
|
108
|
|
|
*/ |
|
109
|
|
|
function set_cmyk($c,$m,$y,$b){ |
|
|
|
|
|
|
110
|
|
|
$this->cmyk['c']=$c; |
|
111
|
|
|
$this->cmyk['m']=$m; |
|
112
|
|
|
$this->cmyk['y']=$y; |
|
113
|
|
|
$this->cmyk['b']=$b; |
|
114
|
|
|
|
|
115
|
|
|
$this->convert_cmyk_to_rgb(); |
|
116
|
|
|
$this->convert_rgb_to_hex(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Sets the pantone color value |
|
121
|
|
|
* @param string $pantone_name name of the pantone color |
|
122
|
|
|
* @access public |
|
123
|
|
|
* @desc Sets the pantone color value |
|
124
|
|
|
*/ |
|
125
|
|
|
function set_pantone($pantone_name){ |
|
|
|
|
|
|
126
|
|
|
$this->pantone=$pantone_name; |
|
|
|
|
|
|
127
|
|
|
$this->cmyk['c']=$this->pantone_pallete[$pantone_name]['c']; |
|
|
|
|
|
|
128
|
|
|
$this->cmyk['m']=$this->pantone_pallete[$pantone_name]['m']; |
|
129
|
|
|
$this->cmyk['y']=$this->pantone_pallete[$pantone_name]['y']; |
|
130
|
|
|
$this->cmyk['b']=$this->pantone_pallete[$pantone_name]['b']; |
|
131
|
|
|
|
|
132
|
|
|
$this->convert_cmyk_to_rgb(); |
|
133
|
|
|
$this->convert_rgb_to_hex(); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Sets the pantone pc color value |
|
138
|
|
|
* @param string $pantone_name_pc name of the pantone pc color |
|
139
|
|
|
* @access public |
|
140
|
|
|
* @desc Sets the pantone pc color value |
|
141
|
|
|
*/ |
|
142
|
|
|
function set_pantone_pc($pantone_name){ |
|
|
|
|
|
|
143
|
|
|
$this->pantone_pc=$pantone_name; |
|
|
|
|
|
|
144
|
|
|
$this->cmyk['c']=$this->pantone_pallete_pc[$pantone_name]['c']; |
|
|
|
|
|
|
145
|
|
|
$this->cmyk['m']=$this->pantone_pallete_pc[$pantone_name]['m']; |
|
146
|
|
|
$this->cmyk['y']=$this->pantone_pallete_pc[$pantone_name]['y']; |
|
147
|
|
|
$this->cmyk['b']=$this->pantone_pallete_pc[$pantone_name]['b']; |
|
148
|
|
|
|
|
149
|
|
|
$this->convert_cmyk_to_rgb(); |
|
150
|
|
|
$this->convert_rgb_to_hex(); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
//include("pantone.color.class.php"); |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Returns the RGB values of a set color |
|
157
|
|
|
* @return array $rgb color values of red ($rgb['r']), green ($rgb['green') and blue ($rgb['b']) |
|
158
|
|
|
* @access public |
|
159
|
|
|
* @desc Returns the RGB values of a set color |
|
160
|
|
|
*/ |
|
161
|
|
|
function get_rgb($val){ |
|
|
|
|
|
|
162
|
|
|
if($val) { |
|
163
|
|
|
return $this->rgb[$val]; |
|
164
|
|
|
} else { |
|
165
|
|
|
return $this->rgb; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Returns the HEX HTML color value of a set color |
|
171
|
|
|
* @return string $hex HEX HTML color value |
|
172
|
|
|
* @access public |
|
173
|
|
|
* @desc Returns the HEX HTML color value of a set color |
|
174
|
|
|
*/ |
|
175
|
|
|
function get_hex(){ |
|
|
|
|
|
|
176
|
|
|
return $this->hex; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Returns the CMYK values of a set color |
|
181
|
|
|
* @return array $cmyk color values of c ($cmyk['c']), m ($cmyk['m'), y ($cmyk['blue']) and b ($cmyk['b']) |
|
182
|
|
|
* @access public |
|
183
|
|
|
* @desc Returns the CMYK values of a set color |
|
184
|
|
|
*/ |
|
185
|
|
|
function get_cmyk(){ |
|
|
|
|
|
|
186
|
|
|
return $this->cmyk; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
/** |
|
190
|
|
|
* Converts the RGB colors to HEX HTML colors |
|
191
|
|
|
* @access private |
|
192
|
|
|
* @desc Converts the RGB colors to HEX HTML colors |
|
193
|
|
|
*/ |
|
194
|
|
|
function convert_rgb_to_hex(){ |
|
|
|
|
|
|
195
|
|
|
$this->hex=$this->hex_trip[$this->rgb['r']].$this->hex_trip[$this->rgb['g']].$this->hex_trip[$this->rgb['b']]; |
|
|
|
|
|
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Converts the RGB colors to CMYK colors |
|
200
|
|
|
* @access private |
|
201
|
|
|
* @desc Converts the RGB colors to CMYK colors |
|
202
|
|
|
*/ |
|
203
|
|
|
function convert_rgb_to_cmyk(){ |
|
|
|
|
|
|
204
|
|
|
$c = (255-$this->rgb['r'] )/255.0*100; |
|
205
|
|
|
$m = (255-$this->rgb['g'] )/255.0*100; |
|
206
|
|
|
$y = (255-$this->rgb['b'] )/255.0*100; |
|
207
|
|
|
|
|
208
|
|
|
$b = min(array($c,$m,$y)); |
|
209
|
|
|
$c=$c-$b; |
|
210
|
|
|
$m=$m-$b; |
|
211
|
|
|
$y=$y-$b; |
|
212
|
|
|
|
|
213
|
|
|
$this->cmyk = array( 'c' => $c, 'm' => $m, 'y' => $y, 'b' => $b); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Converts the CMYK colors to RGB colors |
|
218
|
|
|
* @access private |
|
219
|
|
|
* @desc Converts the CMYK colors to RGB colors |
|
220
|
|
|
*/ |
|
221
|
|
|
function convert_cmyk_to_rgb(){ |
|
|
|
|
|
|
222
|
|
|
$red=$this->cmyk['c']+$this->cmyk['b']; |
|
223
|
|
|
$green=$this->cmyk['m']+$this->cmyk['b']; |
|
224
|
|
|
$blue=$this->cmyk['y']+$this->cmyk['b']; |
|
225
|
|
|
|
|
226
|
|
|
$red=($red-100)*(-1); |
|
227
|
|
|
$green=($green-100)*(-1); |
|
228
|
|
|
$blue=($blue-100)*(-1); |
|
229
|
|
|
|
|
230
|
|
|
$red=round($red/100*255,0); |
|
231
|
|
|
$green=round($green/100*255,0); |
|
232
|
|
|
$blue=round($blue/100*255,0); |
|
233
|
|
|
|
|
234
|
|
|
$this->rgb['r']=$red; |
|
235
|
|
|
$this->rgb['g']=$green; |
|
236
|
|
|
$this->rgb['b']=$blue; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Converts the HTML HEX colors to RGB colors |
|
241
|
|
|
* @access private |
|
242
|
|
|
* @desc Converts the HTML HEX colors to RGB colors |
|
243
|
|
|
* @url http://css-tricks.com/snippets/php/convert-hex-to-rgb/ |
|
244
|
|
|
*/ |
|
245
|
|
|
function convert_hex_to_rgb(){ |
|
|
|
|
|
|
246
|
|
|
$red = substr($this->hex,0,2); |
|
247
|
|
|
$green = substr($this->hex,2,2); |
|
248
|
|
|
$blue = substr($this->hex,4,2); |
|
249
|
|
|
$this->rgb['r'] = hexdec( $red ); |
|
250
|
|
|
$this->rgb['g'] = hexdec( $green ); |
|
251
|
|
|
$this->rgb['b'] = hexdec( $blue ); |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* Converts HTML color name to 6 digit HEX value. |
|
256
|
|
|
* @access private |
|
257
|
|
|
* @param string $name One of the offical HTML color names. |
|
258
|
|
|
* @desc Converts HTML color name to 6 digit HEX value. |
|
259
|
|
|
* @url http://en.wikipedia.org/wiki/HTML_color_names |
|
260
|
|
|
*/ |
|
261
|
|
|
function convert_name_to_hex($name){ |
|
|
|
|
|
|
262
|
|
|
$color_names = array( |
|
263
|
|
|
'aqua' => '00ffff', |
|
264
|
|
|
'cyan' => '00ffff', |
|
265
|
|
|
'gray' => '808080', |
|
266
|
|
|
'grey' => '808080', |
|
267
|
|
|
'navy' => '000080', |
|
268
|
|
|
'silver' => 'C0C0C0', |
|
269
|
|
|
'black' => '000000', |
|
270
|
|
|
'green' => '008000', |
|
271
|
|
|
'olive' => '808000', |
|
272
|
|
|
'teal' => '008080', |
|
273
|
|
|
'blue' => '0000FF', |
|
274
|
|
|
'lime' => '00FF00', |
|
275
|
|
|
'purple' => '800080', |
|
276
|
|
|
'white' => 'ffffff', |
|
277
|
|
|
'fuchsia' => 'FF00FF', |
|
278
|
|
|
'magenta' => 'FF00FF', |
|
279
|
|
|
'maroon' => '800000', |
|
280
|
|
|
'red' => 'FF0000', |
|
281
|
|
|
'yellow' => 'FFFF00' |
|
282
|
|
|
); |
|
283
|
|
|
if (array_key_exists($name, $color_names)) { |
|
284
|
|
|
return $color_names[$name]; |
|
285
|
|
|
} |
|
286
|
|
|
else { |
|
287
|
|
|
//error |
|
288
|
|
|
} |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
?> |
|
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.