Copy link to clipboard
Copied
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!
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 tho
...Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
Sorry, I meant 'FILL' a specific layer with the selected color.
Copy link to clipboard
Copied
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 );
Copy link to clipboard
Copied
Thanks for pointing me in the right direction!
When I run the following. I get an error stating that 'color1 is undefined':
//create a script that stores array of hair colors
//one of these is assigned at random when we run the script
var colors = [color1, color2, color3]
color1 = newSolidColor;
color1.rgb.red = 200;
color1.rgb.green = 55;
color1.rgb.blue = 0;
color2 = newSolidColor;
color2.rgb.red = 128;
color2.rgb.green = 128;
color2.rgb.blue = 0;
color3 = newSolidColor;
color3.rgb.red = 64;
color3.rgb.green = 128;
color3.rgb.blue = 64;
var hairLayer = app.activeDocument.artLayers.getByName("Hair");
app.activeDocument.selection.fill(ColorArray[Math.floor(Math.random(ColorArray.length) * ColorArray.length)]);
Copy link to clipboard
Copied
Awesome, this works. I want to randomly select one of 4 predefined colors from an array rather than having these be completely random.
I'm just missing the syntax to define a color.
In Unity C# I have done something like this:
nColors[] = {color(128,128, 0), color(64, 64, 0), color(255, 0, 65)};
Copy link to clipboard
Copied
Got it! I was referencing the colors in the array before they had been defined. Thanks a tonne for the help!
Copy link to clipboard
Copied
Hi, im trying to do the same thing. I am a novice with javascript. may I ask how you got this code to work? I am also getting the "undefined" error
Copy link to clipboard
Copied
The message should have the line of code the has the undefine error. Look at at the line of code what has not been set what does java see in the line that has not been defined.
Supply pertinent information for more timely and effective answers. The more information you supply about your issue, the better equipped other community members will be to answer. Consider including the following in your question:
Find more inspiration, events, and resources on the new Adobe Community
Explore Now