Selecting a distinct color from an image

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

Posts: 4
Joined: Tue Jan 30, 2018 7:51 pm

Selecting a distinct color from an image

Post by Limey »

This might be asking way too much but I thought i'd put it out there...

I have written a script and action that reads file names (col.A) and Hex colors (col.B) from a csv file.
First it finds a file from a folder that contains an image of a lamp and shade, it removes the lamp and trims to the shade. Then it places the shade into another image that is a scene with a lamp. Now I have a new shade on the lamp in the scene. Ok, so that's all working! Here's where the color comes in.

The color was chosen from somewhere in the shade and written to the CSV file by someone. Currently, the script sets the foreground color and the action fills a layer with it and creates a clipping group and changes the mode to color. This works too!!

I have artwork, a pillow and a wall that have a color matching some highlight color on the lamp shade... Still with me?

Now for the challenge:
I was wondering if perhaps the shade (while it is open on it's own and after it's trimmed) could be sampled for the top say 8 colors as can be done when optimizing a GIF in "Save for Web". It would even work if the document was duplicated, reduced to 200 x 200 and then sampled. The vivid colors will still be there. My thinking is that the vivid colors are the ones with the greatest disparity between R, G and B.
Example: Grays are all 000000 to FFFFFF with RGB values that are always equal. Any colors that are more muted will be a combination of different R,G,B values but closer in value than a more vivid colors.
In the resources I listed below the image in (a) has a vivid pink flower R:244, G:0, B:145. Although the Red and Blue values are higher, Green is zero making for a very distinct color. Here is another #d33aff is R:211,G:58, B:255

I think I have a formula that can determine the more vivd colors as a percentage.
Step 1: Take the 2 highest values of R,G or B and add the two.
Step 2: Divide the answer by 2 to get the average.
Step3: Divide the remaining value (the lowest of the 3) by the resulting average from Step 2
Step 4: Multiply by 100 to provide a percentage (rounded to the nearest integer)

Purple: #ac5aab R=172, G=90, B=171 The Math: 90/((172+171)/2)*100 = 52%
Orange: #fd971e R=253, G=151, B=30 The Math: 30/((253+151)/2)*100 = 14%
Orange would be chosen out of the two.

I believe the lower the percentage, the more vivid is the color. Grays will always be 100% (OK, there is a question then if the image is Greyscale
I hope I'm making sense...

So my thought is that if the colors in the image could be evaluated and the one with the greatest variation in values be singled out, I would not need to manually pick it and write it to the CSV. In essence, the color could be chosen on the fly.

Tie Breaker
Often times the RGB values contain a zero. In that case the percentage will always be zero. This could result in a tie against another color. Both would be a vivid color but "there can be only one". I don't think it matters to me which is chosen at this point.

(a) Here is a good one to work with. The color might be the pink, blue or orange. ... 15m02.html

(b) More lamps with shades (bigger images on product pages)

(c) Here are some colorized scenes where the lamp base, art, pillow and walls were colored by script and action. All files named and saved automatically by script.

(d) This image is just a test but it is the result of using the method and formula from above.

(e) Photoshop's "Save for web" allows the user to specify the maximum numbers of colors in a table. Could something similar be used to pick key colors for the purpose of this post?

Many thanks for reading through this and for any help or ideas that you may have.
This is way above my head but I know some of you geniuses might have some thoughts.

Limey (Paul)

Posts: 45
Joined: Fri Nov 04, 2016 8:29 am

Re: Selecting a distinct color from an image

Post by wasfiwasfi »

interesting !!