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.)

Similar Posts:


DISCLAIMER: These download files are regularly scanned to ensure they remain free from malicious content. Unfortunately, some virus scanners will flag these .zip files as suspicious simply because they contain source code and/or executable files. I have submitted my projects to a number of companies in an attempt to rectify these false-positives. Some have been cooperative. Others have not. If your virus scanner alerts you regarding these files, please allow the file to be submitted for further analysis (if your program allows for that). This should help ensure that any false-positive warnings gradually disappear for all users.

This site - and its many free downloads - are 100% funded by donations. Please consider a small contribution to fund server costs and to help me support my family. Even $1.00 helps. Thank you!