4

If I have this:

.box{
  color: #FF3010;
  background: hsl(0,90%,40%);
}

and then this:

var box = document.querySelector(".box");
var result = document.querySelector(".result");
result.innerHTML = "BG color: "+window.getComputedStyle(box).backgroundColor;
result.innerHTML += "<br>";
result.innerHTML += "color:"+window.getComputedStyle(box).color;

The problem is that it always prints the values in rgb. So, I have 2 questions:

  1. Is it posible to get the value as written in the css?
  2. Is it posible to force the color to be hsl instead of rgb?
Vandervals
  • 5,774
  • 6
  • 48
  • 94

1 Answers1

0

you have two method get the live style of a dom element(not css), but you can not get hsl, but only rgb/rgba string.

var style = window.getComputedStyle(element).getPropertyValue('background-color')

var style = cell.computedStyleMap().get('background-color').toString();

ask your question:

  1. you can get it from two way:

    a. use CSSAPI. you can see doc on mdn.

    b. use mutationobserver, you can from oldvalue get it, if the value be changed by something.

  2. you can not get hsl directly, but you can get rgb by two way as above. and than change them to hsl, and if your hsl is match the html format, you can use hsl directly. the html format hsl not [0-1], but h is [0-360], and s & l with the %.

btw i just try to use hsl in web, but it is not match reality. yellow is more lighter than purple, and green is more lighter than blue. rgb(0, 255, 0) is more more lighter than (123, 0, 123), although the last is lighter than the above in hsl.

Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
defend orca
  • 617
  • 7
  • 17