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