PSD Fill Layer updating using Java
Overview
Creating a regular layer involves using the createRegularLayer function, which requires parameters for defining the layer’s position and size. This function creates a new layer, sets its bounds, and fills it with a specified color.
For a color fill layer, you utilize the FillLayer.createInstance method with the FillType.Color parameter. Once the fill layer is created, access its fill settings through the fill_settings property and set the desired color using the color property of the ColorFillSettings class. In this context, the color is set to Color.getCoral(). Additionally, the clipping property of the fill layer is set to 1, causing it to function as a clipping mask.
Gradient fill layers are created similarly using the FillLayer.create_instance method, but with the FillType.Gradient parameter. Like color fill layers, you access the fill settings through fill_settings and set the gradient color points and transparency points. In this example, the gradient color points are defined with the GradientColorPoint class, and the transparency points with the GradientTransparencyPoint class. The clipping property of the fill layer is also set to 1.
Pattern fill layers are created using FillLayer.createInstance with the FillType.Pattern parameter. Once again, access the fill settings via fill_settings and set the pattern data and other properties. In this code, the pattern data is defined using the PatternFillSettings class, and the clipping property is set to 1.
Once the fill layers are created, add them to the PSD image using the addLayer method, specifying the display name and other properties for each fill layer.
Lastly, save the PSD image and its corresponding PNG image with the provided code. The PNG options are configured to use true color with alpha for transparency.
Please refer to the full example for more details.
Example
public class LayerManipulation { | |
public static Layer createRegularLayer(int left, int top, int width, int height) { | |
Layer layer = new Layer(); | |
layer.setLeft(left); | |
layer.setTop(top); | |
layer.setRight(left + width); | |
layer.setBottom(top + height); | |
int color = Color.getAqua().toArgb(); | |
int[] testData = new int[width * height]; | |
for (int i = 0; i < width * height; i++) { | |
testData[i] = color; | |
} | |
layer.saveArgb32Pixels(layer.getBounds(), testData); | |
return layer; | |
} | |
public static void fillLayerManipulationTest() { | |
String outputPng = "all_fill_layers.png"; | |
String outputPsd = "all_fill_layers.psd"; | |
try (PsdImage image = new PsdImage(100, 100)) { | |
Layer layer1 = createRegularLayer(0, 0, 50, 50); | |
image.addLayer(layer1); | |
FillLayer colorFillLayer = FillLayer.createInstance(FillType.Color); | |
ColorFillSettings colorFillSettings = (ColorFillSettings) colorFillLayer.getFillSettings(); | |
colorFillSettings.setColor(Color.getCoral()); | |
colorFillLayer.setClipping((byte) 1); | |
colorFillLayer.setDisplayName("Color Fill Layer"); | |
image.addLayer(colorFillLayer); | |
Layer layer2 = createRegularLayer(50, 0, 50, 50); | |
image.addLayer(layer2); | |
GradientColorPoint[] gradientColorPoints = new GradientColorPoint[]{ | |
new GradientColorPoint(Color.getRed(), 2048, 50), | |
new GradientColorPoint(Color.getGreen(), 3072, 50), | |
new GradientColorPoint(Color.getBlue(), 4096, 50)}; | |
GradientTransparencyPoint tp1 = new GradientTransparencyPoint(); | |
tp1.setLocation(0); | |
tp1.setMedianPointLocation(50); | |
tp1.setOpacity(128); | |
GradientTransparencyPoint tp2 = new GradientTransparencyPoint(); | |
tp2.setLocation(2048); | |
tp2.setMedianPointLocation(50); | |
tp2.setOpacity(176); | |
GradientTransparencyPoint[] gradientTransparencyPoints = new GradientTransparencyPoint[]{tp1, tp2}; | |
FillLayer gradientFillLayer = FillLayer.createInstance(FillType.Gradient); | |
gradientFillLayer.setDisplayName("Gradient Fill Layer"); | |
GradientFillSettings gradientFillSettings = (GradientFillSettings) gradientFillLayer.getFillSettings(); | |
gradientFillSettings.setColorPoints(gradientColorPoints); | |
gradientFillSettings.setAngle(-45); | |
gradientFillSettings.setTransparencyPoints(gradientTransparencyPoints); | |
gradientFillLayer.setClipping((byte) 1); | |
image.addLayer(gradientFillLayer); | |
Layer layer3 = createRegularLayer(0, 50, 50, 50); | |
image.addLayer(layer3); | |
FillLayer patternFillLayer = FillLayer.createInstance(FillType.Pattern); | |
patternFillLayer.setDisplayName("Pattern Fill Layer"); | |
patternFillLayer.setOpacity((byte) 50); | |
patternFillLayer.setClipping((byte) 1); | |
PatternFillSettings patternFillSettings = (PatternFillSettings) patternFillLayer.getFillSettings(); | |
patternFillSettings.setPatternData(new int[]{ | |
Color.getRed().toArgb(), Color.getTransparent().toArgb(), Color.getTransparent().toArgb(), | |
Color.getTransparent().toArgb(), Color.getRed().toArgb(), Color.getTransparent().toArgb(), | |
Color.getTransparent().toArgb(), Color.getTransparent().toArgb(), Color.getRed().toArgb() | |
}); | |
patternFillSettings.setPatternWidth(3); | |
patternFillSettings.setPatternHeight(3); | |
image.addLayer(patternFillLayer); | |
PngOptions pngOpt = new PngOptions(); | |
pngOpt.setColorType(PngColorType.TruecolorWithAlpha); | |
image.save(outputPng, pngOpt); | |
image.save(outputPsd); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String[] args) { | |
fillLayerManipulationTest(); | |
} | |
} |