To give you an ad-free browsing experience, this website is completely funded by donations. If you use the information in this article to solve a problem, entertain yourself, or otherwise improve your life, please consider a small donation to help pay for server costs. Many thanks, and enjoy the site!

Graphics Code

Real-time Blacklight Effect (in VB6)

This is one of my all-time favorite image effects – a simulated blacklight that looks GREAT on any image with people in it.  I invented this algorithm myself, and the provided code will allow you to test the effect in real-time on any image below 2MB.

Squall (from Final Fantasy VIII) before and after his blacklight transformation

Squall (from Final Fantasy VIII) before and after his blacklight transformation

The formula is fairly simple – assuming you have the R, G, and B values of a particular pixel, the blacklight transformation can be applied as follows:

1) Calculate a luminance value for the pixel

There is no set way to do this; personally, I prefer a proper human-eye accurate conversion using the formula:

L = (222 * R + 707 * G + 71 * B) \ 1000

This places proper emphasis on red, green, and blue according to standard cone density in the human eye.  However, a faster (and look-up table friendly) method could be:

L = (R + G + B) \ 3

For die-hard color perfectionists, luminance could also be calculated by an RGB -> HSL color space conversion, but the scope of that transformation is beyond this article.

2) Perform the blacklight function

My blacklight formula uses a “weight” to determine how bright to make the blacklight.  This is a number between 1 and 7, with an optimal value of 2.

Assuming that this weight value is placed in a variable called “fxWeight”, the blacklight function is performed by:

R = Abs(R - L) * fxWeight
G = Abs(G - L) * fxWeight
B = Abs(B - L) * fxWeight

…where Abs() is an absolute value function.  Basically, the blacklight formula calculates the distance between each color value and the gray value, then multiplies that by the “weight” of the blacklight.

3) Force all R, G, B values below 256

As you can see from the code above, if the blacklight weight is high, it is possible for the transformation to set pixels beyond proper unsigned byte range.  So to be safe, double-check that all R, G, B values are below 256.

(Note: for those who have read Part 4 of my Graphics Programming tutorials, note that this function is perfectly suited to being used in a stream.)

The download link provides code (in VB6) and an .exe for those interested in just playing with the function itself.

Download Code (255 kb)

 

BSD License
All source code in the above zip file has been released under a BSD License.

 

Related articles:

Discussion (Oldest Comments Displayed First)

Be the first to comment on “Real-time Blacklight Effect (in VB6)”

Post a comment

Feel free to use any of these tags in your comment: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>