Example #1
0
 /**
  * Allocate a new color.  Allocate with GD as needed and store
  * previously allocated colors in $this->_colors.
  *
  * @param array $color The new current color
  * @return int           The allocated color
  */
 private function _allocate_color($color)
 {
     if (isset($color["c"])) {
         $color = Helpers::cmyk_to_rgb($color);
     }
     // Full opacity if no alpha set
     if (!isset($color[3])) {
         $color[3] = 0;
     }
     list($r, $g, $b, $a) = $color;
     $r *= 255;
     $g *= 255;
     $b *= 255;
     $a *= 127;
     // Clip values
     $r = $r > 255 ? 255 : $r;
     $g = $g > 255 ? 255 : $g;
     $b = $b > 255 ? 255 : $b;
     $a = $a > 127 ? 127 : $a;
     $r = $r < 0 ? 0 : $r;
     $g = $g < 0 ? 0 : $g;
     $b = $b < 0 ? 0 : $b;
     $a = $a < 0 ? 0 : $a;
     $key = sprintf("#%02X%02X%02X%02X", $r, $g, $b, $a);
     if (isset($this->_colors[$key])) {
         return $this->_colors[$key];
     }
     if ($a != 0) {
         $this->_colors[$key] = imagecolorallocatealpha($this->get_image(), $r, $g, $b, $a);
     } else {
         $this->_colors[$key] = imagecolorallocate($this->get_image(), $r, $g, $b);
     }
     return $this->_colors[$key];
 }