// Selection Color Menu Tool

// This macro set adds a menu to the toolbar that allows you to
// easily change the selection color. Also adds commands to
// Plugins>Menus to restore the previous selection color and
// to get the current color.

// Author: Gilles Carpentier, Faculte des Sciences et
// Technologies,  Universite Paris 12 Val de Marne, France

var xx = requires138b(); // check version at install
function requires138b() {requires("1.38b"); return 0; }

var Red="", Green="", Blue="", color ="", previousColor="";
var cCmds = newMenu("Selection Color Menu Tool",newArray("yellow","red","green","blue","magenta","cyan","orange","black","white"));

macro "Selection Color Menu Tool -  " {
	cmd=getArgument();
	previousColor= call("ij.gui.Roi.getColor");
	run("Colors...", "selection=["+cmd+"]");
	getSelectionColors(call("ij.gui.Roi.getColor"));
	showStatus("Selection color is now " + getColorName(Red,Green,Blue));
}

macro "Restore Previous Selection Color" {
	if (previousColor !="") {
		getSelectionColors (previousColor);
		run("Colors...", "selection=["+getColorName(Red,Green,Blue)+"]");
		showMessage ("Previous selection color \"" + getColorName(Red,Green,Blue) + "\" has been restored");
	} else {showMessage ("Selection Color has not been changed");
}

macro "Get Curent Selection Color" {
	color= call("ij.gui.Roi.getColor");
	getSelectionColors (color);
	showMessage ("Curent selection color: "+ getColorName(Red,Green,Blue) + " \(Red="+Red+" Green="+Green+" Blue="+Blue+"\)");
}

// decodes a color in the form "java.awt.Color[r=255,g=255,b=0]"
function getSelectionColors (color) {
	Red = substring(color, (indexOf(color, "r=")+2), (indexOf(color, "g=")-1));
	Green=  substring(color, (indexOf(color, "g=")+2), (indexOf(color, "b=")-1));
	Blue=  substring(color, (indexOf(color, "b=")+2), indexOf(color, "]"));
}

function getColorName(r,g,b) {
	if (r=="255" && g=="255" && b=="0") ColorName ="yellow";
	if (r=="255" && g=="0" && b=="0") ColorName ="red";
	if (r=="0" && g=="255" && b=="0") ColorName ="green";
	if (r=="0" && g=="0" && b=="255") ColorName ="blue";
	if (r=="255" && g=="0" && b=="255") ColorName ="magenta";
	if (r=="0" && g=="255" && b=="255") ColorName ="cyan";
	if (r=="255" && g=="200" && b=="0") ColorName ="orange";
	if (r=="0" && g=="0" && b=="0") ColorName ="black";
	if (r=="255" && g=="255" && b=="255") ColorName ="white";
	return ColorName;
}
