Class AbstractLinearGradientBuilder
- java.lang.Object
-
- com.itextpdf.kernel.colors.gradients.AbstractLinearGradientBuilder
-
- Direct Known Subclasses:
LinearGradientBuilder
,StrategyBasedLinearGradientBuilder
public abstract class AbstractLinearGradientBuilder extends java.lang.Object
Base class for linear gradient builders implementations.Color transitions for linear gradients are defined by a series of color stops along a gradient vector. A gradient normal defines how the colors in a vector are painted to the surface. For a linear gradient, a normal is a line perpendicular to the vector.
Contains the main logic that works with stop colors and creation of the resulted pdf color object.
-
-
Field Summary
Fields Modifier and Type Field Description private GradientSpreadMethod
spreadMethod
private java.util.List<GradientColorStop>
stops
protected static double
ZERO_EPSILON
The epsilon value used for data creation
-
Constructor Summary
Constructors Constructor Description AbstractLinearGradientBuilder()
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description AbstractLinearGradientBuilder
addColorStop(GradientColorStop gradientColorStop)
Adds the new color stop to the end (more info
).private static java.util.List<GradientColorStop>
adjustNormalizedStopsToCoverDomain(java.util.List<GradientColorStop> normalizedStops, double[] targetDomain, GradientSpreadMethod spreadMethod)
private static void
adjustStopsForPadIfNeeded(java.util.List<GradientColorStop> stopsToConstruct, double[] coordinatesDomain)
Color
buildColor(Rectangle targetBoundingBox, AffineTransform contextTransform, PdfDocument document)
Builds theColor
object representing the linear gradient with specified configuration that fills the target bounding box.private static IPdfFunction
constructFunction(java.util.List<GradientColorStop> toConstruct)
private static AbstractPdfFunction<? extends PdfDictionary>
constructSingleGradientSegmentFunction(GradientColorStop from, GradientColorStop to)
private static java.util.List<GradientColorStop>
copyStopsAndNormalizeAbsoluteOffsets(java.util.List<GradientColorStop> toNormalize, double baseVectorLength)
private static PdfAxialShading
createAxialShading(Point[] baseCoordinatesVector, java.util.List<GradientColorStop> stops, GradientSpreadMethod spreadMethod, Rectangle targetBoundingBox)
protected static Point[]
createCoordinatesForNewDomain(double[] newDomain, Point[] baseVector)
Expand the base vector to cover the new domainprivate static PdfArray
createCoordsPdfArray(Point[] coordsPoints)
protected static double[]
evaluateCoveringDomain(Point[] coords, Rectangle toCover)
Evaluates the minimal domain that covers the box with vector normals.java.util.List<GradientColorStop>
getColorStops()
Get the copy of current color stops list.protected AffineTransform
getCurrentSpaceToGradientVectorSpaceTransformation(Rectangle targetBoundingBox, AffineTransform contextTransform)
Returns the current space to gradient vector space transformations that should be applied to the shading color.protected abstract Point[]
getGradientVector(Rectangle targetBoundingBox, AffineTransform contextTransform)
Returns the base gradient vector in gradient vector space.GradientSpreadMethod
getSpreadMethod()
Get the current spread methodprivate static void
normalizeAutoStops(java.util.List<GradientColorStop> toNormalize)
private static void
normalizeAutoStops(java.util.List<GradientColorStop> toNormalizeList, int fromIndex, int toIndex, double prevOffset, double nextOffset)
private static void
normalizeFirstStopOffset(java.util.List<GradientColorStop> result)
private static void
normalizeHintsOffsets(java.util.List<GradientColorStop> result)
private static java.util.List<GradientColorStop>
normalizeStops(java.util.List<GradientColorStop> toNormalize, double baseVectorLength)
AbstractLinearGradientBuilder
setSpreadMethod(GradientSpreadMethod gradientSpreadMethod)
Set the spread method to use for the gradient
-
-
-
Field Detail
-
ZERO_EPSILON
protected static final double ZERO_EPSILON
The epsilon value used for data creation- See Also:
- Constant Field Values
-
stops
private final java.util.List<GradientColorStop> stops
-
spreadMethod
private GradientSpreadMethod spreadMethod
-
-
Method Detail
-
addColorStop
public AbstractLinearGradientBuilder addColorStop(GradientColorStop gradientColorStop)
Adds the new color stop to the end (more info
). Note: if the previously added color stop's offset would have grater 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 converted to relative first.)- Parameters:
gradientColorStop
- the gradient stop color to add- Returns:
- the current builder instance
-
setSpreadMethod
public AbstractLinearGradientBuilder setSpreadMethod(GradientSpreadMethod gradientSpreadMethod)
Set the spread method to use for the gradient- Parameters:
gradientSpreadMethod
- the gradient spread method to set- Returns:
- the current builder instance
-
getColorStops
public java.util.List<GradientColorStop> getColorStops()
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
-
getSpreadMethod
public GradientSpreadMethod getSpreadMethod()
Get the current spread method- Returns:
- the current spread method
-
buildColor
public Color buildColor(Rectangle targetBoundingBox, AffineTransform contextTransform, PdfDocument document)
Builds theColor
object representing the linear gradient with specified configuration that fills the target bounding box.- Parameters:
targetBoundingBox
- the bounding box to be filled in current spacecontextTransform
- the transformation from the base coordinates space into the current space. Thenull
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
- thePdfDocument
for which the linear gradient would be built.- Returns:
- the constructed
Color
ornull
if no color to be applied or base gradient vector has been specified
-
getGradientVector
protected abstract Point[] getGradientVector(Rectangle targetBoundingBox, AffineTransform contextTransform)
Returns the base gradient vector in gradient vector space. This vector would be set as shading coordinates vector and its length would be used to translate all color stops absolute offsets into the relatives.- Parameters:
targetBoundingBox
- the rectangle to be covered by constructed color in current spacecontextTransform
- the current canvas transformation- Returns:
- the array of exactly two elements specifying the gradient coordinates vector
-
getCurrentSpaceToGradientVectorSpaceTransformation
protected AffineTransform getCurrentSpaceToGradientVectorSpaceTransformation(Rectangle targetBoundingBox, AffineTransform contextTransform)
Returns the current space to gradient vector space transformations that should be applied to the shading color. 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 spacecontextTransform
- the current canvas transformation- Returns:
- the additional transformation to be concatenated to the current for resulted shading
or
null
if no additional transformation is specified
-
evaluateCoveringDomain
protected static double[] evaluateCoveringDomain(Point[] coords, Rectangle toCover)
Evaluates 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 boxtoCover
- the box that needs to be covered- Returns:
- the array of two elements in ascending order specifying the calculated covering domain
-
createCoordinatesForNewDomain
protected static Point[] createCoordinatesForNewDomain(double[] newDomain, Point[] 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 vectorbaseVector
- the array of exactly two elements that specifies the base vector which corresponds to [0, 1] domain- Returns:
- the array of two
-
createAxialShading
private static PdfAxialShading createAxialShading(Point[] baseCoordinatesVector, java.util.List<GradientColorStop> stops, GradientSpreadMethod spreadMethod, Rectangle targetBoundingBox)
-
normalizeStops
private static java.util.List<GradientColorStop> normalizeStops(java.util.List<GradientColorStop> toNormalize, double baseVectorLength)
-
normalizeHintsOffsets
private static void normalizeHintsOffsets(java.util.List<GradientColorStop> result)
-
normalizeAutoStops
private static void normalizeAutoStops(java.util.List<GradientColorStop> toNormalize)
-
normalizeAutoStops
private static void normalizeAutoStops(java.util.List<GradientColorStop> toNormalizeList, int fromIndex, int toIndex, double prevOffset, double nextOffset)
-
normalizeFirstStopOffset
private static void normalizeFirstStopOffset(java.util.List<GradientColorStop> result)
-
copyStopsAndNormalizeAbsoluteOffsets
private static java.util.List<GradientColorStop> copyStopsAndNormalizeAbsoluteOffsets(java.util.List<GradientColorStop> toNormalize, double baseVectorLength)
-
adjustStopsForPadIfNeeded
private static void adjustStopsForPadIfNeeded(java.util.List<GradientColorStop> stopsToConstruct, double[] coordinatesDomain)
-
adjustNormalizedStopsToCoverDomain
private static java.util.List<GradientColorStop> adjustNormalizedStopsToCoverDomain(java.util.List<GradientColorStop> normalizedStops, double[] targetDomain, GradientSpreadMethod spreadMethod)
-
constructFunction
private static IPdfFunction constructFunction(java.util.List<GradientColorStop> toConstruct)
-
constructSingleGradientSegmentFunction
private static AbstractPdfFunction<? extends PdfDictionary> constructSingleGradientSegmentFunction(GradientColorStop from, GradientColorStop to)
-
-