Draw edge of the picture with shader

I got the simple idea to represent the unit life percent by a color (green -> yellow -> red). But the problem was how to keep the texture of my unit and to add the possibility to see the life color ?
I could have used a simple sphere or bar close to the unit but I didn't really liked it. I also tried to colorize the unit but the result is not really beautiful.

One of my idea was to draw the edge of the Texture with the color. And one of the easiest (and also fastest) way to do that is to use Shaders. I'll now describe those simple Shaders in GLSL

This technique is supposed to run with textures using alpha and is more accurate if the texture is well delimited

The Theory

This shader is really a simple one.
* Check if the pixel is enough transparent to be considered out of the texture
* Find if one of the neighbors is part of the texture
* Apply the color

I decided to use the color given by glColor but you can use a variable.
This is maybe not the fastest way to code this algorithm but I just started to write shaders and I'm not really aware with tips and optimizations.

Be aware the the values to find the neighbors can depend of the texture size.

The Pixel shader

uniform sampler2D tex;

void main (void)
{
  // get the texture color of the pixel
  gl_FragColor = texture2D(tex, gl_TexCoord[0].st);

  // if it is enough transparent, we consider it out of the texture
  if (gl_FragColor.a <= 0.2) {

    // if one of the neighbors is not transparent (1 neighbor / side but can be used on corners too)
    if (texture2D(tex, gl_TexCoord[0].st-vec2(0.0,0.008)).a >= 0.2 ||
      texture2D(tex, gl_TexCoord[0].st+vec2(0.0,0.008)).a >= 0.2 ||
      texture2D(tex, gl_TexCoord[0].st-vec2(0.008,0.0)).a >= 0.2 ||
      texture2D(tex, gl_TexCoord[0].st+vec2(0.008,0.0)).a >= 0.2) {
     
      // draw the edge color
      gl_FragColor = vec4(gl_Color.r,gl_Color.g,gl_Color.b, 1.0);
    }
  }
}

The Vertex shader
It is just a simple vertex shader as example too use the pixel shader.

uniform float time;

void main(void)
{
  gl_Position = ftransform();
  gl_TexCoord[0] = gl_MultiTexCoord0;
  gl_FrontColor = gl_Color;
}

GLSL color texture edges