PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/Image/lib/Horde/Image.php

https://github.com/wrobel/horde
PHP | 241 lines | 121 code | 27 blank | 93 comment | 11 complexity | fdf77505c9daf778a93692cb576bd9e5 MD5 | raw file
Possible License(s): BSD-2-Clause, AGPL-1.0, LGPL-2.1, LGPL-3.0, BSD-3-Clause, LGPL-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * This class provides some utility functions, such as generating highlights
  4. * of a color as well as a factory method responsible for creating a concrete
  5. * Horde_Image driver.
  6. *
  7. * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
  8. *
  9. * See the enclosed file COPYING for license information (LGPL). If you
  10. * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  11. *
  12. * @author Chuck Hagenbuch <chuck@horde.org>
  13. * @author Michael J. Rubinsky <mrubinsk@horde.org>
  14. * @package Image
  15. */
  16. class Horde_Image
  17. {
  18. /**
  19. * Calculate a lighter (or darker) version of a color.
  20. *
  21. * @param string $color An HTML color, e.g.: #ffffcc.
  22. * @param string $factor TODO
  23. *
  24. * @return string A modified HTML color.
  25. */
  26. static public function modifyColor($color, $factor = 0x11)
  27. {
  28. list($r, $g, $b) = self::getColor($color);
  29. $r = min(max($r + $factor, 0), 255);
  30. $g = min(max($g + $factor, 0), 255);
  31. $b = min(max($b + $factor, 0), 255);
  32. 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);
  33. }
  34. /**
  35. * Calculate a more intense version of a color.
  36. *
  37. * @param string $color An HTML color, e.g.: #ffffcc.
  38. * @param string $factor TODO
  39. *
  40. * @return string A more intense HTML color.
  41. */
  42. static public function moreIntenseColor($color, $factor = 0x11)
  43. {
  44. list($r, $g, $b) = self::getColor($color);
  45. if ($r >= $g && $r >= $b) {
  46. $g = $g / $r;
  47. $b = $b / $r;
  48. $r += $factor;
  49. $g = floor($g * $r);
  50. $b = floor($b * $r);
  51. } elseif ($g >= $r && $g >= $b) {
  52. $r = $r / $g;
  53. $b = $b / $g;
  54. $g += $factor;
  55. $r = floor($r * $g);
  56. $b = floor($b * $g);
  57. } else {
  58. $r = $r / $b;
  59. $g = $g / $b;
  60. $b += $factor;
  61. $r = floor($r * $b);
  62. $g = floor($g * $b);
  63. }
  64. $r = min(max($r, 0), 255);
  65. $g = min(max($g, 0), 255);
  66. $b = min(max($b, 0), 255);
  67. 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);
  68. }
  69. /**
  70. * Returns the brightness of a color.
  71. *
  72. * @param string $color An HTML color, e.g.: #ffffcc.
  73. *
  74. * @return integer The brightness on a scale of 0 to 255.
  75. */
  76. static public function brightness($color)
  77. {
  78. list($r, $g, $b) = self::getColor($color);
  79. return round((($r * 299) + ($g * 587) + ($b * 114)) / 1000);
  80. }
  81. /**
  82. * @TODO
  83. */
  84. static public function grayscaleValue($r, $g, $b)
  85. {
  86. return round(($r * 0.30) + ($g * 0.59) + ($b * 0.11));
  87. }
  88. /**
  89. * @TODO
  90. */
  91. static public function grayscalePixel($originalPixel)
  92. {
  93. $gray = Horde_Image::grayscaleValue($originalPixel['red'], $originalPixel['green'], $originalPixel['blue']);
  94. return array('red'=>$gray, 'green'=>$gray, 'blue'=>$gray);
  95. }
  96. /**
  97. * Normalizes an HTML color.
  98. *
  99. * @param string $color An HTML color, e.g.: #ffffcc or #ffc.
  100. *
  101. * @return array Array with three elements: red, green, and blue.
  102. */
  103. static public function getColor($color)
  104. {
  105. if ($color[0] == '#') {
  106. $color = substr($color, 1);
  107. }
  108. if (strlen($color) == 3) {
  109. $color = str_repeat($color[0], 2) .
  110. str_repeat($color[1], 2) .
  111. str_repeat($color[2], 2);
  112. }
  113. return array(
  114. hexdec(substr($color, 0, 2)),
  115. hexdec(substr($color, 2, 2)),
  116. hexdec(substr($color, 4, 2))
  117. );
  118. }
  119. /**
  120. * Get the RGB value for a given colorname.
  121. *
  122. * @param string $colorname The colorname
  123. *
  124. * @return array An array of RGB values.
  125. */
  126. static public function getRGB($colorname)
  127. {
  128. require_once dirname(__FILE__) . '/Image/rgb.php';
  129. return isset($GLOBALS['horde_image_rgb_colors'][$colorname]) ?
  130. $GLOBALS['horde_image_rgb_colors'][$colorname] :
  131. array(0, 0, 0);
  132. }
  133. /**
  134. * Get the hex representation of the given colorname.
  135. *
  136. * @param string $colorname The colorname
  137. *
  138. * @return string The hex representation of the color.
  139. */
  140. static public function getHexColor($colorname)
  141. {
  142. require_once dirname(__FILE__) . '/Image/rgb.php';
  143. if (isset($GLOBALS['horde_image_rgb_colors'][$colorname])) {
  144. list($r, $g, $b) = $GLOBALS['horde_image_rgb_colors'][$colorname];
  145. 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);
  146. } else {
  147. return 'black';
  148. }
  149. }
  150. /**
  151. * Get an x,y pair on circle, assuming center is 0,0.
  152. *
  153. * @access private
  154. *
  155. * @param double $degrees The degrees of arc to get the point for.
  156. * @param integer $diameter The diameter of the circle.
  157. *
  158. * @return array (x coordinate, y coordinate) of the point.
  159. */
  160. static public function circlePoint($degrees, $diameter)
  161. {
  162. // Avoid problems with doubles.
  163. $degrees += 0.0001;
  164. return array(cos(deg2rad($degrees)) * ($diameter / 2),
  165. sin(deg2rad($degrees)) * ($diameter / 2));
  166. }
  167. /**
  168. * Get point coordinates at the limits of an arc. Only valid for
  169. * angles ($end - $start) <= 45 degrees.
  170. *
  171. * @access private
  172. *
  173. * @param integer $r The radius of the arc.
  174. * @param integer $start The starting angle.
  175. * @param integer $end The ending angle.
  176. *
  177. * @return array The start point, end point, and anchor point.
  178. */
  179. static public function arcPoints($r, $start, $end)
  180. {
  181. // Start point.
  182. $pts['x1'] = $r * cos(deg2rad($start));
  183. $pts['y1'] = $r * sin(deg2rad($start));
  184. // End point.
  185. $pts['x2'] = $r * cos(deg2rad($end));
  186. $pts['y2'] = $r * sin(deg2rad($end));
  187. // Anchor point.
  188. $a3 = ($start + $end) / 2;
  189. $r3 = $r / cos(deg2rad(($end - $start) / 2));
  190. $pts['x3'] = $r3 * cos(deg2rad($a3));
  191. $pts['y3'] = $r3 * sin(deg2rad($a3));
  192. return $pts;
  193. }
  194. /**
  195. * Return point size for font
  196. */
  197. static public function getFontSize($fontsize)
  198. {
  199. switch ($fontsize) {
  200. case 'medium':
  201. $point = 18;
  202. break;
  203. case 'large':
  204. $point = 24;
  205. break;
  206. case 'giant':
  207. $point = 30;
  208. break;
  209. default:
  210. $point = 12;
  211. }
  212. return $point;
  213. }
  214. }