As part of my ColdFusion calendar system, I got into fooling around with some colors. I didn't end up going in that particular direction, but it got me messing around with both the ColdFusion functions, InputBaseN() and FormatBaseN(), neither of which I had ever used before. They do very simple but very powerful calculations for you.
InputBaseN( strValue, intRadix ) - This takes a numeric value and the radix that defines the numeric value and converts it to our standard base 10 format.
FormatBaseN( strValue, intRadix ) - This takes a base 10 value (what we use all the time) and converts it to a number with the given radix.
Using the ColdFusion InputBaseN() function, we can easily do things like convert HEX (hexadecimal) color values to RGB (red-green-blue) decimal values:
Launch code in new window » Download code as text file »
Running the above code, we get the following output:
Red : FF : 255
Green : CC : 204
Blue : 00 : 0
We can also easily convert bit/binary values to decimal:
Launch code in new window » Download code as text file »
Running the above code, we get the following output:
0 : 0
1 : 1
10 : 2
11 : 3
100 : 4
110 : 6
111 : 7
1000 : 8
1100 : 12
1110 : 14
1111 : 15
Pretty easy stuff! And, as easy as that is, ColdFusion's FormatBaseN() function makes it just as easy to go the other way, such as converting RGB values to HEX values. The only thing that complicates this conversion is that we need a 6-digit HEX value (at least if you are cool, you do), and there is nothing about FormatBaseN() that enforces this. So, in addition to actually changing the base radix, we also need to check the value lengths:
Launch code in new window » Download code as text file »
Running the above code, we get the following output:
FF0E80
So anyway, I didn't end up using these yet, but it's great to know that this functionality exists. If it didn't, I would probably have to end up doing manual multiplication and division and stuff - certainly not something we should be wasting our times on.
Download Code Snippet ZIP File
Comments (8) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
Selecting Contrasting Text Color Based On Background Color
Kinky ColdFusion Calendar System Has Delete, Day View, And Color Coding
Thank you for the code, I will see what it can bring to me
Posted by AndreH on Aug 9, 2007 at 7:44 AM
Very nice! This will help me when converting colors in some server-side font and image manipulation tools I use that require decimal color values. I know a lot of hex color values by heart, but not many equivalent decimal values.
Posted by Adam Fairbanks (Tidy Internet) on Aug 13, 2007 at 1:05 PM
Glad to help in some way :)
Posted by Ben Nadel on Aug 13, 2007 at 1:23 PM
Thank you for sharing.
Posted by P on Nov 16, 2007 at 11:05 AM
I am using InputBaseN to convert IP addresses to decimal (dotted-decimal to binary to decimal); it works for lower-numbered addresses (such as 10.0.0.0), but returns a negative number for higher-numbered addresses (such as 192.0.0.0). It seems its treating the variable like a signed int? Any ideas?
Posted by Tom Sinning on Aug 28, 2008 at 3:50 PM
@Tom,
How are you going from dotted-decimal to binary. I am not even sure what that would do?
Posted by Ben Nadel on Aug 28, 2008 at 3:57 PM
It's for an application that maps a specific user login to one or a range of network addresses. This permits assignment of a "default account" to workstations per the logical/physical network on which they reside (e.g. different campus locations). I convert to decimal to make the comparison to configured network address ranges mathematically simple. All dotted-decimal IP addresses are fundamentally decimal numbers; dotted decimal is a rather arbitrary notation, but it makes network classification, routing, subnet and supernet masking possible.
The process is convert each portion of the dotted-decimal IP address (from CGI variable) to binary, pad the binary to 8 chars where needed (the second, third and forth values need to be 8 chars), concatenate the result (creating the binary equivalent of the dotted-decimal) then convert to decimal.
When the first decimal is small e.g. 10.x.x.x (usually used for internal private networks), InputBaseN works fine - when the first decimal number is large e.g. 192.x.x.x (also used for internal private networks) or 169.x.x.x (Microsoft private assigned in absence of DHCP) the decimal value returned is negative.
Posted by Tom Sinning on Aug 28, 2008 at 4:28 PM
@Tom,
Ok, I see what you are trying to do. I would check with Michael Dinowitz over on www.blogoffusion.com. He often converts IP addresses to integers for database storage (I believe). I think he will be able to give you better advice than I can.
Posted by Ben Nadel on Aug 28, 2008 at 4:38 PM