Introduction
Here's a little snippet for everyone that develops web pages, but that are forced to endure the hell that is VBScript. This article is actually the red-headed step cousin of an earlier C# article I wrote. Why create a new article? To allow it to show up under a VBScript category of course.
Before I go any further, let me admit that I disdain anything related to VB because I've been coding in C++ for 17 years, and C++ programmers are almost required to dislike anything related to VB. By this time you're probably asking yourself why I even submitted the article. Well, it's because I've been assigned the task of making changes to a web site that utilizes VBScript, and I haven't been given the authorization change it to something more modern. In short, I'm coding in VBScript, but I don't like it.
If you need the C# version, go here.
The Snippet
While we had my original C# code to use as a guide, my desire to effect an elegant solution using VBscript was dampened by the sad realization that VBScript has little-to-no programmability. (OF course you know I'm just exercising my acerbic nature and directing what some would call obligatory disdain for anything that starts with the word "VB".)
Anyway, this method should be considered a mere jumping off point because I didn't spend two weeks trying to engineer foolproof validation. In other words, as the code is presented, you should ensure that you call the code with the correctly formatted parameters.
Using the code
Because VBScript is provides the programmability of a two-slice toaster, you don't have the benefit of having neat-o string or numeric conversion functions that make life so wonderful under real languages. I guess it's because VBscript is a largely typeless language where values can morph on their own into whatever type is needed at the time. I find this trait particularly disturbing - I like order and definition in my life (remember, I'm a C++ programmer).
Our solution here is comprised of two functions:
function GetColorValue(sColor, nShade)
Dim nVal: nVal = 0
select case nShade
case 0 nVal = CLng("&H" & (Mid(sColor, 2, 2)))
case 1 nVal = CLng("&H" & (Mid(sColor, 4, 2)))
case 2 nVal = CLng("&H" & (Mid(sColor, 6, 2)))
case else nVal = 255
end select
GetColorValue = nVal
end function
The GetColorValue
function extracts the specified color component from the background color string, and returns its integer value to the calling function. The reason we do this is because we have chosen to store the background colors in a database as a string. Your mileage will vary depending on how you pass your color values.
function BestFontColor(sBkColor)
if InStr(1,sBkColor, "#") <> 1 OR Len(sBkColor) < 7 then
BestFontColor = "#000000"
exit function
end if
Dim nRed: nRed = GetColorValue(sBkColor, 0)
Dim nGreen: nGreen = GetColorValue(sBkColor, 1)
Dim nBlue: nBlue = GetColorValue(sBkColor, 2)
Dim nThreshold: nThreshold = 105
Dim bgDelta: bgDelta = int( (nRed * 0.299) +
(nGreen * 0.587) +
(nBlue * 0.114))
if 255 - bgDelta < nThreshold then
BestFontColor = "#000000"
else
BestFontColor = "#FFFFFF"
end if
end function
The
BestFontColor
function is the code that you actually call from your own code. Simply passing in the background color will return either black or white. If you saw my C# article about this topic, you may recognize the forula I use to determine the best font color.
Disclaimer
This article is short because there's really nothing of any complexity associated with the method described herein. There's also no downloadable code because - well - this is all the code. Hopefully, it will save you a little time...