Search code examples
javascriptadobe-illustratorextendscript

Get RGB color of coordinates: Adobe Illustrator


I'm using ExtendScript to work on JavaScript for Adobe Illustrator 2015. Is there any way I could get RGB values from a coordinate in the code below?

// declares a document
var doc = app.activeDocument;
// sets x and y coordinates to get color from
var xPosition = 70.0;
var yPosition = 64.0;

This is what needs work:

// gets rgb values
double redValue = doc.getRGBColor(xPosition, yPosition).red;
double greenValue = doc.getRGBColor(xPosition, yPosition).red;
double blueValue = doc.getRGBColor(xPosition, yPosition).red;

I've googled quite a bit and found this. It doesn't work, though, either because it was posted in 2009, or because it was meant to be in photoshop.

A solution to the problem or translation of that post would be greatly appreciated.


Solution

  • [EDIT: My apologies for giving essentially an AppleScript answer to your ExtendScript question. I was just looking over AS questions and forgot I went to a different section. I can only hope that you are on a Mac. If not, I guess I'll just eat my downvotes and weep.]

    There is a workaround. The advantage of it (and part of the workaround nature of it) is that it works in all apps. The downside is that it requires python (which should be on your Mac anyway - fairly easy to install if not), and two pieces of third party software (both free), "checkModifierKeys" and "cliclick". I've been using a script that appears in my script menu for years. The python part is described here: http://thechrisgreen.blogspot.com/2013/04/python-script-for-getting-pixel-color.html This script can be saved, made executable and invoked using the AS do shell script command. and the rest of it, for selecting a point on the screen and for waiting for the control key to be pressed (that's how mine works) is pretty simple. The basic checkModifierKeys part (which waits until the Control key is pressed) is:

    set controlIsDown to false
    repeat until (controlIsDown)
        set initialCheck to ((do shell script "/usr/local/bin/checkModifierKeys control"))
        if initialCheck = "1" then set controlIsDown to true
    end repeat
    

    The cliclick part (which gets the coordinates) is:

    set xyGrabbed to do shell script "/usr/local/bin/cliclick p"
    

    It may seem like a long way to go for it, but it works great. My version converts the rgb value to hex with this handler, which is useful for my purposes:

    to makeHex(theNumber) --was anInteger
        --Converts an unsigned integer to a two-digit hexadecimal value
        set theResult to ""
        repeat with theIndex from 1 to 0 by -1
            set theBase to (16 ^ theIndex) as integer
            if theNumber is greater than or equal to theBase then
                set theMultiplier to ((theNumber - (theNumber mod theBase)) / theBase) as integer
                set theResult to theResult & item theMultiplier of ¬
                    {1, 2, 3, 4, 5, 6, 7, 8, 9, "A", "B", "C", "D", "E", "F"}
                set theNumber to (theNumber - theBase * theMultiplier)
            else
                set theResult to (theResult & "0")
            end if
        end repeat
    theResult
    end makeHex