| Total Complexity | 45 |
| Total Lines | 653 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Complex classes like ImageColor often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ImageColor, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 54 | class ImageColor |
||
| 55 | { |
||
| 56 | /** |
||
| 57 | * First color that the class handles for ranges and mixes. |
||
| 58 | * |
||
| 59 | * @var array |
||
| 60 | * @access public |
||
| 61 | * @see setColors() |
||
| 62 | */ |
||
| 63 | public $color1 = []; |
||
| 64 | /** |
||
| 65 | * Second color that the class handles for ranges and mixes. |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | * @access public |
||
| 69 | * @see setColors() |
||
| 70 | */ |
||
| 71 | public $color2 = []; |
||
| 72 | /** |
||
| 73 | * Boolean value for determining whether colors outputted should be limited |
||
| 74 | * to the web safe pallet or not. |
||
| 75 | * |
||
| 76 | * @var bool |
||
| 77 | * @access private |
||
| 78 | * @see setWebSafe() |
||
| 79 | */ |
||
| 80 | private $websafeb = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Mix two colors together by finding their average. If the colors are not |
||
| 84 | * passed as parameters, the class's colors will be mixed instead. |
||
| 85 | * |
||
| 86 | * @param bool|string $col1 The first color you want to mix |
||
| 87 | * @param bool|string $col2 The second color you want to mix |
||
| 88 | * |
||
| 89 | * @return string The mixed color. |
||
| 90 | * @access public |
||
| 91 | * @author Jason Lotito <[email protected]> |
||
| 92 | * @uses setInternalColors() to assign the colors if any are passed to the |
||
| 93 | * class. |
||
| 94 | */ |
||
| 95 | public function mixColors($col1 = false, $col2 = false) |
||
| 96 | { |
||
| 97 | if ($col1) { |
||
| 98 | $this->setInternalColors($col1, $col2); |
||
|
|
|||
| 99 | } |
||
| 100 | |||
| 101 | // after finding the average, it will be a float. add 0.5 and then |
||
| 102 | // cast to an integer to properly round it to an integer. |
||
| 103 | $color3[0] = (int)((($this->color1[0] + $this->color2[0]) / 2) + 0.5); |
||
| 104 | $color3[1] = (int)((($this->color1[1] + $this->color2[1]) / 2) + 0.5); |
||
| 105 | $color3[2] = (int)((($this->color1[2] + $this->color2[2]) / 2) + 0.5); |
||
| 106 | |||
| 107 | if ($this->websafeb) { |
||
| 108 | \array_walk($color3, '\_makeWebSafe'); |
||
| 109 | } |
||
| 110 | |||
| 111 | return $this->rgb2hex($color3); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Determines whether colors the returned by this class will be rounded to |
||
| 116 | * the nearest web safe value. |
||
| 117 | * |
||
| 118 | * @param bool $bool Indicates if colors should be limited to the |
||
| 119 | * websafe pallet. |
||
| 120 | * |
||
| 121 | * @access public |
||
| 122 | * @author Jason Lotito <[email protected]> |
||
| 123 | */ |
||
| 124 | public function setWebSafe($bool = true) |
||
| 125 | { |
||
| 126 | $this->websafeb = (bool)$bool; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Set the two colors this class uses for mixing and ranges. |
||
| 131 | * |
||
| 132 | * @param string $col1 The first color in hex format |
||
| 133 | * @param string $col2 The second color in hex format |
||
| 134 | * |
||
| 135 | * @access public |
||
| 136 | * @author Jason Lotito <[email protected]> |
||
| 137 | */ |
||
| 138 | public function setColors($col1, $col2) |
||
| 139 | { |
||
| 140 | $this->setInternalColors($col1, $col2); |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Get the range of colors between the class's two colors, given a degree. |
||
| 145 | * |
||
| 146 | * @param int $degrees How large a 'step' we should take between the |
||
| 147 | * colors. |
||
| 148 | * |
||
| 149 | * @return array Returns an array of hex strings, one element for each |
||
| 150 | * color. |
||
| 151 | * @access public |
||
| 152 | * @author Jason Lotito <[email protected]> |
||
| 153 | * @todo Allow for degrees for individual parts of the colors. |
||
| 154 | */ |
||
| 155 | public function getRange($degrees = 2) |
||
| 156 | { |
||
| 157 | if (0 == $degrees) { |
||
| 158 | $degrees = 1; |
||
| 159 | } |
||
| 160 | |||
| 161 | // The degrees give us how much we should advance each color at each |
||
| 162 | // phase of the loop. This way, the advance is equal throughout all |
||
| 163 | // the colors. |
||
| 164 | |||
| 165 | $red_steps = ($this->color2[0] - $this->color1[0]) / $degrees; |
||
| 166 | $green_steps = ($this->color2[1] - $this->color1[1]) / $degrees; |
||
| 167 | $blue_steps = ($this->color2[2] - $this->color1[2]) / $degrees; |
||
| 168 | |||
| 169 | $allcolors = []; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * The loop stops once any color has gone beyond the end color. |
||
| 173 | */ |
||
| 174 | |||
| 175 | // Loop through all the degrees between the colors |
||
| 176 | for ($x = 0; $x < $degrees; ++$x) { |
||
| 177 | $col[0] = $red_steps * $x; |
||
| 178 | $col[1] = $green_steps * $x; |
||
| 179 | $col[2] = $blue_steps * $x; |
||
| 180 | |||
| 181 | // Loop through each R, G, and B |
||
| 182 | for ($i = 0; $i < 3; ++$i) { |
||
| 183 | $partcolor = $this->color1[$i] + $col[$i]; |
||
| 184 | // If the color is less than 256 |
||
| 185 | if ($partcolor < 256) { |
||
| 186 | // Makes sure the colors is not less than 0 |
||
| 187 | if ($partcolor > -1) { |
||
| 188 | $newcolor[$i] = $partcolor; |
||
| 189 | } else { |
||
| 190 | $newcolor[$i] = 0; |
||
| 191 | } |
||
| 192 | // Color was greater than 255 |
||
| 193 | } else { |
||
| 194 | $newcolor[$i] = 255; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | |||
| 198 | if ($this->websafeb) { |
||
| 199 | \array_walk($newcolor, '\_makeWebSafe'); |
||
| 200 | } |
||
| 201 | |||
| 202 | $allcolors[] = $this->rgb2hex($newcolor); |
||
| 203 | } |
||
| 204 | |||
| 205 | return $allcolors; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Change the lightness of the class's two colors. |
||
| 210 | * |
||
| 211 | * @param int $degree The degree of the change. Positive values |
||
| 212 | * lighten the color while negative values will darken it. |
||
| 213 | * |
||
| 214 | * @access public |
||
| 215 | * @author Jason Lotito <[email protected]> |
||
| 216 | * @uses ImageColor::$color1 as an input and return value. |
||
| 217 | * @uses ImageColor::$color2 as an input and return value. |
||
| 218 | */ |
||
| 219 | public function changeLightness($degree = 10) |
||
| 220 | { |
||
| 221 | $color1 = &$this->color1; |
||
| 222 | $color2 = &$this->color2; |
||
| 223 | |||
| 224 | for ($x = 0; $x < 3; ++$x) { |
||
| 225 | if (($color1[$x] + $degree) < 256) { |
||
| 226 | if (($color1[$x] + $degree) > -1) { |
||
| 227 | $color1[$x] += $degree; |
||
| 228 | } else { |
||
| 229 | $color1[$x] = 0; |
||
| 230 | } |
||
| 231 | } else { |
||
| 232 | $color1[$x] = 255; |
||
| 233 | } |
||
| 234 | |||
| 235 | if (($color2[$x] + $degree) < 256) { |
||
| 236 | if (($color2[$x] + $degree) > -1) { |
||
| 237 | $color2[$x] += $degree; |
||
| 238 | } else { |
||
| 239 | $color2[$x] = 0; |
||
| 240 | } |
||
| 241 | } else { |
||
| 242 | $color2[$x] = 255; |
||
| 243 | } |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Determine if a light or dark text color would be more readable on a |
||
| 249 | * background of a given color. This is determined by the G(reen) value of |
||
| 250 | * RGB. You can change the dark and the light colors from their default |
||
| 251 | * black and white. |
||
| 252 | * |
||
| 253 | * @param string|array $color The hex color to analyze |
||
| 254 | * @param string $light The light color value to return if we should |
||
| 255 | * have light text. |
||
| 256 | * @param string $dark The dark color value to return if we should have |
||
| 257 | * dark text. |
||
| 258 | * |
||
| 259 | * @return string The light or dark value which would make the text most |
||
| 260 | * readable. |
||
| 261 | * @access public |
||
| 262 | * @static |
||
| 263 | * @author Jason Lotito <[email protected]> |
||
| 264 | */ |
||
| 265 | public function getTextColor($color, $light = '#FFFFFF', $dark = '#000000') |
||
| 266 | { |
||
| 267 | $color = $this->splitColor($color); |
||
| 268 | if ($color[1] > \hexdec('66')) { |
||
| 269 | return $dark; |
||
| 270 | } |
||
| 271 | |||
| 272 | return $light; |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Internal method to set the colors. |
||
| 277 | * |
||
| 278 | * @param string $col1 First color, either a name or hex value |
||
| 279 | * @param string $col2 Second color, either a name or hex value |
||
| 280 | * |
||
| 281 | * @access private |
||
| 282 | * @author Jason Lotito <[email protected]> |
||
| 283 | */ |
||
| 284 | private function setInternalColors($col1, $col2) |
||
| 285 | { |
||
| 286 | if ($col1) { |
||
| 287 | $this->color1 = $this->splitColor($col1); |
||
| 288 | } |
||
| 289 | if ($col2) { |
||
| 290 | $this->color2 = $this->splitColor($col2); |
||
| 291 | } |
||
| 292 | } |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Given a color, properly split it up into a 3 element RGB array. |
||
| 296 | * |
||
| 297 | * @param string $color The color. |
||
| 298 | * |
||
| 299 | * @return array A three element RGB array. |
||
| 300 | * @access private |
||
| 301 | * @static |
||
| 302 | * @author Jason Lotito <[email protected]> |
||
| 303 | */ |
||
| 304 | private function splitColor($color) |
||
| 305 | { |
||
| 306 | $color = \str_replace('#', '', $color); |
||
| 307 | $c[] = \hexdec(\mb_substr($color, 0, 2)); |
||
| 308 | $c[] = \hexdec(\mb_substr($color, 2, 2)); |
||
| 309 | $c[] = \hexdec(\mb_substr($color, 4, 2)); |
||
| 310 | |||
| 311 | return $c; |
||
| 312 | } |
||
| 313 | |||
| 314 | /** |
||
| 315 | * This is deprecated. Use rgb2hex() instead. |
||
| 316 | * |
||
| 317 | * @access private |
||
| 318 | * @param $color |
||
| 319 | * |
||
| 320 | * @return string |
||
| 321 | * @deprecated Function deprecated after 1.0.1 |
||
| 322 | * @see rgb2hex(). |
||
| 323 | * |
||
| 324 | */ |
||
| 325 | private function returnColor($color) |
||
| 326 | { |
||
| 327 | return $this->rgb2hex($color); |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Convert an RGB array to a hex string. |
||
| 332 | * |
||
| 333 | * @param array $color 3 element RGB array. |
||
| 334 | * |
||
| 335 | * @return string Hex color string. |
||
| 336 | * @access public |
||
| 337 | * @static |
||
| 338 | * @author Jason Lotito <[email protected]> |
||
| 339 | * @see hex2rgb() |
||
| 340 | */ |
||
| 341 | public function rgb2hex($color) |
||
| 342 | { |
||
| 343 | return \sprintf('%02X%02X%02X', $color[0], $color[1], $color[2]); |
||
| 344 | } |
||
| 345 | |||
| 346 | /** |
||
| 347 | * Convert a hex color string into an RGB array. An extra fourth element |
||
| 348 | * will be returned with the original hex value. |
||
| 349 | * |
||
| 350 | * @param string $hex Hex color string. |
||
| 351 | * |
||
| 352 | * @return array RGB color array with an extra 'hex' element containing |
||
| 353 | * the original hex string. |
||
| 354 | * @access public |
||
| 355 | * @static |
||
| 356 | * @author Jason Lotito <[email protected]> |
||
| 357 | * @see rgb2hex() |
||
| 358 | */ |
||
| 359 | public function hex2rgb($hex) |
||
| 360 | { |
||
| 361 | $return = $this->splitColor($hex); |
||
| 362 | $return['hex'] = $hex; |
||
| 363 | |||
| 364 | return $return; |
||
| 365 | } |
||
| 366 | |||
| 367 | /** |
||
| 368 | * Convert an HSV (Hue, Saturation, Brightness) value to RGB. |
||
| 369 | * |
||
| 370 | * @param int $h Hue |
||
| 371 | * @param int $s Saturation |
||
| 372 | * @param int $v Brightness |
||
| 373 | * |
||
| 374 | * @return array RGB array. |
||
| 375 | * @access public |
||
| 376 | * @static |
||
| 377 | * @author Jason Lotito <[email protected]> |
||
| 378 | * @uses hsv2hex() to convert the HSV value to Hex. |
||
| 379 | * @uses hex2rgb() to convert the Hex value to RGB. |
||
| 380 | */ |
||
| 381 | public function hsv2rgb($h, $s, $v) |
||
| 382 | { |
||
| 383 | return $this->hex2rgb($this->hsv2hex($h, $s, $v)); |
||
| 384 | } |
||
| 385 | |||
| 386 | /** |
||
| 387 | * Convert HSV (Hue, Saturation, Brightness) to a hex color string. |
||
| 388 | * |
||
| 389 | * Originally written by Jurgen Schwietering. Integrated into the class by |
||
| 390 | * Jason Lotito. |
||
| 391 | * |
||
| 392 | * @param int $h Hue |
||
| 393 | * @param int $s Saturation |
||
| 394 | * @param int $v Brightness |
||
| 395 | * |
||
| 396 | * @return string The hex string. |
||
| 397 | * @access public |
||
| 398 | * @static |
||
| 399 | * @author Jurgen Schwietering <[email protected]> |
||
| 400 | * @uses rgb2hex() to convert the return value to a hex string. |
||
| 401 | */ |
||
| 402 | public function hsv2hex($h, $s, $v) |
||
| 403 | { |
||
| 404 | $s /= 256.0; |
||
| 405 | $v /= 256.0; |
||
| 406 | if (0.0 == $s) { |
||
| 407 | $r = $g = $b = $v; |
||
| 408 | |||
| 409 | return ''; |
||
| 410 | } |
||
| 411 | $h = $h / 256.0 * 6.0; |
||
| 412 | $i = \floor($h); |
||
| 413 | $f = $h - $i; |
||
| 414 | |||
| 415 | $v *= 256.0; |
||
| 416 | $p = (int)($v * (1.0 - $s)); |
||
| 417 | $q = (int)($v * (1.0 - $s * $f)); |
||
| 418 | $t = (int)($v * (1.0 - $s * (1.0 - $f))); |
||
| 419 | switch ($i) { |
||
| 420 | case 0: |
||
| 421 | $r = $v; |
||
| 422 | $g = $t; |
||
| 423 | $b = $p; |
||
| 424 | break; |
||
| 425 | case 1: |
||
| 426 | $r = $q; |
||
| 427 | $g = $v; |
||
| 428 | $b = $p; |
||
| 429 | break; |
||
| 430 | case 2: |
||
| 431 | $r = $p; |
||
| 432 | $g = $v; |
||
| 433 | $b = $t; |
||
| 434 | break; |
||
| 435 | case 3: |
||
| 436 | $r = $p; |
||
| 437 | $g = $q; |
||
| 438 | $b = $v; |
||
| 439 | break; |
||
| 440 | case 4: |
||
| 441 | $r = $t; |
||
| 442 | $g = $p; |
||
| 443 | $b = $v; |
||
| 444 | break; |
||
| 445 | default: |
||
| 446 | $r = $v; |
||
| 447 | $g = $p; |
||
| 448 | $b = $q; |
||
| 449 | break; |
||
| 450 | } |
||
| 451 | |||
| 452 | return $this->rgb2hex([$r, $g, $b]); |
||
| 453 | } |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Allocates a color in the given image. |
||
| 457 | * |
||
| 458 | * User defined color specifications get translated into an array of RGB |
||
| 459 | * values. |
||
| 460 | * |
||
| 461 | * @param resource $img Image handle |
||
| 462 | * @param string|array $color Name or hex string or an RGB array. |
||
| 463 | * |
||
| 464 | * @return bool Image color handle. |
||
| 465 | * @access public |
||
| 466 | * @static |
||
| 467 | * @uses imagefilledarc() to allocate the color. |
||
| 468 | * @uses color2RGB() to parse the color into RGB values. |
||
| 469 | */ |
||
| 470 | public function allocateColor($img, $color) |
||
| 471 | { |
||
| 472 | $color = $this->color2RGB($color); |
||
| 473 | |||
| 474 | return \imagefilledarc($img, $color[0], $color[1], $color[2]); |
||
| 475 | } |
||
| 476 | |||
| 477 | /** |
||
| 478 | * Convert a named or hex color string to an RGB array. If the color begins |
||
| 479 | * with the # character it will be treated as a hex value. Everything else |
||
| 480 | * will be treated as a named color. If the named color is not known, black |
||
| 481 | * will be returned. |
||
| 482 | * |
||
| 483 | * @param string $color |
||
| 484 | * |
||
| 485 | * @return array RGB color |
||
| 486 | * @access public |
||
| 487 | * @static |
||
| 488 | * @author Laurent Laville <[email protected]> |
||
| 489 | * @uses hex2rgb() to convert colors begining with the # character. |
||
| 490 | * @uses namedColor2RGB() to convert everything not starting with a #. |
||
| 491 | */ |
||
| 492 | public function color2RGB($color) |
||
| 503 | } |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Convert a named color to an RGB array. If the color is unknown black |
||
| 507 | * is returned. |
||
| 508 | * |
||
| 509 | * @param string $color Case insensitive color name. |
||
| 510 | * |
||
| 511 | * @return array RGB color array. If the color was unknown, the result |
||
| 512 | * will be black. |
||
| 513 | * @access public |
||
| 514 | * @static |
||
| 515 | * @author Sebastian Bergmann <[email protected]> |
||
| 516 | */ |
||
| 517 | public function namedColor2RGB($color) |
||
| 673 | } |
||
| 674 | |||
| 675 | /** |
||
| 676 | * Convert an RGB percentage string into an RGB array. |
||
| 677 | * |
||
| 678 | * @param string|array $color Percentage color string like "50%,20%,100%". |
||
| 679 | * |
||
| 680 | * @return array RGB color array. |
||
| 681 | * @access public |
||
| 682 | * @static |
||
| 683 | */ |
||
| 684 | public function percentageColor2RGB($color) |
||
| 707 | } |
||
| 708 | } |
||
| 709 | |||
| 710 | // For Array Walk |
||
| 711 | // {{{ |
||
| 712 | /** |
||
| 713 | * Function for array_walk() to round colors to the closest web safe value. |
||
| 742 |