Skip to main content
jamied36294533
Participant
November 14, 2019
Answered

Looking for some help with a Photoshop Random Color Javascript

  • November 14, 2019
  • 1 reply
  • 1676 views

Hi Guys,

I'm trying to write a javascript that stores an array of colors and then assigns one of these at random to a named layer in my active document.

Seems like this would be fairly easy, but I'm not finding any documentation or forum posts that cover this.

I have seen individual colors stored as arrays (r, g, b) but not whole colors.

Any help would be appreciated!

This topic has been closed for replies.
Correct answer JJMack

What colors

Your color array could be created like this

 

var ColorArray = [color,color,color] where each color is a solid color variable. which are array that have three color values for example

 

textColor = new SolidColor;
textColor.rgb.red = 255;
textColor.rgb.green = 255;
textColor.rgb.blue = 255;

 

Once you have your color array  its easy to get a random color from the array  your fill color would be 

(ColorArray[Math.floor(Math.random(ColorArray.length) * ColorArray.length)])

 

You need though to be careful when dealing with layers by name.  Photoshop Document Layer Names are not required to be unique there can be more then one  layer in a document with the same name.

 

Get the layer by name make it the activate layer select all and fill the selection with the color

 

app.activeDocument.selection.fill(ColorArray[Math.floor(Math.random(ColorArray.length) * ColorArray.length)]);

Creating an array of 10 random colors and picking one.

 

randomColor = new SolidColor;	
randomColors = new Array();	    
for(var i = 0; i < 10; i++) {
	randomColor.rgb.red = Math.round(Math.random() * 255);
	randomColor.rgb.green = Math.round(Math.random() * 255);
	randomColor.rgb.blue = Math.round(Math.random() * 255);
	randomColors.push(randomColor);
}

pickone=randomColors[Math.floor(Math.random(randomColors.length) * randomColors.length)];
alert(randomColors + "\nPicked Color is Red " + pickone.rgb.red + " Green " + pickone.rgb.green + " Blue " + pickone.rgb.blue );

 

 

1 reply

JJMack
Community Expert
Community Expert
November 14, 2019

Assign one to a named layer???  What do you mean by that? Fill the Named layer will the random color,  add the random color  values to the layer's current name?  As far as I know there are only 7 layer slot colors that you can set a layer to have.   Creating Random colors, sampling colors, and using known colors is easy. Pushing Colors into and array or  defining an array of colors is easy. However, I do not know what you mean by assign one to a named layer

JJMack
jamied36294533
Participant
November 14, 2019

Sorry, I meant 'FILL' a specific layer with the selected color.

JJMack
Community Expert
JJMackCommunity ExpertCorrect answer
Community Expert
November 14, 2019

What colors

Your color array could be created like this

 

var ColorArray = [color,color,color] where each color is a solid color variable. which are array that have three color values for example

 

textColor = new SolidColor;
textColor.rgb.red = 255;
textColor.rgb.green = 255;
textColor.rgb.blue = 255;

 

Once you have your color array  its easy to get a random color from the array  your fill color would be 

(ColorArray[Math.floor(Math.random(ColorArray.length) * ColorArray.length)])

 

You need though to be careful when dealing with layers by name.  Photoshop Document Layer Names are not required to be unique there can be more then one  layer in a document with the same name.

 

Get the layer by name make it the activate layer select all and fill the selection with the color

 

app.activeDocument.selection.fill(ColorArray[Math.floor(Math.random(ColorArray.length) * ColorArray.length)]);

Creating an array of 10 random colors and picking one.

 

randomColor = new SolidColor;	
randomColors = new Array();	    
for(var i = 0; i < 10; i++) {
	randomColor.rgb.red = Math.round(Math.random() * 255);
	randomColor.rgb.green = Math.round(Math.random() * 255);
	randomColor.rgb.blue = Math.round(Math.random() * 255);
	randomColors.push(randomColor);
}

pickone=randomColors[Math.floor(Math.random(randomColors.length) * randomColors.length)];
alert(randomColors + "\nPicked Color is Red " + pickone.rgb.red + " Green " + pickone.rgb.green + " Blue " + pickone.rgb.blue );

 

 

JJMack