It is currently October 22nd, 2019, 6:07 pm

Use a stretched image to create an image meter border

Tips and Tricks from the Rainmeter Community
User avatar
jsmorley
Developer
Posts: 19613
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Use a stretched image to create an image meter border

jsmorley » September 1st, 2013, 7:33 pm

If you want to have a solid border around an image meter that you want to be semi-transparent, this can be a bit difficult. If you use SolidColor on the image meter or a background image meter, the alpha (opacity) of the SolidColor background will be "added" to the actual image you are displaying.

Another option is to use four image meters to create the border. This is just really a lot of work, a lot of extra meters, and reasonably complicated to get the positioning right.

What you can do is:

Create an image in PhotoShop or Gimp or whatever you use. Have it be fully transparent, with a solid border of as many pixels as you like. The size of this image is not a bit important. Make it as small as you like, as long as the "borders" fit within it and there is still some "transparent" area in the middle. Make the border on the image a solid "white" color. We will use ImageTint later to make it anything we want. Make sure to save the images as .png, to preserve the transparency in the middle.

This is just a 50 x 50 image with a 1 pixel white border. I zoomed it out so you can see it better:
ImageBorder2.jpg
Make as many of these as you like, I did images for border widths of 1, 2, 5, 10 and 15. Heck, use mine. (they are included in the .rmskin below) They will work with any skin.
ImageBorder3.jpg
Now you can use that image "behind" your actual image meter, stretching it with the W and H options to the size you want.

However! If you simply stretch the border image(s), the white border part itself will be stretched, creating a horrible problem with the image anti-aliasing that Rainmeter automatically does, as well as issues with the aspect ratio of the borders.
ImageBorder4.jpg
So what you do to avoid this, is use the ScaleMargins option, to avoid stretching the part of the image that contains the border. Stretch the transparent middle part as much as you like, that doesn't matter.

Set ScaleMargins to the size of the border you want to "protect" from scaling, plus 1 pixel. So to protect a border of 5, you would use ScaleMargins=6,6,6,6.
ImageBorder5.jpg
Here is a sample skin that demonstrates:

Code: Select all

[Rainmeter]
Update=1000

[Metadata]
Name=ImageBorder
Author=JSMorley
Information=Shows how to use a stretched image and ScaleMargins to create a solid border around a semi-transparent image.
License=Creative Commons Attribution-Non-Commercial-Share Alike 3.0
Version=Sep 1, 2013

[Variables]
;Possible values are 1, 2, 5, 10 or 15
BorderSize=10
BorderColor=250,245,187
BorderAlpha=255

[MeterBorder]
Meter=Image
ImageName=#@#Images\border#BorderSize#.png
W=([MeterImage:W] + (#BorderSize# * 2))
H=([MeterImage:H] + (#BorderSize# * 2))
ImageTint=#BorderColor#,#BorderAlpha#
ScaleMargins=(#BorderSize# + 1),(#BorderSize# + 1),(#BorderSize# + 1),(#BorderSize# + 1)
DynamicVariables=1
LeftMouseUpAction=["#@#Images\"]

[MeterImage]
Meter=Image
X=([MeterBorder:X] + #BorderSize#)
Y=([MeterBorder:Y] + #BorderSize#)
ImageName=#@#Images\laughing.jpg
ImageAlpha=190
W=400
PreserveAspectRatio=2
DynamicVariables=1
ImageBorder_1.0.rmskin
ImageBorder.jpg
You do not have the required permissions to view the files attached to this post.
User avatar
iNjUST
Posts: 118
Joined: June 20th, 2012, 12:44 am

Re: Use a stretched image to create an image meter border

iNjUST » September 1st, 2013, 8:43 pm

Nice work!

But why the +1 to the border size when doing the margins? Assuming your border images are pixel-perfect, it shouldn't make any difference.

Also, I can see ways to use this with more complex border shapes/styles.
User avatar
jsmorley
Developer
Posts: 19613
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Use a stretched image to create an image meter border

jsmorley » September 1st, 2013, 8:46 pm

iNjUST wrote:Nice work!

But why the +1 to the border size when doing the margins? Assuming your border images are pixel-perfect, it shouldn't make any difference.

Also, I can see ways to use this with more complex border shapes/styles.
PhotoShop itself is doing some really subtle anti-aliasing when transitioning between the solid color and the transparent middle, so that if I have a white border of 5 pixels, PhotoShop is messing with pixel 6 to smooth the transition. I'll have to play with the image, I suspect there may be a way to get a clean edge.
Wallboy
Posts: 68
Joined: October 1st, 2012, 4:53 am

Re: Use a stretched image to create an image meter border

Wallboy » August 24th, 2014, 3:36 am

jsmorley wrote:Make it as small as you like, as long as the "borders" fit within it and there is still some "transparent" area in the middle.
I've tried making a 3x3 .png with white around the border and a single transparent pixel in the middle and get some weird stretched image with ScaleMargins=2,2,2,2. So I guess we can't make it as small as we want. I've tried slightly larger sizes and get some weird scaling as well. So I just stuck with a 50x50 which seems to work. But I'm curious if there is a way to find out how small we can actually make it? Not that those few little bytes I would save really matter...
User avatar
jsmorley
Developer
Posts: 19613
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Use a stretched image to create an image meter border

jsmorley » August 24th, 2014, 4:09 am

Wallboy wrote: I've tried making a 3x3 .png with white around the border and a single transparent pixel in the middle and get some weird stretched image with ScaleMargins=2,2,2,2. So I guess we can't make it as small as we want. I've tried slightly larger sizes and get some weird scaling as well. So I just stuck with a 50x50 which seems to work. But I'm curious if there is a way to find out how small we can actually make it? Not that those few little bytes I would save really matter...
I don't really know. The code might be having trouble trying to figure out how to stretch an image that ends up consisting of one pixel. I went with an approach where the image was small, but didn't try to push it as far as possible. A 50 X 50 png is around 2,853 bytes, and a 6 X 6 png is about 2,811 bytes. 42 bytes on my 2TB hard drive? I'll still be able to sleep tonight.
Wallboy
Posts: 68
Joined: October 1st, 2012, 4:53 am

Re: Use a stretched image to create an image meter border

Wallboy » August 24th, 2014, 4:50 am

Haha alright, yeah I'm just a little OCD with some things. I'll just leave it 50x50.