Change rectangle hex color

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Megals
Posts: 10
Joined: Wed Jan 18, 2023 2:52 pm

Change rectangle hex color

Post by Megals »

Hi there,

i try to change the color of an existing rectangle without luck. The hex string is a variable. This is my code so far:

Code: Select all

var fillColor = new SolidColor();
fillColor.rgb.hexValue = hex.substring(1);
layerBaseColor.fillPath( fillColor );
fillColor.red = parseInt(hex.substring(1, 3), 16);
fillColor.green = parseInt(hex.substring(3, 5), 16);
fillColor.blue = parseInt(hex.substring(5, 7), 16);
myLayer.fillPath( fillColor );
Best regards
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Change rectangle hex color

Post by Kukurykus »

Hard to guess what you want, specifically when you use not working variables:

Code: Select all

(sc = new SolidColor())
.rgb.hexValue = '808080'
activeDocument.selection.fill(sc)
Megals
Posts: 10
Joined: Wed Jan 18, 2023 2:52 pm

Re: Change rectangle hex color

Post by Megals »

Thank you for your reply.

I want to change the color of an existing rectangle myLayer, by a hex string e.g. #FF0000.

Best regards
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Change rectangle hex color

Post by Kukurykus »

Code: Select all

(function(v) {
	sTT = stringIDToTypeID;
	(sc = new SolidColor()).rgb.hexValue = v;
	(ref = new ActionReference()).putEnumerated
	(sTT('contentLayer'), sTT('ordinal'), sTT('targetEnum'));
	(dsc1 = new ActionDescriptor()).putReference(sTT('null'), ref)
	dsc4 = new ActionDescriptor(), rgb = [].slice.call(sc.rgb.reflect.properties)
	.splice(0, 3); while(shft = rgb.shift()) dsc4.putDouble(sTT(shft), sc.rgb[shft]);
	(dsc3 = new ActionDescriptor()).putObject(sTT('color'), sTT('RGBColor'), dsc4);
	(dsc2 = new ActionDescriptor()).putObject(sTT('fillContents'), sTT('solidColorLayer'), dsc3);
	(dsc5 = new ActionDescriptor()).putBoolean(sTT('fillEnabled'), true), dsc2.putObject(sTT('strokeStyle'),
	sTT('strokeStyle'), dsc5), dsc1.putObject(sTT('to'), sTT('shapeStyle'), dsc2), executeAction(sTT('set'), dsc1)
})('FF0000')
Megals
Posts: 10
Joined: Wed Jan 18, 2023 2:52 pm

Re: Change rectangle hex color

Post by Megals »

Hi,

thank you for the code, but It is not working for me. This is what i did:
- New File, added a rectangle.
- Put the code into a jsx file and drag and dropped it.
- Added a alert to the code to make sure it gets executed.
- i made sure the rectangle layer is active
- Photoshop 2022
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Change rectangle hex color

Post by Kukurykus »

Zip your psd and attach to post.
Megals
Posts: 10
Joined: Wed Jan 18, 2023 2:52 pm

Re: Change rectangle hex color

Post by Megals »

attached it
Attachments
rectangle_color.zip
(7.35 KiB) Downloaded 172 times
User avatar
Kukurykus
Posts: 528
Joined: Mon Jul 25, 2016 12:36 pm

Re: Change rectangle hex color

Post by Kukurykus »

What is version of your application (which you can check from 'Help' menu > 'About Photoshop')? If that is not the latest you have to change:

Code: Select all

(shft)
to:

Code: Select all

(shft.name)
Megals
Posts: 10
Joined: Wed Jan 18, 2023 2:52 pm

Re: Change rectangle hex color

Post by Megals »

Now it is working! Thank you so much!