1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: class Horde_Image
17: {
18: 19: 20: 21: 22: 23: 24: 25:
26: static public function modifyColor($color, $factor = 0x11)
27: {
28: list($r, $g, $b) = self::getColor($color);
29:
30: $r = min(max($r + $factor, 0), 255);
31: $g = min(max($g + $factor, 0), 255);
32: $b = min(max($b + $factor, 0), 255);
33:
34: return '#' . str_pad(dechex($r), 2, '0', STR_PAD_LEFT) . str_pad(dechex($g), 2, '0', STR_PAD_LEFT) . str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
35: }
36:
37: 38: 39: 40: 41: 42: 43: 44:
45: static public function moreIntenseColor($color, $factor = 0x11)
46: {
47: list($r, $g, $b) = self::getColor($color);
48:
49: if ($r >= $g && $r >= $b) {
50: $g = $g / $r;
51: $b = $b / $r;
52:
53: $r += $factor;
54: $g = floor($g * $r);
55: $b = floor($b * $r);
56: } elseif ($g >= $r && $g >= $b) {
57: $r = $r / $g;
58: $b = $b / $g;
59:
60: $g += $factor;
61: $r = floor($r * $g);
62: $b = floor($b * $g);
63: } else {
64: $r = $r / $b;
65: $g = $g / $b;
66:
67: $b += $factor;
68: $r = floor($r * $b);
69: $g = floor($g * $b);
70: }
71:
72: $r = min(max($r, 0), 255);
73: $g = min(max($g, 0), 255);
74: $b = min(max($b, 0), 255);
75:
76: return '#' . str_pad(dechex($r), 2, '0', STR_PAD_LEFT) . str_pad(dechex($g), 2, '0', STR_PAD_LEFT) . str_pad(dechex($b), 2, '0', STR_PAD_LEFT);
77: }
78:
79: 80: 81: 82: 83: 84: 85:
86: static public function brightness($color)
87: {
88: list($r, $g, $b) = self::getColor($color);
89:
90: return round((($r * 299) + ($g * 587) + ($b * 114)) / 1000);
91: }
92:
93: 94: 95:
96: static public function grayscaleValue($r, $g, $b)
97: {
98: return round(($r * 0.30) + ($g * 0.59) + ($b * 0.11));
99: }
100:
101: 102: 103:
104: static public function grayscalePixel($originalPixel)
105: {
106: $gray = Horde_Image::grayscaleValue($originalPixel['red'], $originalPixel['green'], $originalPixel['blue']);
107: return array('red'=>$gray, 'green'=>$gray, 'blue'=>$gray);
108: }
109:
110: 111: 112: 113: 114: 115: 116:
117: static public function getColor($color)
118: {
119: if ($color[0] == '#') {
120: $color = substr($color, 1);
121: }
122:
123: if (strlen($color) == 3) {
124: $color = str_repeat($color[0], 2) .
125: str_repeat($color[1], 2) .
126: str_repeat($color[2], 2);
127: }
128:
129: return array(
130: hexdec(substr($color, 0, 2)),
131: hexdec(substr($color, 2, 2)),
132: hexdec(substr($color, 4, 2))
133: );
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: static public function getRGB($colorname)
144: {
145: require_once dirname(__FILE__) . '/Image/rgb.php';
146: return isset($GLOBALS['horde_image_rgb_colors'][$colorname]) ?
147: $GLOBALS['horde_image_rgb_colors'][$colorname] :
148: array(0, 0, 0);
149: }
150:
151: 152: 153: 154: 155: 156: 157:
158: static public function getHexColor($colorname)
159: {
160: require_once dirname(__FILE__) . '/Image/rgb.php';
161: if (isset($GLOBALS['horde_image_rgb_colors'][$colorname])) {
162: list($r, $g, $b) = $GLOBALS['horde_image_rgb_colors'][$colorname];
163: return '#' . str_pad(dechex(min($r, 255)), 2, '0', STR_PAD_LEFT) . str_pad(dechex(min($g, 255)), 2, '0', STR_PAD_LEFT) . str_pad(dechex(min($b, 255)), 2, '0', STR_PAD_LEFT);
164: } else {
165: return 'black';
166: }
167: }
168:
169: 170: 171: 172: 173: 174: 175: 176: 177: 178:
179: static public function circlePoint($degrees, $diameter)
180: {
181:
182: $degrees += 0.0001;
183:
184: return array(cos(deg2rad($degrees)) * ($diameter / 2),
185: sin(deg2rad($degrees)) * ($diameter / 2));
186: }
187:
188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199:
200: static public function arcPoints($r, $start, $end)
201: {
202:
203: $pts['x1'] = $r * cos(deg2rad($start));
204: $pts['y1'] = $r * sin(deg2rad($start));
205:
206:
207: $pts['x2'] = $r * cos(deg2rad($end));
208: $pts['y2'] = $r * sin(deg2rad($end));
209:
210:
211: $a3 = ($start + $end) / 2;
212: $r3 = $r / cos(deg2rad(($end - $start) / 2));
213: $pts['x3'] = $r3 * cos(deg2rad($a3));
214: $pts['y3'] = $r3 * sin(deg2rad($a3));
215:
216: return $pts;
217: }
218:
219: 220: 221:
222: static public function getFontSize($fontsize)
223: {
224: switch ($fontsize) {
225: case 'medium':
226: $point = 18;
227: break;
228: case 'large':
229: $point = 24;
230: break;
231: case 'giant':
232: $point = 30;
233: break;
234: default:
235: $point = 12;
236: }
237:
238: return $point;
239: }
240:
241: }
242: