Unable to change foreground color of FillLayer in java

I wanted to change color of a layer in my psd doc programmatically but unable to that. can anybody help whether it is possible or not?

@mail9deep

I suggest you to please visit the following link for more about setting color of Layer. Please, share if I may help you further in this regard.

Hi @mudassir.fayyaz ,
Didn’t get any answer from there. I can explain you what exactly I want.
FillLayer recLayer;
I want to change the background color and foreground color of this fillLayer. is it possible?

@mail9deep

There is option to change the background color of the layer using API that you may explore on your end. Please visit API reference guide section for Layer’s Background Color.

Hi @mudassir.fayyaz,

I have tried using this but it’s not working.
1st way i tried by this.
FillLayer recLayer = (FillLayer) findLayer(“Rectangle 6”,psdImage);
IColorFillSettings settings = (IColorFillSettings)recLayer.getFillSettings();
settings.setColor(Color.getRed());
recLayer.setFillSettings(settings);
recLayer.update();

2nd way:
recLayer.setBackgroundColor(Color.getBlue());

none of the above is working.

@mail9deep

Can you please share the source file and generated output so that we may investigate it further on our end.

Hi @mudassir.fayyaz

https://drive.google.com/drive/folders/1Kl2brbMcILt1Juay1Be0mW02UWZvMuZP?usp=sharing

Have shared psd file and output image. In Output Image want to change the color of box on which Order Now is written from orange to different color. Let me know if you need anything else. Also in same psd wanted to change the font of Order Now Text.

@mail9deep

I am sorry that I am unable to access the shared link. Can you please grant public access to the file so that we may download that and help you further.

Hi @mudassir.fayyaz ,

I am resharing the link with public access.
https://drive.google.com/drive/folders/1Kl2brbMcILt1Juay1Be0mW02UWZvMuZP?usp=sharing

@mail9deep

Thank you for sharing the information. We will process that and will try to help you further as soon as possible.

Hi @mudassir.fayyaz,

Did you get any solution for changing the background and foreground color of the given sample?

@mail9deep

Can you please try using following sample code.

	String dataDir = "C:\\Projects\\data\\";
    String targetFilePath = dataDir + "Zyrtec-Endcards-RD2.psd";
    String resultFilePath = dataDir + "output.png";
    String resultFilePathPsd = dataDir + "output.psd";

    PsdImage psdImage = null;
    try
    {
        RasterImage im = null;
        psdImage = (PsdImage) Image.load(targetFilePath);

        for (int i = 0; i<psdImage.getLayers().length; i++) {
            Layer layer = psdImage.getLayers()[i];

            if (layer.getDisplayName().equals("Rectangle 6")) {
                if (layer instanceof FillLayer) {
                    FillLayer recLayer = (FillLayer) layer;
                    IColorFillSettings settings = (IColorFillSettings) recLayer.getFillSettings();
                    settings.setColor(Color.getPurple());
                    recLayer.setFillSettings(settings);
                    recLayer.update();
                }
            }
        }

        PngOptions pngOptions = new PngOptions();
        pngOptions.setColorType(PngColorType.TruecolorWithAlpha);

        psdImage.save(resultFilePathPsd);
        psdImage.save(resultFilePath, pngOptions);
    }
    finally
    {
        if (psdImage != null) psdImage.dispose();
    }

Hi @mudassir.fayyaz,

It worked for me, but can you let me know why we are saving psdImage twice?

@mail9deep

It was to elaborate that we have saved file to PSD as well as to PNG.