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.

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