示例#1
0
 /**
  * Return gradient URL
  *
  * Creates the definitions needed for a gradient, if a proper gradient does
  * not yet exists. In each case a URL referencing the correct gradient will
  * be returned.
  * 
  * @param ezcGraphColor $color Gradient
  * @return string Gradient URL
  */
 protected function getGradientUrl(ezcGraphColor $color)
 {
     switch (true) {
         case $color instanceof ezcGraphLinearGradient:
             if (!in_array($color->__toString(), $this->drawnGradients, true)) {
                 $gradient = $this->dom->createElement('linearGradient');
                 $gradient->setAttribute('id', 'Definition_' . $color->__toString());
                 $this->defs->appendChild($gradient);
                 // Start of linear gradient
                 $stop = $this->dom->createElement('stop');
                 $stop->setAttribute('offset', 0);
                 $stop->setAttribute('style', sprintf('stop-color: #%02x%02x%02x; stop-opacity: %.2F;', $color->startColor->red, $color->startColor->green, $color->startColor->blue, 1 - $color->startColor->alpha / 255));
                 $gradient->appendChild($stop);
                 // End of linear gradient
                 $stop = $this->dom->createElement('stop');
                 $stop->setAttribute('offset', 1);
                 $stop->setAttribute('style', sprintf('stop-color: #%02x%02x%02x; stop-opacity: %.2F;', $color->endColor->red, $color->endColor->green, $color->endColor->blue, 1 - $color->endColor->alpha / 255));
                 $gradient->appendChild($stop);
                 $gradient = $this->dom->createElement('linearGradient');
                 $gradient->setAttribute('id', $color->__toString());
                 $gradient->setAttribute('x1', sprintf('%.4F', $color->startPoint->x));
                 $gradient->setAttribute('y1', sprintf('%.4F', $color->startPoint->y));
                 $gradient->setAttribute('x2', sprintf('%.4F', $color->endPoint->x));
                 $gradient->setAttribute('y2', sprintf('%.4F', $color->endPoint->y));
                 $gradient->setAttribute('gradientUnits', 'userSpaceOnUse');
                 $gradient->setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#Definition_' . $color->__toString());
                 $this->defs->appendChild($gradient);
                 $this->drawnGradients[] = $color->__toString();
             }
             return sprintf('url(#%s)', $color->__toString());
         case $color instanceof ezcGraphRadialGradient:
             if (!in_array($color->__toString(), $this->drawnGradients, true)) {
                 $gradient = $this->dom->createElement('linearGradient');
                 $gradient->setAttribute('id', 'Definition_' . $color->__toString());
                 $this->defs->appendChild($gradient);
                 // Start of linear gradient
                 $stop = $this->dom->createElement('stop');
                 $stop->setAttribute('offset', 0);
                 $stop->setAttribute('style', sprintf('stop-color: #%02x%02x%02x; stop-opacity: %.2F;', $color->startColor->red, $color->startColor->green, $color->startColor->blue, 1 - $color->startColor->alpha / 255));
                 $gradient->appendChild($stop);
                 // End of linear gradient
                 $stop = $this->dom->createElement('stop');
                 $stop->setAttribute('offset', 1);
                 $stop->setAttribute('style', sprintf('stop-color: #%02x%02x%02x; stop-opacity: %.2F;', $color->endColor->red, $color->endColor->green, $color->endColor->blue, 1 - $color->endColor->alpha / 255));
                 $gradient->appendChild($stop);
                 $gradient = $this->dom->createElement('radialGradient');
                 $gradient->setAttribute('id', $color->__toString());
                 $gradient->setAttribute('cx', sprintf('%.4F', $color->center->x));
                 $gradient->setAttribute('cy', sprintf('%.4F', $color->center->y));
                 $gradient->setAttribute('fx', sprintf('%.4F', $color->center->x));
                 $gradient->setAttribute('fy', sprintf('%.4F', $color->center->y));
                 $gradient->setAttribute('r', max($color->height, $color->width));
                 $gradient->setAttribute('gradientUnits', 'userSpaceOnUse');
                 $gradient->setAttributeNS('http://www.w3.org/1999/xlink', 'xlink:href', '#Definition_' . $color->__toString());
                 $this->defs->appendChild($gradient);
                 $this->drawnGradients[] = $color->__toString();
             }
             return sprintf('url(#%s)', $color->__toString());
         default:
             return false;
     }
 }