Search code examples
javascriptphotoshopphotoshop-script

Communication issue between functions into JS and JSX files


I have made this function into a JS file...

function getColors(isPick, isForecolor)
{
    var chosenFunction = 'getColor(' + isPick + ', ' + isForecolor + ')';
    csInterface.evalScript(chosenFunction, function(result)
    {
        if(result !== 'undefined')
        {
            if (isForecolor == true){
                foregroundHexColor = result;
                // etc...
            }
            else
            {
                backgroundHexColor = result;
                //etc..
            };
        };
    });
};

which get a hexadecimal color value from this function from a JSX file.

function getColor(isPick, isForecolor)
{
    var color_PickerCase;
    var decimal_Color;
    var hexadecimal_Color;

    if (isForecolor == true)
    {
        color_PickerCase = app.foregroundColor.rgb.hexValue;
    }
    else
    {
        color_PickerCase = app.backgroundColor.rgb.hexValue;
    };

    if (isPick == true)
    {
        if (app.showColorPicker(isForecolor)){
            decimal_Color = color_PickerCase;
            hexadecimal_Color = decimal_Color.toString(16);
        }
        else
        {
            return;
        };
    }
    else
    {
        decimal_Color = color_PickerCase;
        hexadecimal_Color = decimal_Color.toString(16);
    };

    return hexadecimal_Color;    
};

In some way it works, but for some reason I have to do the same thing two times so to get the value!!! Any idea why is this happening?

Thank you for your time!!!

UPDATE: A correction, it works only at first click. Then needs to clicked two times so to get the value!!!


Solution

  • Well, here is the solution...

    function getColor(isPick, isForecolor)
    {
        var color_PickerCase;
        var decimal_Color;
        var hexadecimal_Color;
    
        if (isPick === true && app.showColorPicker(isForecolor) === false)
        {
                return;
        }
    
        if (isForecolor === true)
        {
            color_PickerCase = app.foregroundColor.rgb.hexValue;
        }
        else
        {
            color_PickerCase = app.backgroundColor.rgb.hexValue;
        }
    
        decimal_Color = color_PickerCase;
        hexadecimal_Color = decimal_Color.toString(16);
        return hexadecimal_Color;    
    };
    

    As joojaa from graphicdesign said, I was asking for the color before picking it and I was getting the color form the last time!!!