Easily Convert Between HTML and RGB Colors using JavaScript

24. October 2009

To make things easier for converting between HTML Colors and RGB Colors using JavaScript I wrote the below “ColorConverter” object. This object has 2 methods that easily allow you to convert between HTML Colors (ex: #FF33C2) and RGB Colors (ex: 255, 0, 233). There isn’t anything built into JavaScript for doing this, and it can come in very handing when working with the Bing Maps VEColor object.

Usage Examples:

var rgb = ColorConverter.toRGB("#FF000A"); // returns {r:255, g:0, b:10}

var htmlColor = ColorConverter.toHTML(255,0,14); // returns "FF0021"

// Also supports 3 character HTML color values like the Web Browsers and CSS do
rgb = ColorConverter.toRGB("#DDD"); // returns {r:255, g:255, b:255}

Full Code for the “ColorConverter”:

(function(){
    window.ColorConverter = {
        toHTML: function(r, g, b){
            return $ensureHexLength(r.toString(16)) + $ensureHexLength(g.toString(16)) + $ensureHexLength(b.toString(16));
        },
        toRGB: function(color){
            var r, g, b;
            var html = color;
            
            // Parse out the RGB values from the HTML Code
            if (html.substring(0, 1) == "#")
            {
                html = html.substring(1);
            }
            
            if (html.length == 3)
            {
                r = html.substring(0, 1);
                r = r + r;
                
                g = html.substring(1, 2);
                g = g + g;
                
                b = html.substring(2, 3);
                b = b + b;
            }
            else if (html.length == 6)
            {
                r = html.substring(0, 2);
                g = html.substring(2, 4);
                b = html.substring(4, 6);
            }
        
            // Convert from Hex (Hexidecimal) to Decimal
            r = parseInt(r, 16);
            g = parseInt(g, 16);
            b = parseInt(b, 16);
        
            return {r: r, g: g, b: b};
        }
    };
    
    function $ensureHexLength(str){
        if (str.length == 1){
            str = "0" + str;
        }
        return str;
    }
})();

JavaScript, Bing Maps ,



Comments

waggi
waggi
11/20/2009 11:33:36 AM #
Great post.
11/20/2009 11:36:59 AM #
Wow this will be great instead I use code for color like this #009898 I can now convert in into JavaScript but the only problem it is very long codes but I guess this is effective.
12/8/2009 6:44:19 PM #
can you please provide a sample code for detecting open html tags and closing them programmatically.
thanks
12/9/2009 5:30:37 PM #
Just try to smile for about 2-3 mins then you can get back to work
12/20/2009 7:30:23 AM #
This article gives the light in which we can observe the reality. this is very nice one and gives indepth information. thanks for this nice article
12/24/2009 3:34:16 AM #
Nice to be visiting your blog again, it has been months for me. Well this article that i've been waited for so long. I need this article to complete my assignment in the college, and it has same topic with your article. Thanks, great share.
12/28/2009 5:18:36 PM #
I just hope to have understood this the way it was meant
12/30/2009 9:01:58 AM #
I love reading your posts. I wish you a Happy New Year!
1/9/2010 3:38:48 PM #
When I originally commented I clicked the "Notify me when new comments are added" checkbox and now each time a comment is added I get four emails with the same comment.
Is there any way you can remove me from that service?
Thanks!
1/12/2010 7:12:26 AM #
I have recently started using the blogengine.net and I having some problems here? in your blog you stated that we need to enable write permissions on the App_Data folder...unfortunately I don't understand how to enable it.
1/16/2010 4:41:09 PM #
Well, this is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!
1/18/2010 2:50:35 AM #
Happy to see your blog as it is just what I’ve looking for and excited to read all the posts.I am looking forward to another great articles from you..
1/18/2010 2:50:57 AM #
Well, this is my first visit to your blog! We are a group of volunteers and starting a new initiative in a community in the same niche. Your blog provided us valuable information to work on. You have done a marvellous job!
1/18/2010 6:32:13 PM #
Great detailed information, I ll be visiting you more frequently, here is very interesting information.
1/18/2010 8:30:11 PM #
Cheers to the author for giving me some solid ideas....
1/18/2010 8:33:19 PM #
Its a sad fact but i have to face it. .. many people in my family has died of cancer or are suffering from it. I am taking an extra step here to be away from this wretched disease by using colon cleanising as i have heard it is one of the preventive measures.
1/18/2010 10:32:58 PM #
That was a very very good article.
1/19/2010 2:13:13 PM #
Good Work.Keep tit up.
1/19/2010 2:47:47 PM #
Hi,

How do you deduct proffessional tax from your Gross salary in ITR 2 ? In which Schedule?
1/19/2010 3:23:22 PM #
Here given nice information about colorconverter function coding details.Convert Html to RGB colors coding details given here.
SEO
1/19/2010 3:40:48 PM #
How long have you been in this field? You seem to know a lot more than I do, I’d love to know your sources!
1/20/2010 11:18:58 AM #
I have surfed the net more than three hours today, yet I never found any interesting article like yours. It's worth enough for me. Thanks.
1/20/2010 4:40:06 PM #
It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks
Jhon
Jhon
1/21/2010 12:45:12 PM #
I was very pleased to find this site.I wanted to thank you for this great read!! I definitely enjoying every little bit of it and I have you bookmarked to check out new stuff you post.

Regards
www.prlog.org/...ancy-treating-pregnancy-acne.html
www.prlog.org/...roducts-devices-and-remedies.html
Mahi
Mahi
1/21/2010 1:54:52 PM #
The good thing about your information is that it is explicit enough for students to grasp. Thanks for your efforts in spreading academic knowledge.


Regards
www.prlog.org/...ancy-treating-pregnancy-acne.html
www.prlog.org/...roducts-devices-and-remedies.html

1/21/2010 2:09:00 PM #
Great blog post. It’s useful information.
1/21/2010 2:40:00 PM #
Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that.
1/21/2010 4:19:59 PM #
Starting to understand a bit more now... Thanks for keeping it simple!
1/22/2010 5:52:58 AM #
Hi,

Very useful info. Hope to see more posts soon!
1/22/2010 6:46:13 AM #
Hi,

Why didn’t I find this post earlier? Keep up the good work!
1/23/2010 2:27:17 AM #
great article
1/23/2010 3:28:40 AM #
It’s hard to find knowledgeable people on this topic, but you sound like you know what you’re talking about! Thanks
Comments are closed