2

In my CSS file I have this code:

.class { color: #fff; }

Now I am trying to read the CSS using:

document.stylesheets[0].cssRules.cssText

which returns:

.class { color: rgb(255,255,255); }

instead of the original CSS.

Is there any way to get original, hexadecimal value using document.stylesheets[0].cssRules.cssText without conversion?

Drenmi
  • 8,492
  • 4
  • 42
  • 51
  • 6
    Possible duplicate of [RGB to Hex and Hex to RGB](http://stackoverflow.com/questions/5623838/rgb-to-hex-and-hex-to-rgb) – ketan Jan 01 '16 at 06:22
  • 4
    @ketan: Have you read question correctly? I am not trying to convert the values. Actual values written in css are in hexadecimal. But when I am trying read stylesheets using document.stylessheets[0].cssRules.cssText it is showing rgb values instead of hexadecimal. So somehow values are getting converted automatically which I dont want. I need original hexadecimal value. – sharanpreet Jan 01 '16 at 08:27
  • That's the way it works. `cssText` contains a canonical rgb representation of the color no matter how it was specified. –  Jan 01 '16 at 11:59
  • @torazaburo: we know how it works. But is there any way get original hexadecimal value? – Samrat Jan 03 '16 at 16:59
  • Not as far I know, without parsing the CSS file yourself. Why do you want to do this? –  Jan 03 '16 at 17:19

1 Answers1

0

Is there any way to get original, hexadecimal value?

No, not from the browser, which represents the color in normalized RGB form. Yes, if you are willing to use one of several competent CSS parsers out there, which is easier than it sounds. I suggest using rework. It is designed for use in node, but you could also run it from the browser using something like browserify. You throw CSS at it, and it returns an internal representation of its structure called AST, which you can then walk to find things you are interested in.