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 9237491
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:32:48+00:00 2026-06-18T07:32:48+00:00

Real problematic Write a function that returns the color value from any CSS color

  • 0

Real problematic

Write a function that returns the color value from any CSS color OR from a color defined by a class

Initial formulation

I would like to know if a string passed in parameter to my function is a color – answering only that would already be great – and then to know its hex value.

So I have defined a regex to find out if the string is something like #fff or also rgb(0,0,0), but it doesn’t catch CSS standard colors such as black and white. Should I test each color name or is there any way or preexisting function to do that ? Thanks.

Solution personally used

  • If you want dot-preceded class names such as “.myClass”
function getColor(classOrColor) {
    if(classOrColor[0] === '.') {
      var temp = $('<div id="temp" style="display:none;" class="'+ classOrColor.split('.').join(' ') + '"/>').appendTo('body');
      var color = temp.css('color');
      temp.remove();
      return color;
    } else {
      return classOrColor;
    }
  }

Usage examples:

getColor('yellow')
getColor('#abc')
getColor('rgb(1,2,3)')
getColor('.myClass .myOtherClass')

Solution based on mplungjan’s answers

  • If you want plain class names such as “myClass”

Search if a class with this name exists using this answer: https://stackoverflow.com/questions/983586/&#8230;

Then call the same function (with minor modification)

function getColor(classOrColor) {
    if(classExists(classOrColor)) {
      var temp = $('<div id="temp" style="display:none;" class="'+ classOrColor + '"/>').appendTo('body');
      var color = temp.css('color');
      temp.remove();
      return color;
    } else {
      return classOrColor;
    }
  }

Usage examples:

getColor('#abc')
getColor('myClass')
  • 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-18T07:32:49+00:00Added an answer on June 18, 2026 at 7:32 am

    You could use getComputedStyle – this does not work on IE8 and under.

    Please see the answer to Javascript function to convert color names to hex codes for a better coverage than mine (saw it after I wrote it)

    DEMO

    function getRGB(str){
        var elem = document.createElement("div");
        elem.style.display="none";
        elem.style.color=str;
        document.body.appendChild(elem);
        return  window.getComputedStyle(elem,null).getPropertyValue("color");
      }
    alert(getRGB("red"));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a program that is parsting tweets in real time from the twitter
I'm trying to create a pure function that returns the multiplication of two other
Any real world simple samples of using abstract class? I'm trying to get in
I need to print real datbase type name from DbParameter.DbType , but when I
Real-life case (helps understand the question) I am building a device that can freely
Real quick background : We have a PDFMaker (HTMLDoc) that converts html into a
Real simple question here - how do I add the .hoverIntent plugin from Brian
Real World Haskell has this example: class BasicEq3 a where isEqual3 :: a ->
What real programming languages are easy to write interpreters for? Real languages for me,
In real world cube root for a negative number should exist: cuberoot(-1)=-1 , that

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.