1: <?php
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: class X2Color {
43:
44: 45: 46:
47: public static function rgb2yiq ($color) {
48:
49: $matrixA = array (
50: array (0.299, 0.587, 0.144),
51: array (0.595716, -0.274453, -0.321263),
52: array (0.211456, -0.522591, 0.31135),
53: );
54: $product = array ();
55: $matrixB = $color;
56: foreach ($matrixA as $row) {
57: $product[] = $row[0] * $matrixB[0] + $row[1] * $matrixB[1] + $row[2] * $matrixB[2];
58: }
59: return $product;
60: }
61:
62: 63: 64:
65: public static function getColorBrightness ($hexColor) {
66: $rgb = self::hex2rgb ($hexColor);
67: if (!is_array ($rgb)) {
68: return false;
69: }
70: $yiq = self::rgb2yiq ($rgb);
71: return $yiq[0];
72: }
73:
74:
75: public static function hex2rgb($color) {
76: if($color[0] == '#')
77: $color = substr($color, 1);
78:
79: if(strlen($color) === 6)
80: list($r,$g,$b) = array($color[0].$color[1],$color[2].$color[3],$color[4].$color[5]);
81: elseif (strlen($color) === 3)
82: list($r,$g,$b) = array($color[0].$color[0],$color[1].$color[1],$color[2].$color[2]);
83: else
84: return false;
85:
86: return array(hexdec($r),hexdec($g),hexdec($b));
87: }
88:
89: public static function rgb2hex($r,$g,$b) {
90: $r = dechex($r);
91: $g = dechex($g);
92: $b = dechex($b);
93: if(strlen($r) < 2)
94: $r = '0'.$r;
95: if(strlen($g) <2 )
96: $g = '0'.$g;
97: if(strlen($b) < 2)
98: $b = '0'.$b;
99:
100: return $r.$g.$b;
101: }
102:
103: public static function rgb2hex2($r, $g=-1, $b=-1, $hash = '#') {
104: if (is_array($r) && sizeof($r) == 3)
105: list($r, $g, $b) = $r;
106:
107: $r = intval($r);
108: $g = intval($g);
109: $b = intval($b);
110:
111: $r = dechex($r<0?0:($r>255?255:$r));
112: $g = dechex($g<0?0:($g>255?255:$g));
113: $b = dechex($b<0?0:($b>255?255:$b));
114:
115: $color = (strlen($r) < 2?'0':'').$r;
116: $color .= (strlen($g) < 2?'0':'').$g;
117: $color .= (strlen($b) < 2?'0':'').$b;
118: return $hash.$color;
119: }
120:
121: public static function hex2rgb2($color) {
122: if ($color[0] == '#')
123: $color = substr($color, 1);
124:
125: if (strlen($color) >= 6)
126: list($r, $g, $b) = array($color[0].$color[1],
127: $color[2].$color[3],
128: $color[4].$color[5]);
129: else if (strlen($color) == 3)
130: list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
131: else
132: return false;
133:
134: $r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
135:
136: return array($r, $g, $b);
137: }
138:
139: 140: 141: 142: 143: 144: 145:
146: public static function brightness($color, $percent, $smartMode=false){
147: if(!$color)
148: return '';
149:
150: $color = self::hex2rgb2($color);
151: if (!$color)
152: return '';
153:
154: if ($smartMode) {
155: $hsb = self::rgbtohsb($color);
156: if ($hsb[2] > 0.6) {
157: $percent *= -1;
158: }
159: }
160:
161:
162: $color[0] += 255*$percent;
163: $color[1] += 255*$percent;
164: $color[2] += 255*$percent;
165: return self::rgb2hex2($color[0], $color[1], $color[2]);
166:
167: }
168:
169: public static function opaque($color, $percent = 0.9){
170: if(!$color)
171: return '';
172:
173: $color = self::hex2rgb2($color);
174:
175: return "rgba($color[0], $color[1], $color[2], $percent)" ;
176:
177: }
178:
179: public static function smartText($context, $text) {
180: if(!$text || !$context){
181: return '';
182: }
183:
184: $contextRGB = self::hex2rgb2($context);
185: $textRGB = self::hex2rgb2($text);
186:
187: $contextHSB = self::rgbtohsb($contextRGB);
188: $textHSB = self::rgbtohsb($textRGB);
189:
190: $diff = $contextHSB[2] - $textHSB[2];
191: if ( abs($diff) > 0.56 ) {
192:
193: return $text;
194: }
195:
196: $h = $contextHSB[2] < 0.6 ? 0.8 : -0.8;
197:
198: $textHSB[2] += $h;
199:
200: $textRGB = self::hsbtorgb($textHSB);
201: $text = self::rgb2hex2($textRGB);
202:
203:
204: return $text;
205: }
206:
207: public static function rgbtohsb( $rgb ) {
208: $red = $rgb[0];
209: $green = $rgb[1];
210: $blue = $rgb[2];
211:
212: $oldR = $red;
213: $oldG = $green;
214: $oldB = $blue;
215:
216:
217: $red /= 255;
218: $green /= 255;
219: $blue /= 255;
220:
221: $max = max( $red, $green, $blue );
222: $min = min( $red, $green, $blue );
223:
224: $hue;
225: $sat;
226: $lum = ( $max + $min ) / 2;
227: $diff = $max - $min;
228:
229: if( $diff == 0 ){
230: $hue = $sat = 0;
231: } else {
232: $sat = $diff / ( 1 - abs( 2 * $lum - 1 ) );
233:
234: switch( $max ){
235: case $red:
236: $hue = 60 * fmod( ( ( $green - $blue ) / $diff ), 6 );
237: if ($blue > $green) {
238: $hue += 360;
239: }
240: break;
241:
242: case $green:
243: $hue = 60 * ( ( $blue - $red ) / $diff + 2 );
244: break;
245:
246: case $blue:
247: $hue = 60 * ( ( $red - $green ) / $diff + 4 );
248: break;
249: }
250: }
251:
252: return array( round( $hue, 2 ), round( $sat, 2 ), round( $lum, 2 ) );
253: }
254:
255: public static function hsbtorgb( $hsb ){
256:
257: foreach ($hsb as $i => $v){
258: $hsb[$i] = ($v < 1) ? $v : 1;
259: $hsb[$i] = ($v > 0) ? $v : 0;
260: }
261:
262: $hue = $hsb[0];
263: $sat = $hsb[1];
264: $lum = $hsb[2];
265:
266: $red;
267: $green;
268: $blue;
269:
270: $c = ( 1 - abs( 2 * $lum - 1 ) ) * $sat;
271: $x = $c * ( 1 - abs( fmod( ( $hue / 60 ), 2 ) - 1 ) );
272: $m = $lum - ( $c / 2 );
273:
274: if ( $hue < 60 ) {
275: $red = $c;
276: $green = $x;
277: $blue = 0;
278:
279: } else if ($hue < 120) {
280: $red = $x;
281: $green = $c;
282: $blue = 0;
283:
284: } else if ($hue < 180) {
285: $red = 0;
286: $green = $c;
287: $blue = $x;
288:
289: } else if ($hue < 240) {
290: $red = 0;
291: $green = $x;
292: $blue = $c;
293:
294: } else if ($hue < 300) {
295: $red = $x;
296: $green = 0;
297: $blue = $c;
298:
299: } else {
300: $red = $c;
301: $green = 0;
302: $blue = $x;
303: }
304:
305: $red = ( $red + $m ) * 255;
306: $green = ( $green + $m ) * 255;
307: $blue = ( $blue + $m ) * 255;
308:
309: return array( floor( $red ), floor( $green ), floor( $blue ) );
310: }
311:
312: public static function gradientCss($color1,$color2) {
313: return "
314: background:$color1;
315: background:-moz-linear-gradient(top, $color1 0%, $color2 100%);
316: background:-webkit-gradient(linear, left top, left bottom, color-stop(0%,$color1), color-stop(100%,$color2));
317: background:-webkit-linear-gradient(top, $color1 0%,$color2 100%);
318: background:-o-linear-gradient(top, $color1 0%,$color2 100%);
319: background:-ms-linear-gradient(top, $color1 0%,$color2 100%);
320: background:linear-gradient(to bottom, $color1 0%,$color2 100%);
321: filter:progid:DXImageTransform.Microsoft.gradient( startColorstr='$color1', endColorstr='$color2',GradientType=0);";
322: }
323:
324: public static function HSVToRGB ($h, $s, $v) {
325: $h *= 360;
326: $c = $v * $s;
327: $hPrime = $h / 60;
328: $x = $c * (1 - abs (fmod ($hPrime, 2) - 1));
329: switch (floor ($hPrime)) {
330: case 0:
331: list ($r, $g, $b) = array ($c, $x, 0);
332: break;
333: case 1:
334: list ($r, $g, $b) = array ($x, $c, 0);
335: break;
336: case 2:
337: list ($r, $g, $b) = array (0, $c, $x);
338: break;
339: case 3:
340: list ($r, $g, $b) = array (0, $x, $c);
341: break;
342: case 4:
343: list ($r, $g, $b) = array ($x, 0, $c);
344: break;
345: case 5:
346: list ($r, $g, $b) = array ($c, 0, $x);
347: break;
348: }
349: $r *= 255;
350: $g *= 255;
351: $b *= 255;
352: return array (floor ($r), floor ($g), floor ($b));
353: }
354:
355: public static function generatePalette ($count, $seed=null, $s=0.95, $v=0.99) {
356: $goldenRatio = 0.6180339887;
357: if ($seed === null)
358: $h = rand () / getrandmax ();
359: else
360: $h = $seed;
361: $colors = array ();
362: for ($i = 0; $i < $count; $i++) {
363: $h += $goldenRatio;
364: $h = fmod ($h, 1);
365: $colors[] = self::HSVToRGB ($h, $s, $v);
366: }
367: return $colors;
368: }
369: }
370: ?>
371: