How to create a path and gradient fill each randomly created closed path by iteration

Explorer ,
May 09, 2021 May 09, 2021

Copy link to clipboard

Copied

Hello,

 

I am tryng to create a path and gradient fill each randomly created closed path by iteration. Below you will find the code that only fills each closed path, and not gradient filled because I am unable to find a way on how to do that:

 

 

var doc = app.activeDocument;

var currentRulerUnits = app.preferences.rulerUnits;
var currentTypeUnits = app.preferences.typeUnits;

app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;

var height= doc.height;
var width = doc.width;

function triangles() {
	for (i=0; i<5; i++) {
		var size = random(10,90);
		var p1 = PPI(random(Math.floor(0+size), Math.floor(width-size)), random(Math.floor(0+size), Math.floor(height-size)));
		var p2 = PPI(random(Math.floor(0+size), Math.floor(width-size)), random(Math.floor(0+size), Math.floor(height-size)));
		var p3 = PPI(random(Math.floor(0+size), Math.floor(width-size)), random(Math.floor(0+size), Math.floor(height-size)));
		doc.suspendHistory("triangles","triangle(p1,p2,p3,'triangle'+[i],doc)");
	}
}
doc.suspendHistory("pattern","triangles()");

function triangle(point1,point2,point3,name,doc){
	var spinfo = new SubPathInfo();
	spinfo.closed = true;
	spinfo.operation = ShapeOperation.SHAPEADD;
	spinfo.entireSubPath = [point1,point2,point3];
	var line = doc.pathItems.add(name, [spinfo]);
	fillPath();
	line.remove();
	};

function PPI(x,y){
	var ppinfo = new PathPointInfo();
	ppinfo.anchor = [x,y];
	ppinfo.leftDirection = [x,y];
	ppinfo.rightDirection = [x,y];
	ppinfo.kind = PointKind.CORNERPOINT;
	return ppinfo;
};

function fillPath() {
	var desc1 = new ActionDescriptor();
	var ref1 = new ActionReference();
	ref1.putEnumerated(charIDToTypeID('Path'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
	desc1.putReference(charIDToTypeID('null'), ref1);
	desc1.putBoolean(charIDToTypeID('WhPt'), true);
	desc1.putEnumerated(charIDToTypeID('Usng'), charIDToTypeID('FlCn'), charIDToTypeID('FrgC'));
	desc1.putUnitDouble(charIDToTypeID('Opct'), charIDToTypeID('#Prc'), 100);
	desc1.putEnumerated(charIDToTypeID('Md  '), charIDToTypeID('BlnM'), charIDToTypeID('Nrml'));
	desc1.putUnitDouble(charIDToTypeID('Rds '), charIDToTypeID('#Pxl'), 0);
	desc1.putBoolean(charIDToTypeID('AntA'), true);
	executeAction(charIDToTypeID('Fl  '), desc1, DialogModes.NO);
}

function gradientFill() {
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putUnitDouble(charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 88);
    desc2.putUnitDouble(charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 356);
    desc1.putObject(charIDToTypeID('From'), charIDToTypeID('Pnt '), desc2);
    var desc3 = new ActionDescriptor();
    desc3.putUnitDouble(charIDToTypeID('Hrzn'), charIDToTypeID('#Pxl'), 360);
    desc3.putUnitDouble(charIDToTypeID('Vrtc'), charIDToTypeID('#Pxl'), 120);
    desc1.putObject(charIDToTypeID('T   '), charIDToTypeID('Pnt '), desc3);
    desc1.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('GrdT'), charIDToTypeID('Lnr '));
    desc1.putBoolean(charIDToTypeID('Dthr'), true);
    desc1.putBoolean(charIDToTypeID('UsMs'), true);
    var desc4 = new ActionDescriptor();
    desc4.putString(charIDToTypeID('Nm  '), "Black, White");
    desc4.putEnumerated(charIDToTypeID('GrdF'), charIDToTypeID('GrdF'), charIDToTypeID('CstS'));
    desc4.putDouble(charIDToTypeID('Intr'), 4096);
    var list1 = new ActionList();
    var desc5 = new ActionDescriptor();
    var desc6 = new ActionDescriptor();
    desc6.putDouble(charIDToTypeID('Cyn '), 75.02);
    desc6.putDouble(charIDToTypeID('Mgnt'), 68.01);
    desc6.putDouble(charIDToTypeID('Ylw '), 67);
    desc6.putDouble(charIDToTypeID('Blck'), 90.19);
    desc5.putObject(charIDToTypeID('Clr '), charIDToTypeID('CMYC'), desc6);
    desc5.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('Clry'), charIDToTypeID('UsrS'));
    desc5.putInteger(charIDToTypeID('Lctn'), 0);
    desc5.putInteger(charIDToTypeID('Mdpn'), 50);
    list1.putObject(charIDToTypeID('Clrt'), desc5);
    var desc7 = new ActionDescriptor();
    var desc8 = new ActionDescriptor();
    desc8.putDouble(charIDToTypeID('Cyn '), 0);
    desc8.putDouble(charIDToTypeID('Mgnt'), 0);
    desc8.putDouble(charIDToTypeID('Ylw '), 0);
    desc8.putDouble(charIDToTypeID('Blck'), 0);
    desc7.putObject(charIDToTypeID('Clr '), charIDToTypeID('CMYC'), desc8);
    desc7.putEnumerated(charIDToTypeID('Type'), charIDToTypeID('Clry'), charIDToTypeID('UsrS'));
    desc7.putInteger(charIDToTypeID('Lctn'), 4096);
    desc7.putInteger(charIDToTypeID('Mdpn'), 50);
    list1.putObject(charIDToTypeID('Clrt'), desc7);
    desc4.putList(charIDToTypeID('Clrs'), list1);
    var list2 = new ActionList();
    var desc9 = new ActionDescriptor();
    desc9.putUnitDouble(charIDToTypeID('Opct'), charIDToTypeID('#Prc'), 100);
    desc9.putInteger(charIDToTypeID('Lctn'), 0);
    desc9.putInteger(charIDToTypeID('Mdpn'), 50);
    list2.putObject(charIDToTypeID('TrnS'), desc9);
    var desc10 = new ActionDescriptor();
    desc10.putUnitDouble(charIDToTypeID('Opct'), charIDToTypeID('#Prc'), 100);
    desc10.putInteger(charIDToTypeID('Lctn'), 4096);
    desc10.putInteger(charIDToTypeID('Mdpn'), 50);
    list2.putObject(charIDToTypeID('TrnS'), desc10);
    desc4.putList(charIDToTypeID('Trns'), list2);
    desc1.putObject(charIDToTypeID('Grad'), charIDToTypeID('Grdn'), desc4);
    executeAction(charIDToTypeID('Grdn'), desc1, DialogModes.NO);
  };

