[FEATURE REQUEST] Get all colors used in an INDEXEDCOLOR document

In an Indexed Color document, I can dump all of my swatches into an .act file with Image > Mode > Color Table… and then save the .act.

How can I get a list of colors in an INDEXEDCOLOR document using UXP?

Right now my workaround is horribly slow: nested loops to traverse every pixel in the document, top to bottom, left to right, and call app.activeDocument.sampleColor() on every one of them, collecting each unique color in an array. This is a deal breaker because of the time required to scan millions of pixels even though I know there will only be 10 unique colors in the document.

How can UXP help me do this? Anyone?

There’s a “histogram” property on the document. Quoting the docs:

histogram containing the number of pixels at each color intensity level for the composite channel. The array contains 256 members. Valid only when mode = `DocumentMode.{RGB,CMYK,INDEXEDCOLOR}

Could this be helpful?

I don’t think Histograms are helpful, because they return you how the population of pixels are represented in each channel (how many Red 0, Red 1, …, Red 255, Green 0, Green 1, … etc.) which cannot tell you anything useful about the way these values are actually combined in each pixel (how many R0G132B200, etc.)

A possibly faster (compared to color samplers) workaround would be to get pixel values using the Imaging API, now in beta. Get the whole pixel array, eliminate duplicates, count, and you should have the swatches.

1 Like

Hi! Any more insight on how to use Imaging API to get the whole pixel array and then parse that data? I can’t figure it out.