/* Expand Canvas... but with a 'relative' option so you don't have to do the math, background color options, can restore selection and can show original canvas with selection. v230612: 1st version Peter J. Lee NHMFL/FSU F1: getColorArrayFromColorName_v230908. F3 : Replaced function: pad. F4: Updated getColorFromColorName function (012324). */ macroL = "Expand_Canvas_ASC_v230612-f4.ijm"; saveSettings(); /* To restore settings at the end */ cW1 = Image.width; cH1 = Image.height; orBG = Color.background; imageDepth = bitDepth(); selType = selectionType; if (selType>=0) getSelectionBounds(selX1, selY1, null, null); medianBGIs = guessBGMedianIntensity(); medianBGI = round((medianBGIs[0]+medianBGIs[1]+medianBGIs[2])/3); bgI = maxOf(0,medianBGI); if(bgI==0) defBG = "black"; else if ((imageDepth==8 ||imageDepth==24) && bgI==255) defBG = "white"; else if (imageDepth==16 && bgI==65535) defBG = "white"; else defBG = "#" + "" + String.pad(toHex(medianBGIs[0]), 2) + "" + String.pad(toHex(medianBGIs[1]), 2) + "" + String.pad(toHex(medianBGIs[2]), 2); if (defBG=="white") grayChoices = newArray("white", "black", "off-white", "off-black", "light_gray", "gray", "dark_gray"); else grayChoices = newArray("black", "white", "off-white", "off-black", "light_gray", "gray", "dark_gray"); 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"); if (is("grayscale")) colorChoices = grayChoices; else colorChoices = Array.concat(grayChoices, colorChoicesStd, colorChoicesMod, colorChoicesNeon); if (defBG!="black" && defBG!="white") colorChoices = Array.concat(defBG,colorChoices); Dialog.create("Options: " + macroL); Dialog.addMessage("Original image width: " + cW1 + " pixels, original image height: " + cH1 + " pixels"); Dialog.addNumber("New canvas width",cW1,0,8,"pixels"); Dialog.addNumber("New canvas width",cH1,0,8,"pixels"); Dialog.addCheckbox("Use relative values below instead",true); Dialog.addNumber("Relative canvas width expansion \(negative values crop\)",0,0,8,"pixels"); Dialog.addNumber("Relative canvas height expansion\(negative values crop\)",0,0,8,"pixels"); anchorPositions = newArray("Top-Left","Top-Center","Top-Right","Center-Left","Center","Center-Right","Bottom-Left","Bottom-Center","Bottom-Right"); Dialog.addRadioButtonGroup("Relative to",anchorPositions,3,3,"Center"); Dialog.addChoice("Background expansion color",colorChoices,colorChoices[0]); if (selType<0) Dialog.addCheckbox("Add selection around original canvas",false); Dialog.show(); cW2 = Dialog.getNumber; cH2 = Dialog.getNumber; useRel = Dialog.getCheckbox(); cRW = Dialog.getNumber(); cRH = Dialog.getNumber(); relPos = Dialog.getRadioButton(); bgCol = Dialog.getChoice(); if (selType<0) oCanvasSel = Dialog.getCheckbox(); else oCanvasSel = false; if (useRel){ if(cRW==0 && cRH==0) exit("No values given for relative expansion"); cW2 = cW1 + cRW; cH2 = cH1 + cRH; } else{ cRW = cW2 - cW1; cRH = cH2 - cH1; } if (startsWith(relPos,"Top")) ySel = 0; else if (startsWith(relPos,"Center")) ySel = floor(cRH/2); else ySel = cRH; if (endsWith(relPos,"Left")) xSel = 0; else if (endsWith(relPos,"Center")) xSel = floor(cRW/2); else xSel = cRW; if (startsWith(bgCol,"#")) Color.setBackground(bgCol); else setBackgroundFromColorName(bgCol); if(oCanvasSel) run("Select All"); run("Select None"); run("Canvas Size...", "width="+cW2+" height="+cH2+" position=" + relPos); if (oCanvasSel || selType>=0){ run("Restore Selection"); if (selType>=0){ xSel += selX1; ySel += selY1; } setSelectionLocation(xSel, ySel); } Color.setBackground(orBG); /* May be redundant with restoreSettings */ restoreSettings(); showStatus("Canvas expanded using " + macroL); /* ( 8(|) ( 8(|) Functions @@@@@:-) @@@@@:-) */ function guessBGMedianIntensity(){ /* v220822 1st color array version (based on https://wsr.imagej.net//macros/tools/ColorPickerTool.txt) */ iW = Image.width-1; iH = Image.height-1; interrogate = round(maxOf(1,(iW+iH)/200)); if (bitDepth==24){red = 0; green = 0; blue = 0;} else int = 0; xC = newArray(0,iW,0,iW); yC = newArray(0,0,iH,iH); xAdd = newArray(1,-1,1,-1); yAdd = newArray(1,1,-1,-1); if (bitDepth==24){ reds = newArray(); greens = newArray(); blues = newArray();} else ints = newArray; for (i=0; i>16)&0xff); // extract red byte (bits 23-17) greens = Array.concat(greens,(v>>8)&0xff); // extract green byte (bits 15-8) blues = Array.concat(blues,v&0xff); // extract blue byte (bits 7-0) } else ints = Array.concat(ints,getValue(xC[i]+j*xAdd[i],yC[i]+j*yAdd[i])); } } midV = round((xC.length-1)/2); if (bitDepth==24){ reds = Array.sort(reds); greens = Array.sort(greens); blues = Array.sort(blues); medianVals = newArray(reds[midV],greens[midV],blues[midV]); } else{ ints = Array.sort(ints); medianVals = newArray(ints[midV],ints[midV],ints[midV]); } return medianVals; } /* BAR-based 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; } function setBackgroundFromColorName(colorName) { colorArray = getColorArrayFromColorName(colorName); setBackgroundColor(colorArray[0], colorArray[1], colorArray[2]); }