Setting foreground color via a variable


#1

Hello. I am trying the following lines, first run(“Colors…” is working but second is not (gives error). Whats the right syntax for setting this color from an array element (or single variable)? According to last print line, all seems in place…?

forecol="255,0,255";
//working
run("Colors...", "foreground="+"[red]"+"");
//not working
run("Colors...", "foreground="+forecol+"+");

print("foreground="+forecol+"");

#2

In an ImageJ macro you can use to select the foreground color:

setForegroundColor(255, 105, 0);

or

col = newArray(250, 50, 200);
setForegroundColor(col[0],col[1],col[2]);

See:

https://imagej.nih.gov/ij/developer/macro/functions.html#S

https://imagej.nih.gov/ij/macros/examples/ForegroundBackgroundColors.txt


#3

@Bio7, thank you for the answer. My code is like below;

// @ColorRGB(label = "Color of 1st label: ", value = "yellow") lcolor1
// @ColorRGB(label = "Color of 2nd label: ", value = "blue") lcolor2
// @ColorRGB(label = "Color of 3rd label: ", value = "red") lcolor3
colorlist=newArray(3);
colorlist[0]=lcolor1;
colorlist[1]=lcolor2;
colorlist[2]=lcolor3;

Then I am drawing some text, with a color that depends on an array of unique elements.

objectlist="lemon", "sky", "fire");

If (a comparison result) comes out as lemon, then set foreground color to yellow, then draw text with the color from colorlist, use element at same array index.

colorlist=("yellow", "blue", "red");

But unfortunately, wth the color selection method above (content of lcolor1), colorlist[0] value is not “yellow”; but 255,255,0 which cannot be fed into any color setting command.

With your method, after user select the color from big palette, should I convert these values to arrays, too?

colorlist0=(255,255,0);
colorlist1=(0,0,255);
colorlistx=(143,254,27);
etc..

#4

This is a limitation of the IJ1 macro language that doesn’t know objects of type ColorRGB, so for macros, the Script Parameter framework has to work with a String representation of the colors.

You can do something like this to work around the issue:

c = split(colorlist[0], ",")
setColor(parseInt(c[0]), parseInt(c[1]), parseInt(c[2]));

#5

Thank you very much, I was about to give up. Yes it seems the only possible way to feed RGB.


#6

If you switch to a full-fledged scripting language like Groovy (or Python, Javascript, Scala, etc.), you’ll be able to use the ColorRGB instances directly (converting to java.awt.Color for IJ1 ROIs):

// @ColorRGB(label = "Color of 1st label: ", value = "yellow") lcolor1
// @ColorRGB(label = "Color of 2nd label: ", value = "blue") lcolor2
// @ColorRGB(label = "Color of 3rd label: ", value = "red") lcolor3
// @ImagePlus imp

import ij.gui.TextRoi
import org.scijava.ui.awt.AWTColors

colorlist=[]
colorlist.add(lcolor1)
colorlist.add(lcolor2)
colorlist.add(lcolor3)

textRoi = new TextRoi(10, 10, "My Label Text")

textRoi.setColor(AWTColors.getColor(colorlist[2]))

imp.setRoi(textRoi)

#7

Thanks, up to now I mostly complained about array functions of this macro language. Im afraid I have no knowledge on javascript or other languages, how can I integrate the above script code into an IJ macro? (I remember there was an “eval” thing, which I have no idea about…)


#8

You can learn here:

Yep, you want to use eval("script", scriptCode) where scriptCode is the actual code you want to run. See the documentation.