Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8098451
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:04:59+00:00 2026-06-05T22:04:59+00:00

Given a string of a valid CSS color value: #fff #ffffff white rgb(255, 255,

  • 0

Given a string of a valid CSS color value:

  • #fff
  • #ffffff
  • white
  • rgb(255, 255, 255)

I need to get an array of numbers of the following format: [R, G, B]

What is the most efficient way of doing this in JavaScript (assuming a major browser)?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:05:01+00:00Added an answer on June 5, 2026 at 10:05 pm
    function parseColor(input) {
        var m;
    

    Obviously, the numeric values will be easier to parse than names. So we do those first.

        m = input.match(/^#([0-9a-f]{3})$/i)[1];
        if( m) {
            // in three-character format, each value is multiplied by 0x11 to give an
            // even scale from 0x00 to 0xff
            return [
                parseInt(m.charAt(0),16)*0x11,
                parseInt(m.charAt(1),16)*0x11,
                parseInt(m.charAt(2),16)*0x11
            ];
        }
    

    That’s one. Now for the full six-digit format:

        m = input.match(/^#([0-9a-f]{6})$/i)[1];
        if( m) {
            return [
                parseInt(m.substr(0,2),16),
                parseInt(m.substr(2,2),16),
                parseInt(m.substr(4,2),16)
            ];
        }
    

    And now for rgb() format:

        m = input.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
        if( m) {
            return [m[1],m[2],m[3]];
        }
    

    Optionally, you can also add support for rgba format, and even hsl/hsla if you add an HSL2RGB conversion function.

    Finally, the named colours.

        return ({
            "red":[255,0,0],
            "yellow":[255,255,0],
            // ... and so on. Yes, you have to define ALL the colour codes.
        })[input];
    

    And close the function:

    }
    

    Actually, I don’t know why I bothered writing all that. I just noticed you specified “assuming a major browser”, I’m assuming that also means “up-to-date”? If so…

    function parseColor(input) {
        var div = document.createElement('div'), m;
        div.style.color = input;
        m = getComputedStyle(div).color.match(/^rgb\s*\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)$/i);
        if( m) return [m[1],m[2],m[3]];
        else throw new Error("Colour "+input+" could not be parsed.");
    }
    

    An up-to-date browser will convert any given colour to rgb() format in its computed style. Just get it back, and read it out.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to know if a given string is a valid DateTime format string
I need validate the given input String is a valid Timestamp in milliseconds. For
I'm in need to modify a given string to contain only alpha numerical characters,
How do I create an array in smarty from a given string like 22||33||50
Is it possible to detect if a given string is valid regular expression, using
How can I get all the unclosed tags in a given string, prefferably in
I'm working on a method that does something given a string parameter. A valid
Given a string and an array of chars: string userDir = WindowsIdentity.GetCurrent().Name; char[] chars
Given this interface public interface IMyInterface { string Method1(); } Why is this valid
How can I verify a given xpath string is valid in C#/.NET? I'm not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.