[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Miniwindows
. . -> [Subject]  Drawing PNG images with an alpha mask - new in version 4.42

Drawing PNG images with an alpha mask - new in version 4.42

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Mon 06 Jul 2009 05:39 AM (UTC)

Amended on Mon 06 Jul 2009 06:09 AM (UTC) by Nick Gammon

Message
Version 4.42 of MUSHclient introduces a new script function WindowDrawImageAlpha. This behaves in a similar way to WindowDrawImage however it lets you draw a PNG image which has an imbedded alpha channel, and respects the alpha information in such an image.

The images in the next posting show the general idea. What this lets you do is have images with soft edges. Previously an image had to be rectangular, with a possible single transparent colour through which you could see the underlying image. Now you can make an alpha channel (with a suitable painting program) which lets you gradually blend into the background.

An example of doing this is here:


win = GetPluginID ()  -- get a unique name
WindowCreate (win, 0, 0, 365, 285, 12, 0, ColourNameToRGB("white"))  -- create window

-- Grid
  for i = 1, math.max (WindowInfo (win, 3), WindowInfo (win, 4)) / 20 do
    WindowLine (win, i * 20, 0, i * 20, WindowInfo (win, 4), 0xC0C0C0, 0, 1)
    WindowLine (win, 0, i * 20, WindowInfo (win, 3), i * 20, 0xC0C0C0, 0, 1)
  end -- for

check (WindowLoadImage (win, "im", "C:/alpha_test.png"))

check (WindowDrawImageAlpha (win, "im", 20, 20, 0, 0, 1)  )

WindowShow (win,  true)  -- show it 


Previously you could do something similar with WindowMergeImageAlpha - however this required two images, which was a bit fiddly to set up, whereas this new function just lets you use a single existing image with an alpha channel.

The alpha channel effectively is a 4th byte per pixel (the others being the red, green and blue components of the pixel). This 4th byte specifies the transparency of the pixel, from fully transparent (0) to fully opaque (255), in the range 0 to 255. As you can see from the mask in the image below the black parts (which have a value of 0) are transparent, so the background shows through. The white parts (which have a value of 255) are opaque, so you do not see the background. The grey parts (somewhere between 1 and 254) let the background image show through partially.

This will only work for PNG images which have an alpha channel imbedded in them.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #1 on Mon 06 Jul 2009 05:42 AM (UTC)
Message

Original image:

Alpha mask in Photoshop:

Resulting image in Photoshop (checkerboard shows transparent area):

Resulting miniwindow in MUSHclient drawn using code above:


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #2 on Mon 06 Jul 2009 06:26 AM (UTC)

Amended on Mon 06 Jul 2009 11:14 AM (UTC) by Nick Gammon

Message
The function prototype for WindowDrawImageAlpha is:


long WindowDrawImageAlpha(BSTR Name, BSTR ImageId, long Left, long Top, long Right, long Bottom, double Opacity, long SrcLeft, long SrcTop);


In addition to the window name, and image ID, you specify where the image is to be drawn (Left, Top, Right, Bottom), the Opacity (from 0 to 1) and the starting position of the image in the loaded image.

The Opacity parameter lets you draw semi-transparent images (apart from any extra transparency introduced by the alpha mask). So for example, by specifying an opacity of 0.5 you would have a "ghostly" image drawn on top of the background.

SrcLeft and SrcTop let you specify to pull out a sub-image from the loaded image. Thus, a loaded image could be a "tiled" set of icons, and by specifying the left and top of each tile, you would get part of the image.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #3 on Mon 06 Jul 2009 06:30 AM (UTC)
Message

Resulting miniwindow in MUSHclient drawn with 50% opacity (ie. an Opacity value of 0.5):


- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by David Haley   USA  (3,881 posts)  [Biography] bio
Date Reply #4 on Mon 06 Jul 2009 03:20 PM (UTC)
Message
Very cool. It's starting to feel like one could write an entire custom graphical client in MUSHclient. :-)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Mon 06 Jul 2009 11:42 PM (UTC)
Message
Funny you should say that ...

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


18,850 views.

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]