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

[Script - JS] Index layer (or file) colours to array for comparison

Community Beginner ,
Sep 13, 2021 Sep 13, 2021

Copy link to clipboard

Copied

Hey all,

 

I've been working on a project using Photoshop that involves using a large volume of unique colours - I've written some very basic scripts that randomise the colours (within a tethered range), but I've not been able to work out how to compare my generated colour against existing colours.

 

End goal is to iterate through that array of all colours present in the layer (or image), run an if statement, and on a negative for a match provide that colour as the foreground colour (therefore unique).

 

Is anyone able to advise on how to pull all unique colours in a layer (or image), and then push that to an array? If this is even possible in PS, mind. Any input or help would be greatly appreciated.

 

Thanks!

TOPICS
Actions and scripting

Views

246

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 Expert ,
Sep 15, 2021 Sep 15, 2021

Copy link to clipboard

Copied

What do you mean by »color« here? 

 

Do all the Layer’s pixels’ RGB values exactly match a limited number of pre-set colors or are there gradients, soft edges, …? 

Please post meaningful screenshots including the pertinent Panels. 

Votes

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
Community Beginner ,
Sep 15, 2021 Sep 15, 2021

Copy link to clipboard

Copied

I've attached an example of the end output - each tile is a unique colour (no soft edges).

 

This is normally achieved by manually noting down the last colour used, and then by making a manual adjustment to the RGB values (ie incrementing R, G or B by a single point) - but in the process of creating an image like this, there are thousands of unique colours, making it extremely time consuming.

 

To reduce overhead, the workflow is currently like this:

  • Basic script to generate a random colour
  • Draw outline, fill with said random colour
  • Check to see if that random colour is unique, or not, via colour-select tool (magic wand, 0 tolerance non contiguous)
  • Repeat for next tile

 

With the above workflow, matches are not apparent early on during the process due to the randomness, but once a thousand or more tiles have been drawn, conflicts start to happen (two tiles of the same colour at most usually). Or if I tether the random colour to say the current background colour, conflicts happen a lot sooner.

 

Effectively want I want to do, is every time I run the script to generate a new random colour, I want to parse all of the colours currently in whatever document/image I'm working on, for example, to make sure my newly generated colour isn't present in the document/image already.

 

So the question is, ss there a way for me to grab every colour in a layer or document, and then place that in an array, so I can iterate through the array and compare my newly generated colour against all of the colours stored in the array?

 

Example:

  • Run my current script, output is random.
  • Colour R:111, G:62, B:122 is the result
  • Iterate through an array, compare result above against all colours currently present in the image. If there is a match, retry.
    • Basically: Do something like document.colours >> parsedDocumentColours[parsedColours], forEach colour in parsedDocumentColours, if colour == generatedColour, then declareMatch

 

I realise this is unconventional use of Photoshop and I may have to look into a non-PS solution (such as a Python script) - but I was wanting to determine if this is something I can do within Photoshop for purpose of optimising workflow. This is for a non-profit community project (for a game), and an attempt to reduce workload for it.

Votes

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
Community Expert ,
Sep 16, 2021 Sep 16, 2021

Copy link to clipboard

Copied

Quite frankly I think you should rethink your process. 

Even using Illustrator might provide advantages for this. 

 

Another alternative might be creating a randomized array of distinct RGB colors (thousands even) and then using those one after the other. 

 

But evaluating the colors of all pixels in a layer anew each time seems like a potentially huge waste of time. 

The approach many people have thought of (running the color picker through the image) is effectively useless for large images; using the Histogram to limit the number of possible colors and check against those would probably also become lengthy with hundreds (and more) of colors; exporting RAW data and processing that with the Script also seems dicey to me …

But maybe someone has thunk of a better approach by now. 

Votes

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
Community Expert ,
Sep 16, 2021 Sep 16, 2021

Copy link to clipboard

Copied

Maybe you could write each color you use in the metadata and have the Script that creates the random colors check against that. 

Votes

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
Community Beginner ,
Sep 20, 2021 Sep 20, 2021

Copy link to clipboard

Copied

Thanks for taking a look! I'll defo explore some of your suggestions, will see what works (or doesn't) and/or re-assess my approach to this as necessary.

Votes

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
Community Expert ,
Sep 20, 2021 Sep 20, 2021

Copy link to clipboard

Copied

LATEST

Please let us know if you find an effective solution or if you hit further problems. 

Votes

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