Skip to main content
Simonetos The Greek
Inspiring
October 26, 2019
Answered

Communication issue between functions into JS and JSX files

  • October 26, 2019
  • 1 reply
  • 1658 views

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!!!

This topic has been closed for replies.
Correct answer Simonetos The Greek

Your more a Geek than I am I do not know Java and looking at your Java code I see

var csInterface = new CSInterface();

That is Greek to me. If you writing a Photoshop extension that well beyond my knowledge. I only hack a little at Photoshop scripting which has many limitations like you can not get at thing like cursor position  so the only way to get at color at locations is to use the color simpler tool to get at colors.  You need to know the locations  to set color sampler or you can manually use the tool to set up to 10 locations and then use Photoshop scripting to get the locations and get colors. I use the color sampler tools both way. I would think that Adobe would use   showColorPicker();  as a boolean in application extensions as well as in extendscript  to avoid confusion programmers.  But then we are dealing with Adobe development who knows what gone on between groups....  I still see you JSX code may return no value. else return;


Well solution found!!! 🙂

 

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 on stackexchange said, I was asking for the color before picking it and I was getting the color form the last time!!!

1 reply

JJMack
Community Expert
Community Expert
October 26, 2019

How are you using this function. How are you associating it with a click. What values are being passed for isPick and isForecolor and how are you using any color that may be returned.   The variable isPick and isForecolor seem to be used only as true false conditionals.  If isForecolor is true color_PicketCase is set to the foreground color if false the background color.  If  isPicker is true nothing will most likely be returned.  If  isPicker is false either the foreground or background color will be returned depends on the if isForecolor value passed was 1.

 

You seem to be using the boolean showColorPicker();  in a strange way  I do not know JavaScript so I do not know what using the isForecolor boolean in the showColorPicker  booleans method statement would do.  It should not accept any parameters IMO it just returns a boolean about the color picker dialog being canceled or not.

if (app.showColorPicker(isForecolor))

 

JJMack
Simonetos The Greek
Inspiring
October 26, 2019

Well, I think is better to show you my code from my JS file...

(function()
{
   'use strict';

    var csInterface = new CSInterface();

    var foregroundHexColor;
    var foregroundRGBColor;
    var backgroundHexColor;
    var backgroundRGBColor;
        
    var currentColorDiv = document.getElementById("current-color-div");
    var currentColorRedTextbox = document.getElementById("current-color-red-textbox");
    var currentColorGreenTextbox = document.getElementById("current-color-green-textbox");
    var currentColorBlueTextbox = document.getElementById("current-color-blue-textbox");
    var currentColorHexTextbox = document.getElementById("current-color-hex-textbox");

    var newColorDiv = document.getElementById("new-color-div");  
    var newColorRedTextbox = document.getElementById("new-color-red-textbox");
    var newColorGreenTextbox = document.getElementById("new-color-green-textbox");
    var newColorBlueTextbox = document.getElementById("new-color-blue-textbox");
    var newColorHexTextbox = document.getElementById("new-color-hex-textbox");
    
    init();
    
    function init()
    {
        themeManager.init();
        $(document).ready(function()
        {
            getColors(false, true); // Gets the foreground color when document is ready.
            getColors(false, false); // Gets the background color when document is ready.
        });
        $("#current-color-pick-button").click(function()
        {
            getColors(true, true); // Gets the foreground color when "Pick" button clicked.
        });
        $("#new-color-pick-button").click(function()
        {
            getColors(true, false); // Gets the background color when "Pick" button clicked.
        });
    };

    function hexToRGB(hex)
    {
	    var r = hex >> 16;
	    var g = hex >> 8 & 0xFF;
	    var b = hex & 0xFF;
        return [r,g,b];
    };
    function getColors(isPick, isForecolor)
    {
        var chosenFunction = 'getColor(' + isPick + ',' + isForecolor + ')';       
        csInterface.evalScript(chosenFunction, function(result)
        {
            if(result !== 'undefined')
            {
                if (isForecolor == true){
                    foregroundHexColor = result;
                    currentColorDiv.style.backgroundColor = "#" + foregroundHexColor;
                    currentColorHexTextbox.value = "#" + foregroundHexColor;
                    foregroundRGBColor = hexToRGB(parseInt(foregroundHexColor, 16));
                    currentColorRedTextbox.value = foregroundRGBColor[0]
                    currentColorGreenTextbox.value = foregroundRGBColor[1]
                    currentColorBlueTextbox.value = foregroundRGBColor[2]
                }
                else
                {
                    backgroundHexColor = result;
                    newColorDiv.style.backgroundColor = "#" + backgroundHexColor;
                    newColorHexTextbox.value = "#" + backgroundHexColor;
                    backgroundRGBColor = hexToRGB(parseInt(backgroundHexColor, 16));
                    newColorRedTextbox.value = backgroundRGBColor[0]
                    newColorGreenTextbox.value = backgroundRGBColor[1]
                    newColorBlueTextbox.value = backgroundRGBColor[2]
                };
            };
        });
    };
}());

 

JJMack
Community Expert
Community Expert
October 26, 2019

Your more a Geek than I am I do not know Java and looking at your Java code I see

var csInterface = new CSInterface();

That is Greek to me. If you writing a Photoshop extension that well beyond my knowledge. I only hack a little at Photoshop scripting which has many limitations like you can not get at thing like cursor position  so the only way to get at color at locations is to use the color simpler tool to get at colors.  You need to know the locations  to set color sampler or you can manually use the tool to set up to 10 locations and then use Photoshop scripting to get the locations and get colors. I use the color sampler tools both way. I would think that Adobe would use   showColorPicker();  as a boolean in application extensions as well as in extendscript  to avoid confusion programmers.  But then we are dealing with Adobe development who knows what gone on between groups....  I still see you JSX code may return no value. else return;

JJMack