function random(min, max) {
	const num = Math.floor(Math.random() * (max - min + 1)) + min;
	return num;
}

 

 

So, my simple question is, is it possible to gradient fill each iteration of randomly created closed path?

 

Thanks,

Damian

TOPICS
Actions and scripting, How to, Make It, Windows

Views

1.7K

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , May 09, 2021 May 09, 2021
If I understood correctly, then  try instead of calling the fillPath() function to call (app.doAction) a specially prepared Action that will transform your path into a Shape filled with the gradient you need. Instead of an action call, you can use the code obtained with the ScriptListener. If you want a random gradient, this is trickier. I don't know the solution yet.    

Likes

Translate

Translate
Adobe Community Professional ,
May 09, 2021 May 09, 2021

Copy link to clipboard

Copied

If I understood correctly, then  try instead of calling the fillPath() function to call (app.doAction) a specially prepared Action that will transform your path into a Shape filled with the gradient you need. Instead of an action call, you can use the code obtained with the ScriptListener.

 

If you want a random gradient, this is trickier. I don't know the solution yet.
 

 

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 09, 2021 May 09, 2021

Copy link to clipboard

Copied

Yes, as r-bin suggested, use scriptListener to record changing a path to a gradient fill. I do that all the time for my artwork.

FB_IMG_1605799469997.jpg

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

Thank you both for your suggestions as this was my alternative goal if there isn't any other and shorter way to do the path filled with gradient instead of solid color. At least there is a solution.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 10, 2021 May 10, 2021

Copy link to clipboard

Copied

With my paths, I draw the initial ones, then use a script to morph them. I also select points on a reference image to create the colors for the gradients. A lot can be done with them.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 12, 2021 May 12, 2021

Copy link to clipboard

Copied

Interesting on what you say about morphing the initial created path and what you say about using an image's pixel information to create gradients. Would you mind sharing the code so I can study it?

 

Thank you,

Damian

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Adobe Community Professional ,
May 12, 2021 May 12, 2021

Copy link to clipboard

Copied

The script is long and a bit complicated to use. In a nut shell, the morphed shapes have to have the same number of anchors, made in the same order. So I normally draw one, then copy it, na then edit the shape. I add two reference dots to align the path along two other paths. Then add 7 to 14 dots for reference for color sampling. The shapes are aligned vertically,  the two reference paths are broken up into small sections so that the morphed shapes can be placed equally along them. The delta change between each anchor point and handed is calculated to adjust the next shape. A new shape is drawn, then moved and rotated to fit along the two paths. The color reference points are also sized and rotated with the shapes, then used to mark a location to sample a reference image. The new paths are then converted to gradient shape layes, using the color info from the reference points.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
May 20, 2021 May 20, 2021

Copy link to clipboard

Copied

LATEST

Thank you Chuck for your information, I will try to figure it out on my own.

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines