/* "Create transparent overlay from selection v231206: 1st Version Peter J. Lee, Applied Superconductivity Center v231207: Added stroke options. b: Names overlays. v231208: Expanded selections recognized to include composite ROIs. Changed naming from transparency to opacity to match # name. Added add/replace options if there are already overlays on the image. v231209: Removed redundant code leftover from earlier versions. F1: Updated getColorFromColorName function (012324). */ macro "Create transparent overlay from selection" { macroL = "Create_Transparent_Overlay_v231209-f1.ijm"; prefsNameKey = "ascTranspOverlay."; lastBox = call("ij.Prefs.get", prefsNameKey + "roi.box", ""); selType = selectionType; /* Returns the selection type, where 0=rectangle, 1=oval, 2=polygon, 3=freehand, 4=traced, 5=straight line, 6=segmented line, 7=freehand line, 8=angle, 9=composite and 10=point. Returns -1 if there is no selection. */ if ((selType<0 || selType>4) && selType!=9){ usePreviousBox = false; if (lastBox!=""){ previousBox = split(lastBox,","); if (lengthOf(previousBox)==4) usePreviousBox = getBoolean("There is no selection, do you want to use the previously saved bounding box?"); } if (usePreviousBox) makeRectangle(parseInt(previousBox[0]), parseInt(previousBox[1]), parseInt(previousBox[2]), parseInt(previousBox[3])); selType = selectionType; } if ((selType<0 || selType>4) && selType!=9){ msg = "Draw a bounding box around the ROIs to be selected"; waitForUser("Please select the ROIs", msg); selType = selectionType; } if (selType>=0 && (selType<4 || selType==9)){ getSelectionBounds(selPosStartX, selPosStartY, originalSelEWidth, originalSelEHeight); call("ij.Prefs.set", prefsNameKey + "roi.box","" + selPosStartX + "," + selPosStartY + "," + originalSelEWidth + "," + originalSelEHeight); } overlayN = Overlay.size(); Dialog.create("Overlay options \(" + macroL + "\)"); grayChoices = newArray("white", "black", "off-white", "off-black", "light_gray", "gray", "dark_gray"); colorChoicesStd = newArray("red", "green", "blue", "cyan", "magenta", "yellow", "pink", "orange", "violet"); colorChoicesMod = newArray("garnet", "gold", "aqua_modern", "blue_accent_modern", "blue_dark_modern", "blue_modern", "blue_honolulu", "gray_modern", "green_dark_modern", "green_modern", "green_modern_accent", "green_spring_accent", "orange_modern", "pink_modern", "purple_modern", "red_n_modern", "red_modern", "tan_modern", "violet_modern", "yellow_modern"); colorChoicesNeon = newArray("jazzberry_jam", "radical_red", "wild_watermelon", "outrageous_orange", "supernova_orange", "atomic_tangerine", "neon_carrot", "sunglow", "laser_lemon", "electric_lime", "screamin'_green", "magic_mint", "blizzard_blue", "dodger_blue", "shocking_pink", "razzle_dazzle_rose", "hot_magenta"); colorChoices = Array.concat(grayChoices, colorChoicesStd, colorChoicesMod, colorChoicesNeon); colorFillChoice = call("ij.Prefs.get", prefsNameKey + "colorFillChoice", "white"); Dialog.addChoice("Select Fill color", colorChoices, colorFillChoice); opacFillN = parseInt(call("ij.Prefs.get", prefsNameKey + "opacFillN", "50")); Dialog.addSlider("Fill opacity \(0 = No Fill\)", 0, 100, opacFillN); colorStrokeChoice = call("ij.Prefs.get", prefsNameKey + "colorStrokeChoice", "white"); Dialog.addChoice("Select color", colorChoices, colorStrokeChoice); opacStrokeN = parseInt(call("ij.Prefs.get", prefsNameKey + "opacStrokeN", "50")); Dialog.addSlider("Stroke opacity", 0, 100, opacStrokeN); strokeWidth = parseInt(call("ij.Prefs.get", prefsNameKey + "strokeWidth", "0")); Dialog.addNumber("Stroke width \(0 = No stroke \)", strokeWidth, 0, 4, "pixels"); if (overlayN>0){ replaceOverlay = call("ij.Prefs.get", prefsNameKey + "replaceOverlay", true); Dialog.addCheckbox("Replace current with new overlay", replaceOverlay); } flattenIt = call("ij.Prefs.get", prefsNameKey + "flattenIt", true); Dialog.addCheckbox("Create flattened copy of image after overlay creation", flattenIt); removeOv = call("ij.Prefs.get", prefsNameKey + "removeOv", true); Dialog.addCheckbox("Remove overlay from original image", removeOv); Dialog.addMessage("The non-destructive overlays will be saved in the TIFF header\nif you save the image as an uncompressed TIFF file"); Dialog.show(); colorFillChoice = Dialog.getChoice(); call("ij.Prefs.set", prefsNameKey + "colorFillChoice", colorFillChoice); opacFillN = parseInt(Dialog.getNumber()); call("ij.Prefs.set", prefsNameKey + "opacFillN", opacFillN); colorStrokeChoice = Dialog.getChoice(); call("ij.Prefs.set", prefsNameKey + "colorStrokeChoice", colorStrokeChoice); opacStrokeN = parseInt(Dialog.getNumber()); call("ij.Prefs.set", prefsNameKey + "opacStrokeN", opacStrokeN); strokeWidth = parseInt(Dialog.getNumber()); call("ij.Prefs.set", prefsNameKey + "strokeWidth", strokeWidth); if (overlayN>0){ replaceOverlay = Dialog.getCheckbox(); call("ij.Prefs.set", prefsNameKey + "replaceOverlay", replaceOverlay); } else replaceOverlay = false; flattenIt = Dialog.getCheckbox(); call("ij.Prefs.set", prefsNameKey + "flattenIt", flattenIt); removeOv = Dialog.getCheckbox(); call("ij.Prefs.set", prefsNameKey + "removeOv", removeOv); ovName = ""; if (opacFillN==0 && (strokeWidth==0 || opacStrokeN==0)) exit ("It is 'transparent' that there is nothing to do here!"); else { colFillHex = getHexColorFromColorName(colorFillChoice); if (opacFillN>0) opacFillHex = String.pad(toHex(255 * opacFillN/100), 2); else opacFillHex = ""; ovName += "" + colorFillChoice + " fill "; colFillHex = replace(colFillHex, "#", opacFillHex); ovOptions = "fill=" + colFillHex; } if (strokeWidth>0 && opacStrokeN>0){ colStrokeHex = getHexColorFromColorName(colorStrokeChoice); if (opacStrokeN>0) opacStrokeHex = String.pad(toHex(255 * opacStrokeN/100), 2); else opacStrokeHex = ""; colStrokeHex = replace(colStrokeHex, "#", opacStrokeHex); ovName += "" + colorStrokeChoice + " border"; ovOptions += " width=" + strokeWidth + " stroke=" + colStrokeHex; } if (replaceOverlay) ovOptions += " new"; setSelectionName(ovName); /* Makes it easier to identify colors vs # in overlay descriptions in the overlay manager */ run("Add Selection...", ovOptions); run("Select None"); wait(100); overlayedID = getImageID(); if (flattenIt){ run("Flatten"); flatID = getImageID(); } if (removeOv){ selectImage(overlayedID); run("Remove Overlay"); if (flattenIt) selectImage(flatID); } showStatus(macroL + " macro finished", "flash green"); } /* ASC Modified BAR Color Functions */ function getColorArrayFromColorName(colorName) { /* v180828 added Fluorescent Colors v181017-8 added off-white and off-black for use in gif transparency and also added safe exit if no color match found v191211 added Cyan v211022 all names lower-case, all spaces to underscores v220225 Added more hash value comments as a reference v220706 restores missing magenta v230130 Added more descriptions and modified order. v230908: Returns "white" array if not match is found and logs issues without exiting. v240123: Removed duplicate entries: Now 53 unique colors */ functionL = "getColorArrayFromColorName_v240123"; cA = newArray(255,255,255); /* defaults to white */ if (colorName == "white") cA = newArray(255,255,255); else if (colorName == "black") cA = newArray(0,0,0); else if (colorName == "off-white") cA = newArray(245,245,245); else if (colorName == "off-black") cA = newArray(10,10,10); else if (colorName == "light_gray") cA = newArray(200,200,200); else if (colorName == "gray") cA = newArray(127,127,127); else if (colorName == "dark_gray") cA = newArray(51,51,51); else if (colorName == "red") cA = newArray(255,0,0); else if (colorName == "green") cA = newArray(0,255,0); /* #00FF00 AKA Lime green */ else if (colorName == "blue") cA = newArray(0,0,255); else if (colorName == "cyan") cA = newArray(0, 255, 255); else if (colorName == "yellow") cA = newArray(255,255,0); else if (colorName == "magenta") cA = newArray(255,0,255); /* #FF00FF */ else if (colorName == "pink") cA = newArray(255, 192, 203); else if (colorName == "violet") cA = newArray(127,0,255); else if (colorName == "orange") cA = newArray(255, 165, 0); else if (colorName == "garnet") cA = newArray(120,47,64); /* #782F40 */ else if (colorName == "gold") cA = newArray(206,184,136); /* #CEB888 */ else if (colorName == "aqua_modern") cA = newArray(75,172,198); /* #4bacc6 AKA "Viking" aqua */ else if (colorName == "blue_accent_modern") cA = newArray(79,129,189); /* #4f81bd */ else if (colorName == "blue_dark_modern") cA = newArray(31,73,125); /* #1F497D */ else if (colorName == "blue_honolulu") cA = newArray(0,118,182); /* Honolulu Blue #006db0 */ else if (colorName == "blue_modern") cA = newArray(58,93,174); /* #3a5dae */ else if (colorName == "gray_modern") cA = newArray(83,86,90); /* bright gray #53565A */ else if (colorName == "green_dark_modern") cA = newArray(121,133,65); /* Wasabi #798541 */ else if (colorName == "green_modern") cA = newArray(155,187,89); /* #9bbb59 AKA "Chelsea Cucumber" */ else if (colorName == "green_modern_accent") cA = newArray(214,228,187); /* #D6E4BB AKA "Gin" */ else if (colorName == "green_spring_accent") cA = newArray(0,255,102); /* #00FF66 AKA "Spring Green" */ else if (colorName == "orange_modern") cA = newArray(247,150,70); /* #f79646 tan hide, light orange */ else if (colorName == "pink_modern") cA = newArray(255,105,180); /* hot pink #ff69b4 */ else if (colorName == "purple_modern") cA = newArray(128,100,162); /* blue-magenta, purple paradise #8064A2 */ else if (colorName == "jazzberry_jam") cA = newArray(165,11,94); else if (colorName == "red_n_modern") cA = newArray(227,24,55); else if (colorName == "red_modern") cA = newArray(192,80,77); else if (colorName == "tan_modern") cA = newArray(238,236,225); else if (colorName == "violet_modern") cA = newArray(76,65,132); else if (colorName == "yellow_modern") cA = newArray(247,238,69); /* Fluorescent Colors https://www.w3schools.com/colors/colors_crayola.asp */ else if (colorName == "radical_red") cA = newArray(255,53,94); /* #FF355E */ else if (colorName == "wild_watermelon") cA = newArray(253,91,120); /* #FD5B78 */ else if (colorName == "shocking_pink") cA = newArray(255,110,255); /* #FF6EFF Ultra Pink */ else if (colorName == "razzle_dazzle_rose") cA = newArray(238,52,210); /* #EE34D2 */ else if (colorName == "hot_magenta") cA = newArray(255,0,204); /* #FF00CC AKA Purple Pizzazz */ else if (colorName == "outrageous_orange") cA = newArray(255,96,55); /* #FF6037 */ else if (colorName == "supernova_orange") cA = newArray(255,191,63); /* FFBF3F Supernova Neon Orange*/ else if (colorName == "sunglow") cA = newArray(255,204,51); /* #FFCC33 */ else if (colorName == "neon_carrot") cA = newArray(255,153,51); /* #FF9933 */ else if (colorName == "atomic_tangerine") cA = newArray(255,153,102); /* #FF9966 */ else if (colorName == "laser_lemon") cA = newArray(255,255,102); /* #FFFF66 "Unmellow Yellow" */ else if (colorName == "electric_lime") cA = newArray(204,255,0); /* #CCFF00 */ else if (colorName == "screamin'_green") cA = newArray(102,255,102); /* #66FF66 */ else if (colorName == "magic_mint") cA = newArray(170,240,209); /* #AAF0D1 */ else if (colorName == "blizzard_blue") cA = newArray(80,191,230); /* #50BFE6 Malibu */ else if (colorName == "dodger_blue") cA = newArray(9,159,255); /* #099FFF Dodger Neon Blue */ else IJ.log(colorName + " not found in " + functionL + ": Color defaulted to white"); return cA; } /* Hex conversion below adapted from T.Ferreira, 20010.01 https://imagej.net/doku.php?id=macro:rgbtohex */ function getHexColorFromColorName(colorNameString) { /* v231207: Uses IJ String.pad instead of function: pad */ colorArray = getColorArrayFromColorName(colorNameString); r = toHex(colorArray[0]); g = toHex(colorArray[1]); b = toHex(colorArray[2]); hexName= "#" + "" + String.pad(r, 2) + "" + String.pad(g, 2) + "" + String.pad(b, 2); return hexName; }