The reason it is inverted is because…
- toolbar button images (icons) use white for opaque pixels and black for transparent
- UI button images use black for opaque pixels and white for transparent
Welcome to the world of left and right hands not knowing what the others are doing 
You can invert the mask quite easily, but keep in mind that toolbar button images can have a greyscale mask (varying levels of transparency), whereas UI button images only take black/white. So you’ll need a threshold parameter to determine which greyscale levels will be black, and which will be white (typically 50%, I’d imagine, but other values may work better):
fn iconMaskToButtonMaskSimple maskBmp threshold:127.5 = (
local bmpHeightMin1 = maskBmp.height - 1
local bmpWidth = maskBmp.width
local scanline
for y = 0 to (bmpHeightMin1) do (
scanline = getPixels maskBmp [0,y] bmpWidth
for x = 1 to bmpWidth do (
scanline[x] = (if (scanline[x].v < threshold) then ( (color 0 0 0) ) else ( (color 255 255 255) ) )
)
setPixels maskBmp [0,y] scanline
)
maskBmp
)
-- usage example:
-- m_toolbarbutton = openBitMap (iconDir + "\\Maintoolbar_24a.bmp")
-- m_uibutton = iconMaskToButtonMaskSimple m_toolbarbutton threshold:127.5
-- display m_uibutton
Alternatively, however, you could generate the button images -with- the apparent varying levels of transparency by constructing a new bitmap yourself with as the background color the UI background color (typically greyish, but some users have their max set to the Dark scheme, etc.). That’s a wee bit more involved, but here’s the code:
fn iconToButtonImage iconBmp maskBmp = (
local bmpHeightMin1 = maskBmp.height - 1
local bmpWidth = maskBmp.width
local uiBackgroundColor = (colorman.getcolor #background as color) * 255
local iconScanline, maskScanline
local iconPixel, maskPixel, newPixel
local mult
for y = 0 to (bmpHeightMin1) do (
iconScanline = getPixels iconBmp [0,y] bmpWidth
maskScanline = getPixels maskBmp [0,y] bmpWidth
for x = 1 to bmpWidth do (
iconPixel = iconScanline[x]
maskPixel = maskScanline[x]
mult = maskPixel.v / 255.0
newPixel = (mult * iconPixel) + ((1 - mult) * uiBackgroundColor)
if (newPixel.r > 255) do ( newPixel.r = 255 )
if (newPixel.g > 255) do ( newPixel.g = 255 )
if (newPixel.b > 255) do ( newPixel.b = 255 )
newPixel.alpha = 0
iconScanline[x] = newPixel
)
setPixels iconBmp [0,y] iconScanline
)
iconBmp
)
-- usage example:
-- i_toolbarbutton = openBitMap (iconDir + "\\Maintoolbar_24i.bmp")
-- m_toolbarbutton = openBitMap (iconDir + "\\Maintoolbar_24a.bmp")
-- i_uibutton = iconToButtonImage i_toolbarbutton m_toolbarbutton
-- display i_uibutton
Note that the above code assumes that the input icon image and alpha are a non-premultiplied alpha combination (_XXa.bmp), and not pre-multiplied (_XXp.bmp), etc.
I hope this helps %)