473,326 Members | 2,134 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,326 developers and data experts.

Function to convert decimal color number into HTML hex color string

Akatz712
The following function converts a decimal number representing a color stored in the way that Microsoft windows stores colors (low byte is red), and converts it to a hex string which is needed for web applications, namely "#RRGGBB".

Expand|Select|Wrap|Line Numbers
  1. function decimalColorToHTMLcolor(number) {
  2.     //converts to a integer
  3.     var intnumber = number - 0;
  4.  
  5.     // isolate the colors - really not necessary
  6.     var red, green, blue;
  7.  
  8.     // needed since toString does not zero fill on left
  9.     var template = "#000000";
  10.  
  11.     // in the MS Windows world RGB colors
  12.     // are 0xBBGGRR because of the way Intel chips store bytes
  13.     red = (intnumber&0x0000ff) << 16;
  14.     green = intnumber&0x00ff00;
  15.     blue = (intnumber&0xff0000) >>> 16;
  16.  
  17.     // mask out each color and reverse the order
  18.     intnumber = red|green|blue;
  19.  
  20.     // toString converts a number to a hexstring
  21.     var HTMLcolor = intnumber.toString(16);
  22.  
  23.     //template adds # for standard HTML #RRGGBB
  24.     HTMLcolor = template.substring(0,7 - HTMLcolor.length) + HTMLcolor;
  25.  
  26.     return HTMLcolor;
  27.  
Apr 22 '07 #1
0 21272

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: oddstray | last post by:
Hi, I have a number which is larger than the max unsigned long int. I don't have 64-bit integers available to me. I need to get the resulting 40-bit hex string. I can't find any algorithm...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ 3.5. - How do I convert a Number into a String with exactly 2 decimal places?...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
10
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
4
by: Pubs | last post by:
Hi, I have a variable, a string that contains a hex number (max is FF). I want to convert it to a byte value. Could some one let me know how is it to be done. I have seen conversion of direct...
1
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
2
by: star01daisy | last post by:
This is what the assignment says to do: Write a C++ program to do decimal-binary number conversions. The program gives the user a choice of conversion type (binary to decimal or decimal to binary)....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.