Category: Graphics Code  ·  Originally posted January 20, 2009  ·  Last updated February 1, 2011

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

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

 

bookmark and share this article

Email this article to a friend Print a copy of this article Subscribe to article comments Bookmark this article on Delicious Submit this article to reddit Submit this article to Digg Submit this article to StumbleUpon Share this article on Facebook Tweet this article

related articles

Discussion (Be the first to comment!)

Public comments are closed for this article. Private comments may be submitted using this link.

Comments are closed.