Class AbstractGradientBuilder

java.lang.Object
com.itextpdf.kernel.colors.gradients.AbstractGradientBuilder
All Implemented Interfaces:
IGradientBuilder
Direct Known Subclasses:
AbstractLinearGradientBuilder, AbstractRadialGradientBuilder

public abstract class AbstractGradientBuilder extends Object implements IGradientBuilder
Base class for gradient builders implementations.
  • Constructor Details

    • AbstractGradientBuilder

      public AbstractGradientBuilder()
  • Method Details

    • addStopColor

      public IGradientBuilder addStopColor (GradientColorStop gradientColorStop)
      Adds the new color stop to the end (more info).

      Note: if the previously added color stop's offset would have greater offset than the added one, then the new offset would be normalized to be equal to the previous one. (Comparison made between relative on coordinates vector offsets. If any of them has the absolute offset, then the absolute value would be converted to relative first.)

      Specified by:
      addStopColor in interface IGradientBuilder
      Parameters:
      gradientColorStop - the gradient stop color to add
      Returns:
      the current builder instance
    • setSpread

      public IGradientBuilder setSpread (GradientSpreadMethod gradientSpreadMethod)
      Set the spread method to use for the gradient.
      Specified by:
      setSpread in interface IGradientBuilder
      Parameters:
      gradientSpreadMethod - the gradient spread method to set
      Returns:
      the current builder instance
    • setSvgLuminanceMode

      public IGradientBuilder setSvgLuminanceMode (boolean svgLuminanceMode)
      Sets whether to build a DeviceGray gradient using the SVG luminance coefficients. This method is SVG-specific because PDF and SVG use different luminance formulas. By default, gradients are built in DeviceRGB. The original RGB stop colors are not modified.
      Specified by:
      setSvgLuminanceMode in interface IGradientBuilder
      Parameters:
      svgLuminanceMode - true to build a luminance gradient, false to build an RGB gradient
      Returns:
      the current builder instance
    • getStopColors

      public List<GradientColorStop> getStopColors()
      Get the copy of current color stops list. Note that the stop colors are not copied here
      Returns:
      the copy of current stop colors list
    • getSpread

      public GradientSpreadMethod getSpread()
      Get the current spread method
      Returns:
      the current spread method
    • buildColor

      public Color buildColor (Rectangle targetBoundingBox, AffineTransform contextTransform, PdfDocument document)
      Builds the Color object representing the gradient with specified configuration that fills the target bounding box.
      Specified by:
      buildColor in interface IGradientBuilder
      Parameters:
      targetBoundingBox - the bounding box to be filled in current space
      contextTransform - the transformation from the base coordinates space into the current space. The null value is valid and can be used if there is no transformation from base coordinates to current space specified, or it is equal to identity transformation.
      document - the PdfDocument for which the linear gradient would be built.
      Returns:
      the constructed Color or null if no color to be applied or base gradient vector has been specified
    • getGradientVectorWithTransform

      protected Tuple2<T[],AffineTransform> getGradientVectorWithTransform (Rectangle targetBoundingBox, AffineTransform contextTransform)
      Returns the base gradient vector in gradient vector space and the current space to gradient vector space transformations that should be applied to the shading color.

      The vector would be set as shading coordinates vector and its length would be used to translate all color stops absolute offsets into the relatives.

      The transformation should be invertible as the current target bounding box coordinates should be transformed into the resulted shading space coordinates.

      Parameters:
      targetBoundingBox - the rectangle to be covered by constructed color in current space
      contextTransform - the current canvas transformation
      Returns:
      the pair of the vector, which is an array of exactly two elements specifying the gradient coordinates vector, and the additional transformation to be concatenated to the current for resulted shading or null if no additional transformation is specified
    • computeCoveringDomain

      protected abstract double[] computeCoveringDomain (T[] coords, Rectangle toCover)
      Computes the minimal domain that covers the box with vector normals. The domain corresponding to the initial vector is [0, 1].
      Parameters:
      coords - the array of exactly two elements that describe the base vector (corresponding to [0,1] domain, that need to be adjusted to cover the box
      toCover - the box that needs to be covered
      Returns:
      the array of two elements in ascending order specifying the calculated covering domain
    • createCoordsForNewDomain

      protected abstract T[] createCoordsForNewDomain (double[] newDomain, T[] baseVector)
      Expand the base vector to cover the new domain
      Parameters:
      newDomain - the array of exactly two elements that specifies the domain that should be covered by the created vector
      baseVector - the array of exactly two elements that specifies the base vector which corresponds to [0, 1] domain
      Returns:
      the array of two
    • createCoveringCoordinates

      protected abstract T[] createCoveringCoordinates (Rectangle targetBoundingBox)
      Creates some gradient vector which would cover the target bounding box.
      Parameters:
      targetBoundingBox - the bounding box to cover
      Returns:
      constructed gradient vector
    • getBaseVectorLength

      protected abstract double getBaseVectorLength (T[] coordinates)
      Evaluates base vector length for future color stops spreading.
      Parameters:
      coordinates - the base vector
      Returns:
      base vector length
    • createCoordsDictEntry

      protected abstract PdfArray createCoordsDictEntry (T[] coordinates)
      Converts gradient vector into pdf coordinates array for shading.
      Parameters:
      coordinates - vector to convert
      Returns:
      constructed pdf array
    • createPdfShading

      protected abstract AbstractPdfShading createPdfShading (PdfColorSpace colorSpace, PdfArray coordinates, PdfArray coordinatesDomain, IPdfFunction stopsFunction)
      Creates the gradient shading object.
      Parameters:
      colorSpace - colorspace to use
      coordinates - gradient coordinates
      coordinatesDomain - gradient coordinates domain
      stopsFunction - color stops function
      Returns:
      constructed gradient shading
    • getToIntervalTransform

      protected static AffineTransform getToIntervalTransform (Point domainStart, Point domainEnd, double scale)
      Constructs AffineTransform which represents transformation from current coordinates into the coordinates in which domainStart would be (0,0), domain end would be on X axes (i.e. (X,0) coordinate) and the surface scaled both on X and Y by scale factor.
      Parameters:
      domainStart - the point to become (0,0)
      domainEnd - the point to be placed on X axes
      scale - the scale factor
      Returns:
      the constructed transformation
    • isZero

      protected static boolean isZero (double d)
      Checks whether the argument is close to 0
      Parameters:
      d - to check
      Returns:
      true if the argument is close to 0