Click here to Skip to main content
Click here to Skip to main content

Determining The Ideal Text Color Based on Specified Background Color (For VBScript)

, 7 May 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
The VBScript version of my C# article about the very same thing

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:

'-------------------------------------------------------------------------
' Extracts the color value from the string that represents the background 
' color. It's assumed that the color will ALWAYS be in the standard hex 
' notation of #RRGGBB.  jms - 07May2007 
'-------------------------------------------------------------------------
function GetColorValue(sColor, nShade)
    Dim nVal: nVal = 0
    select case nShade
        case 0 nVal = CLng("&H" & (Mid(sColor, 2, 2)))  ' red
        case 1 nVal = CLng("&H" & (Mid(sColor, 4, 2)))  ' green
        case 2 nVal = CLng("&H" & (Mid(sColor, 6, 2)))  ' blue
        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.

'-------------------------------------------------------------------------------
' Determines the best choice of text color based on the background color. The   
' two possible resulting values are black text or white text.  jms - 07MAY2007  
'-------------------------------------------------------------------------------
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...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

John Simmons / outlaw programmer
Software Developer (Senior)
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.
 
My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141223.1 | Last Updated 8 May 2007
Article Copyright 2007 by John Simmons / outlaw programmer
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid