-
Notifications
You must be signed in to change notification settings - Fork 120
/
color.class.php
executable file
·291 lines (261 loc) · 7.35 KB
/
color.class.php
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
<?php
/**
* Class for converting colortypes
*
* The class includes the following colors formats and types:
*
* - CMYK
* - RGB
* - Pantone (seperate include file: pantone.color.class.php)
* - HEX Codes for HTML
*
* @author Sven Wagener <wagener_at_indot_dot_de>
* @copyright Sven Wagener
* @include Funktion:_include_
* @url http://phpclasses.ehd.com.br/browse/package/804.html
*/
class color {
/**
* @var array $rgb
* @access private
* @desc array for RGB colors
*/
var $rgb=array('r'=>0,'g'=>0,'b'=>0);
/**
* @var string $hex
* @access private
* @desc variable for HTML HEX color
*/
var $hex='';
/**
* @var array $cmyk
* @access private
* @desc array for cmyk colors
*/
var $cmyk=array('c'=>0,'m'=>0,'y'=>0,'b'=>0);
/**
* Sets the RGB values
* @param int $red number from 0-255 for blue color value
* @param int $green number from 0-255 for green color value
* @param int $blue number from 0-255 for blue color value
* @access public
* @desc Sets the RGB values
*/
function set_rgb($red,$green,$blue){
$this->rgb['r']=$red;
$this->rgb['g']=$green;
$this->rgb['b']=$blue;
$this->convert_rgb_to_cmyk();
$this->convert_rgb_to_hex();
}
/**
* Sets the HEX HTML color value
* @param string $hex 6,3,2, or 1 characters long.
* @access public
* @desc Sets the HEX HTML color value like ffff00. It will convert shorthand to a 6 digit hex.
*/
function set_hex($hex){
//$hex = settype($hex, 'string');
$hex = strtolower($hex);
$hex = preg_replace('/#/', '', $hex); //Strips out the # character
$hexlength = strlen($hex);
$input = $hex;
switch($hexlength) {
case 1:
$hex = $input.$input.$input.$input.$input.$input;
break;
case 2:
$hex = $input[0].$input[1].$input[0].$input[1].$input[0].$input[1];
break;
case 3:
$hex = $input[0].$input[0].$input[1].$input[1].$input[2].$input[2];
break;
}
$this->hex=$hex;
$this->convert_hex_to_rgb();
$this->convert_rgb_to_cmyk();
}
/**
* Sets the HTML color name, converting it to a 6 digit hex code.
* @param string $name The name of the color.
* @access public
* @desc Sets the HTML color name, converting it to a 6 digit hex code.
*/
function set_name($name){
$this->hex = $this->convert_name_to_hex($name);
$this->convert_hex_to_rgb();
$this->convert_rgb_to_cmyk();
}
/**
* Sets the CMYK color values
* @param int $c number from 0-100 for c color value
* @param int $m number from 0-100 for m color value
* @param int $y number from 0-100 for y color value
* @param int $b number from 0-100 for b color value
* @access public
* @desc Sets the CMYK color values
*/
function set_cmyk($c,$m,$y,$b){
$this->cmyk['c']=$c;
$this->cmyk['m']=$m;
$this->cmyk['y']=$y;
$this->cmyk['b']=$b;
$this->convert_cmyk_to_rgb();
$this->convert_rgb_to_hex();
}
/**
* Sets the pantone color value
* @param string $pantone_name name of the pantone color
* @access public
* @desc Sets the pantone color value
*/
function set_pantone($pantone_name){
$this->pantone=$pantone_name;
$this->cmyk['c']=$this->pantone_pallete[$pantone_name]['c'];
$this->cmyk['m']=$this->pantone_pallete[$pantone_name]['m'];
$this->cmyk['y']=$this->pantone_pallete[$pantone_name]['y'];
$this->cmyk['b']=$this->pantone_pallete[$pantone_name]['b'];
$this->convert_cmyk_to_rgb();
$this->convert_rgb_to_hex();
}
/**
* Sets the pantone pc color value
* @param string $pantone_name_pc name of the pantone pc color
* @access public
* @desc Sets the pantone pc color value
*/
function set_pantone_pc($pantone_name){
$this->pantone_pc=$pantone_name;
$this->cmyk['c']=$this->pantone_pallete_pc[$pantone_name]['c'];
$this->cmyk['m']=$this->pantone_pallete_pc[$pantone_name]['m'];
$this->cmyk['y']=$this->pantone_pallete_pc[$pantone_name]['y'];
$this->cmyk['b']=$this->pantone_pallete_pc[$pantone_name]['b'];
$this->convert_cmyk_to_rgb();
$this->convert_rgb_to_hex();
}
//include("pantone.color.class.php");
/**
* Returns the RGB values of a set color
* @return array $rgb color values of red ($rgb['r']), green ($rgb['green') and blue ($rgb['b'])
* @access public
* @desc Returns the RGB values of a set color
*/
function get_rgb($val){
if($val) {
return $this->rgb[$val];
} else {
return $this->rgb;
}
}
/**
* Returns the HEX HTML color value of a set color
* @return string $hex HEX HTML color value
* @access public
* @desc Returns the HEX HTML color value of a set color
*/
function get_hex(){
return $this->hex;
}
/**
* Returns the CMYK values of a set color
* @return array $cmyk color values of c ($cmyk['c']), m ($cmyk['m'), y ($cmyk['blue']) and b ($cmyk['b'])
* @access public
* @desc Returns the CMYK values of a set color
*/
function get_cmyk(){
return $this->cmyk;
}
/**
* Converts the RGB colors to HEX HTML colors
* @access private
* @desc Converts the RGB colors to HEX HTML colors
*/
function convert_rgb_to_hex(){
$this->hex=$this->hex_trip[$this->rgb['r']].$this->hex_trip[$this->rgb['g']].$this->hex_trip[$this->rgb['b']];
}
/**
* Converts the RGB colors to CMYK colors
* @access private
* @desc Converts the RGB colors to CMYK colors
*/
function convert_rgb_to_cmyk(){
$c = (255-$this->rgb['r'] )/255.0*100;
$m = (255-$this->rgb['g'] )/255.0*100;
$y = (255-$this->rgb['b'] )/255.0*100;
$b = min(array($c,$m,$y));
$c=$c-$b;
$m=$m-$b;
$y=$y-$b;
$this->cmyk = array( 'c' => $c, 'm' => $m, 'y' => $y, 'b' => $b);
}
/**
* Converts the CMYK colors to RGB colors
* @access private
* @desc Converts the CMYK colors to RGB colors
*/
function convert_cmyk_to_rgb(){
$red=$this->cmyk['c']+$this->cmyk['b'];
$green=$this->cmyk['m']+$this->cmyk['b'];
$blue=$this->cmyk['y']+$this->cmyk['b'];
$red=($red-100)*(-1);
$green=($green-100)*(-1);
$blue=($blue-100)*(-1);
$red=round($red/100*255,0);
$green=round($green/100*255,0);
$blue=round($blue/100*255,0);
$this->rgb['r']=$red;
$this->rgb['g']=$green;
$this->rgb['b']=$blue;
}
/**
* Converts the HTML HEX colors to RGB colors
* @access private
* @desc Converts the HTML HEX colors to RGB colors
* @url http://css-tricks.com/snippets/php/convert-hex-to-rgb/
*/
function convert_hex_to_rgb(){
$red = substr($this->hex,0,2);
$green = substr($this->hex,2,2);
$blue = substr($this->hex,4,2);
$this->rgb['r'] = hexdec( $red );
$this->rgb['g'] = hexdec( $green );
$this->rgb['b'] = hexdec( $blue );
}
/**
* Converts HTML color name to 6 digit HEX value.
* @access private
* @param string $name One of the offical HTML color names.
* @desc Converts HTML color name to 6 digit HEX value.
* @url http://en.wikipedia.org/wiki/HTML_color_names
*/
function convert_name_to_hex($name){
$color_names = array(
'aqua' => '00ffff',
'cyan' => '00ffff',
'gray' => '808080',
'grey' => '808080',
'navy' => '000080',
'silver' => 'C0C0C0',
'black' => '000000',
'green' => '008000',
'olive' => '808000',
'teal' => '008080',
'blue' => '0000FF',
'lime' => '00FF00',
'purple' => '800080',
'white' => 'ffffff',
'fuchsia' => 'FF00FF',
'magenta' => 'FF00FF',
'maroon' => '800000',
'red' => 'FF0000',
'yellow' => 'FFFF00'
);
if (array_key_exists($name, $color_names)) {
return $color_names[$name];
}
else {
//error
}
}
}
?>