ASP 101 - Active Server Pages 101 - Web05
The Place ASP Developers Go!

Please visit our partners

Windows Technology Windows Technology
15 Seconds
ASP 101
ASP Wire
VB Forums
VB Wire
internet.commerce internet.commerce
Partners & Affiliates

ASP 101 is an site
ASP 101 is an site
Internet News
Small Business
Personal Technology

Corporate Info
Tech Jobs
E-mail Offers

ASP 101 News Flash ASP 101 News Flash

 Top ASP 101 Stories Top ASP 101 Stories
An Overview of ASP.NET
Connections, Commands, And Procedures
What is ASP?

Use RecordSet.Filter
Show All Tips >>
ASP 101 RSS Feed ASP 101 Updates

Quick Tips

Convert Colors Between RGB and Hex

In HTML colors are specified by indicating the three color components that make up the color desired. These are Red, Green, and Blue and are specified using their hexadecimal values from 0 (00) to 255 (FF). Color attributes follow this general form:


where RR is the red component, GG is the green component, and BB is the blue component of the specified color. VBScript comes with the built in Hex function to convert decimal values to this hexadecimal form. For example:

<%= Hex(255) %>

will return FF which you could then use as any of the color components of an HTML color attribute. But what if you need to convert from the hexadecimal values to the standard decimal ones used by so many paint and drawing programs? There's really no function to do it built into VBScript.

The reason there's no function for it is that you really don't need one. Hexadecimal numbers are represented as numbers prefixed by "&H" and if you ask VBScript to give you their value, it'll automatically convert them to decimal form. For example:

<%= &HFF %>

will return 255 without you doing anything at all. The real problem arises when you read that value in from somewhere. Most values that contain alphanumeric characters will come into VBScript as variants of sub-type string. Luckily it's solved easily enough. Simply pre-pend the "&H" I mentioned earlier and then convert to a numeric type and VBScript will figure everything out for you. While:

<%= "&H" & "FF" %>

returns &HFF

<%= CInt("&H" & "FF") %>

will return the 255 we're looking for.

Previous      Show All Tips      Next

If you have a tip you would like to submit, please send it to:

Home |  News |  Samples |  Articles |  Lessons |  Resources |  Forum |  Links |  Search |  Feedback
The Network for Technology Professionals



Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers