AbstractColor.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <?php
  2. namespace Intervention\Image;
  3. abstract class AbstractColor
  4. {
  5. /**
  6. * Initiates color object from integer
  7. *
  8. * @param integer $value
  9. * @return \Intervention\Image\AbstractColor
  10. */
  11. abstract public function initFromInteger($value);
  12. /**
  13. * Initiates color object from given array
  14. *
  15. * @param array $value
  16. * @return \Intervention\Image\AbstractColor
  17. */
  18. abstract public function initFromArray($value);
  19. /**
  20. * Initiates color object from given string
  21. *
  22. * @param string $value
  23. * @return \Intervention\Image\AbstractColor
  24. */
  25. abstract public function initFromString($value);
  26. /**
  27. * Initiates color object from given ImagickPixel object
  28. *
  29. * @param ImagickPixel $value
  30. * @return \Intervention\Image\AbstractColor
  31. */
  32. abstract public function initFromObject($value);
  33. /**
  34. * Initiates color object from given R, G and B values
  35. *
  36. * @param integer $r
  37. * @param integer $g
  38. * @param integer $b
  39. * @return \Intervention\Image\AbstractColor
  40. */
  41. abstract public function initFromRgb($r, $g, $b);
  42. /**
  43. * Initiates color object from given R, G, B and A values
  44. *
  45. * @param integer $r
  46. * @param integer $g
  47. * @param integer $b
  48. * @param float $a
  49. * @return \Intervention\Image\AbstractColor
  50. */
  51. abstract public function initFromRgba($r, $g, $b, $a);
  52. /**
  53. * Calculates integer value of current color instance
  54. *
  55. * @return integer
  56. */
  57. abstract public function getInt();
  58. /**
  59. * Calculates hexadecimal value of current color instance
  60. *
  61. * @param string $prefix
  62. * @return string
  63. */
  64. abstract public function getHex($prefix);
  65. /**
  66. * Calculates RGB(A) in array format of current color instance
  67. *
  68. * @return array
  69. */
  70. abstract public function getArray();
  71. /**
  72. * Calculates RGBA in string format of current color instance
  73. *
  74. * @return string
  75. */
  76. abstract public function getRgba();
  77. /**
  78. * Determines if current color is different from given color
  79. *
  80. * @param AbstractColor $color
  81. * @param integer $tolerance
  82. * @return boolean
  83. */
  84. abstract public function differs(AbstractColor $color, $tolerance = 0);
  85. /**
  86. * Creates new instance
  87. *
  88. * @param string $value
  89. */
  90. public function __construct($value = null)
  91. {
  92. $this->parse($value);
  93. }
  94. /**
  95. * Parses given value as color
  96. *
  97. * @param mixed $value
  98. * @return \Intervention\Image\AbstractColor
  99. */
  100. public function parse($value)
  101. {
  102. switch (true) {
  103. case is_string($value):
  104. $this->initFromString($value);
  105. break;
  106. case is_int($value):
  107. $this->initFromInteger($value);
  108. break;
  109. case is_array($value):
  110. $this->initFromArray($value);
  111. break;
  112. case is_object($value):
  113. $this->initFromObject($value);
  114. break;
  115. case is_null($value):
  116. $this->initFromArray(array(255, 255, 255, 0));
  117. break;
  118. default:
  119. throw new \Intervention\Image\Exception\NotReadableException(
  120. "Color format ({$value}) cannot be read."
  121. );
  122. }
  123. return $this;
  124. }
  125. /**
  126. * Formats current color instance into given format
  127. *
  128. * @param string $type
  129. * @return mixed
  130. */
  131. public function format($type)
  132. {
  133. switch (strtolower($type)) {
  134. case 'rgba':
  135. return $this->getRgba();
  136. case 'hex':
  137. return $this->getHex('#');
  138. case 'int':
  139. case 'integer':
  140. return $this->getInt();
  141. case 'array':
  142. return $this->getArray();
  143. case 'obj':
  144. case 'object':
  145. return $this;
  146. default:
  147. throw new \Intervention\Image\Exception\NotSupportedException(
  148. "Color format ({$type}) is not supported."
  149. );
  150. }
  151. }
  152. /**
  153. * Reads RGBA values from string into array
  154. *
  155. * @param string $value
  156. * @return array
  157. */
  158. protected function rgbaFromString($value)
  159. {
  160. $result = false;
  161. // parse color string in hexidecimal format like #cccccc or cccccc or ccc
  162. $hexPattern = '/^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i';
  163. // parse color string in format rgb(140, 140, 140)
  164. $rgbPattern = '/^rgb ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3})\)$/i';
  165. // parse color string in format rgba(255, 0, 0, 0.5)
  166. $rgbaPattern = '/^rgba ?\(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9.]{1,4})\)$/i';
  167. if (preg_match($hexPattern, $value, $matches)) {
  168. $result = array();
  169. $result[0] = strlen($matches[1]) == '1' ? hexdec($matches[1].$matches[1]) : hexdec($matches[1]);
  170. $result[1] = strlen($matches[2]) == '1' ? hexdec($matches[2].$matches[2]) : hexdec($matches[2]);
  171. $result[2] = strlen($matches[3]) == '1' ? hexdec($matches[3].$matches[3]) : hexdec($matches[3]);
  172. $result[3] = 1;
  173. } elseif (preg_match($rgbPattern, $value, $matches)) {
  174. $result = array();
  175. $result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
  176. $result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
  177. $result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0;
  178. $result[3] = 1;
  179. } elseif (preg_match($rgbaPattern, $value, $matches)) {
  180. $result = array();
  181. $result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
  182. $result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
  183. $result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0;
  184. $result[3] = ($matches[4] >= 0 && $matches[4] <= 1) ? $matches[4] : 0;
  185. } else {
  186. throw new \Intervention\Image\Exception\NotReadableException(
  187. "Unable to read color ({$value})."
  188. );
  189. }
  190. return $result;
  191. }
  192. }