Gradient
A gradient object. New gradients can be created using a Document's addGradient function.
Stops
Editing
Creating Copies
Type
addStop: location:
returns GradientStop; input Color; input Float
Creates and returns a new gradient stop object with the passed-in color and location, adding it to the gradient.
Example: adds a new color stop to the gradient fill of the frontmost selected shape
var doc = [app activeDocument]
var shape = [[doc selectedShapes] lastObject]
var fill = [shape fill]
if([fill fillType] == "gradient")
{
var gradient = [fill gradient]
var color = [doc addColorWithRed:64 green:128 blue:255 alpha:1]
[gradient addStop:color location:0.5]
}
duplicate
returns Gradient
Creates and returns a duplicate of this gradient.
endPoint
returns CGPoint; settable
Gets / Sets the end point of the gradient, relative to its parent shape's bounds. A value of (0, 0) is the top left corner of the shape's bounds; a value of (1, 1) is the bottom right corner.
Example: swap the start and end points of the frontmost selected shape's gradient fill
var doc = [app activeDocument]
var shape = [[doc selectedShapes] lastObject]
var fill = [shape fill]
if([fill fillType] == "gradient")
{
var gradient = [fill gradient]
var temp = [gradient startPoint]
gradient.startPoint = [gradient endPoint]
gradient.endPoint = temp
}
startPoint
returns CGPoint; settable
Gets / Sets the start point of the gradient, relative to its parent shape's bounds. A value of (0, 0) is the top left corner of the shape's bounds; a value of (1, 1) is the bottom right corner.
Example: set the start and end points of the frontmost selected shape's gradient fill, top-center to bottom-center
var doc = [app activeDocument]
var shape = [[doc selectedShapes] lastObject]
var fill = [shape fill]
if([fill fillType] == "gradient")
{
var gradient = [fill gradient]
gradient.startPoint = CGPointMake(0.5, 0)
gradient.endPoint = CGPointMake(0.5, 1)
}
stops
returns Array
Gets the array of gradient stop objects in the gradient.
Example: change the stop color each selected shape's gradient fill to 50% opacity
var shapes = [[app activeDocument] selectedShapes]
for(var i = 0; i < [shapes count]; i++)
{
var fill = [(shapes[i]) fill]
if([fill fillType] == "gradient")
{
var gradient = [fill gradient]
var stops = [gradient stops]
for(var j = 0; j < [stops count]; j++)
{
var stopColor = [(stops[j]) color]
var newColor = [stopColor duplicate]
newColor.alpha = 0.5
stops[j].color = newColor
}
}
}
type
returns String; settable
Gets / Sets the type of this gradient - "linear", "radial", or "angle"
Example: change the type of each selected shape's gradient fill to radial
var shapes = [[app activeDocument] selectedShapes]
for(var i = 0; i < [shapes count]; i++)
{
var shape = shapes[i]
var fill = [shape fill]
if([fill fillType] == "gradient")
[fill gradient].type = "radial"
}
GradientStop
Properties
Editing
color
_returns Color; settable
Gets / Sets the color of this stop.
location
returns Float; settable
Gets / Sets the location of this stop on the parent gradient.
remove
Removes the stop from the parent gradient.
Example: remove all of the stops in the frontmost selected shape's gradient fill, and set two new color stops
var doc = [app activeDocument]
var shape = [[doc selectedShapes] lastObject]
var fill = [shape fill]
if([fill fillType] == "gradient")
{
var gradient = [fill gradient]
var stops = [gradient stops]
for(var i = 0; i < [stops count]; i++)
{
var stop = stops[i]
[stop remove]
}
var color1 = [doc addColorWithRed:64 green:128 blue:255 alpha:1]
var color2 = [doc addColorWithRed:255 green:128 blue:64 alpha:1]
[gradient addStop:color1 location:0]
[gradient addStop:color2 location:1]
}
Next: Image
