Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Looking for some help with a Photoshop Random Color Javascript

New Here ,
Nov 14, 2019 Nov 14, 2019

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!

TOPICS
Actions and scripting
1.7K
Translate
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

Community Expert , Nov 14, 2019 Nov 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 tho

...
Translate
Adobe
Community Expert ,
Nov 14, 2019 Nov 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

image.png

JJMack
Translate
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
New Here ,
Nov 14, 2019 Nov 14, 2019

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

Translate
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
Community Expert ,
Nov 14, 2019 Nov 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
Translate
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
New Here ,
Nov 14, 2019 Nov 14, 2019

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)]);

 

Translate
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
New Here ,
Nov 14, 2019 Nov 14, 2019

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)};
Translate
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
New Here ,
Nov 14, 2019 Nov 14, 2019

Got it! I was referencing the colors in the array before they had been defined. Thanks a tonne for the help!

Translate
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
New Here ,
Feb 13, 2020 Feb 13, 2020

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

Translate
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
Community Expert ,
Feb 14, 2020 Feb 14, 2020
LATEST

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:

  • Adobe product and version number
  • Operating system and version number
  • The full text of any error message(s)
  • What you were doing when the problem occurred
  • Screenshots of the problem
  • Computer hardware, such as CPU; GPU; the amount of RAM;
  • Action, Script, System Info etc

 

JJMack
Translate
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