/quizbd-master-main/vendor/intervention/image/src/Intervention/Image/Imagick/Color.php

https://gitlab.com/IR31121994/quizbd-master · PHP · 279 lines · 129 code · 32 blank · 118 comment · 5 complexity · 5d5b9f526ea9f15b32d27b387330d103 MD5 · raw file

  1. <?php
  2. namespace Intervention\Image\Imagick;
  3. use Intervention\Image\AbstractColor;
  4. class Color extends AbstractColor
  5. {
  6. /**
  7. * ImagickPixel containing current color information
  8. *
  9. * @var \ImagickPixel
  10. */
  11. public $pixel;
  12. /**
  13. * Initiates color object from integer
  14. *
  15. * @param int $value
  16. * @return \Intervention\Image\AbstractColor
  17. */
  18. public function initFromInteger($value)
  19. {
  20. $a = ($value >> 24) & 0xFF;
  21. $r = ($value >> 16) & 0xFF;
  22. $g = ($value >> 8) & 0xFF;
  23. $b = $value & 0xFF;
  24. $a = $this->rgb2alpha($a);
  25. $this->setPixel($r, $g, $b, $a);
  26. }
  27. /**
  28. * Initiates color object from given array
  29. *
  30. * @param array $value
  31. * @return \Intervention\Image\AbstractColor
  32. */
  33. public function initFromArray($array)
  34. {
  35. $array = array_values($array);
  36. if (count($array) == 4) {
  37. // color array with alpha value
  38. list($r, $g, $b, $a) = $array;
  39. } elseif (count($array) == 3) {
  40. // color array without alpha value
  41. list($r, $g, $b) = $array;
  42. $a = 1;
  43. }
  44. $this->setPixel($r, $g, $b, $a);
  45. }
  46. /**
  47. * Initiates color object from given string
  48. *
  49. * @param string $value
  50. *
  51. * @return \Intervention\Image\AbstractColor
  52. */
  53. public function initFromString($value)
  54. {
  55. if ($color = $this->rgbaFromString($value)) {
  56. $this->setPixel($color[0], $color[1], $color[2], $color[3]);
  57. }
  58. }
  59. /**
  60. * Initiates color object from given ImagickPixel object
  61. *
  62. * @param ImagickPixel $value
  63. *
  64. * @return \Intervention\Image\AbstractColor
  65. */
  66. public function initFromObject($value)
  67. {
  68. if (is_a($value, '\ImagickPixel')) {
  69. $this->pixel = $value;
  70. }
  71. }
  72. /**
  73. * Initiates color object from given R, G and B values
  74. *
  75. * @param int $r
  76. * @param int $g
  77. * @param int $b
  78. *
  79. * @return \Intervention\Image\AbstractColor
  80. */
  81. public function initFromRgb($r, $g, $b)
  82. {
  83. $this->setPixel($r, $g, $b);
  84. }
  85. /**
  86. * Initiates color object from given R, G, B and A values
  87. *
  88. * @param int $r
  89. * @param int $g
  90. * @param int $b
  91. * @param float $a
  92. *
  93. * @return \Intervention\Image\AbstractColor
  94. */
  95. public function initFromRgba($r, $g, $b, $a)
  96. {
  97. $this->setPixel($r, $g, $b, $a);
  98. }
  99. /**
  100. * Calculates integer value of current color instance
  101. *
  102. * @return int
  103. */
  104. public function getInt()
  105. {
  106. $r = $this->getRedValue();
  107. $g = $this->getGreenValue();
  108. $b = $this->getBlueValue();
  109. $a = intval(round($this->getAlphaValue() * 255));
  110. return intval(($a << 24) + ($r << 16) + ($g << 8) + $b);
  111. }
  112. /**
  113. * Calculates hexadecimal value of current color instance
  114. *
  115. * @param string $prefix
  116. *
  117. * @return string
  118. */
  119. public function getHex($prefix = '')
  120. {
  121. return sprintf('%s%02x%02x%02x', $prefix,
  122. $this->getRedValue(),
  123. $this->getGreenValue(),
  124. $this->getBlueValue()
  125. );
  126. }
  127. /**
  128. * Calculates RGB(A) in array format of current color instance
  129. *
  130. * @return array
  131. */
  132. public function getArray()
  133. {
  134. return [
  135. $this->getRedValue(),
  136. $this->getGreenValue(),
  137. $this->getBlueValue(),
  138. $this->getAlphaValue()
  139. ];
  140. }
  141. /**
  142. * Calculates RGBA in string format of current color instance
  143. *
  144. * @return string
  145. */
  146. public function getRgba()
  147. {
  148. return sprintf('rgba(%d, %d, %d, %.2F)',
  149. $this->getRedValue(),
  150. $this->getGreenValue(),
  151. $this->getBlueValue(),
  152. $this->getAlphaValue()
  153. );
  154. }
  155. /**
  156. * Determines if current color is different from given color
  157. *
  158. * @param AbstractColor $color
  159. * @param int $tolerance
  160. * @return boolean
  161. */
  162. public function differs(AbstractColor $color, $tolerance = 0)
  163. {
  164. $color_tolerance = round($tolerance * 2.55);
  165. $alpha_tolerance = round($tolerance);
  166. $delta = [
  167. 'r' => abs($color->getRedValue() - $this->getRedValue()),
  168. 'g' => abs($color->getGreenValue() - $this->getGreenValue()),
  169. 'b' => abs($color->getBlueValue() - $this->getBlueValue()),
  170. 'a' => abs($color->getAlphaValue() - $this->getAlphaValue())
  171. ];
  172. return (
  173. $delta['r'] > $color_tolerance or
  174. $delta['g'] > $color_tolerance or
  175. $delta['b'] > $color_tolerance or
  176. $delta['a'] > $alpha_tolerance
  177. );
  178. }
  179. /**
  180. * Returns RGB red value of current color
  181. *
  182. * @return int
  183. */
  184. public function getRedValue()
  185. {
  186. return intval(round($this->pixel->getColorValue(\Imagick::COLOR_RED) * 255));
  187. }
  188. /**
  189. * Returns RGB green value of current color
  190. *
  191. * @return int
  192. */
  193. public function getGreenValue()
  194. {
  195. return intval(round($this->pixel->getColorValue(\Imagick::COLOR_GREEN) * 255));
  196. }
  197. /**
  198. * Returns RGB blue value of current color
  199. *
  200. * @return int
  201. */
  202. public function getBlueValue()
  203. {
  204. return intval(round($this->pixel->getColorValue(\Imagick::COLOR_BLUE) * 255));
  205. }
  206. /**
  207. * Returns RGB alpha value of current color
  208. *
  209. * @return float
  210. */
  211. public function getAlphaValue()
  212. {
  213. return round($this->pixel->getColorValue(\Imagick::COLOR_ALPHA), 2);
  214. }
  215. /**
  216. * Initiates ImagickPixel from given RGBA values
  217. *
  218. * @return \ImagickPixel
  219. */
  220. private function setPixel($r, $g, $b, $a = null)
  221. {
  222. $a = is_null($a) ? 1 : $a;
  223. return $this->pixel = new \ImagickPixel(
  224. sprintf('rgba(%d, %d, %d, %.2F)', $r, $g, $b, $a)
  225. );
  226. }
  227. /**
  228. * Returns current color as ImagickPixel
  229. *
  230. * @return \ImagickPixel
  231. */
  232. public function getPixel()
  233. {
  234. return $this->pixel;
  235. }
  236. /**
  237. * Calculates RGA integer alpha value into float value
  238. *
  239. * @param int $value
  240. * @return float
  241. */
  242. private function rgb2alpha($value)
  243. {
  244. // (255 -> 1.0) / (0 -> 0.0)
  245. return (float) round($value/255, 2);
  246. }
  247. }