Setting foreground color via a variable


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…?

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



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

setForegroundColor(255, 105, 0);


col = newArray(250, 50, 200);



@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

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?



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]));


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


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


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




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…)


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.