Search code examples
javascriptdartwebcolorshighlight

How to get the web highlight color programatically?


I want to get the highlight color of a div, but programatically. Here's what I tryied:

    final e = html.document.getElementById("body");
    e?.style?.backgroundColor = "highlight";
    print(e?.style?.backgroundColor);

It returns highlight, but I'd like to have its hex or rgb code.


Solution

  • Use window.getComputedStyle() to return the RGB value which is what browsers return regardless of format that developer used to set color.

    Then you can parse the RGB to hex if needed

    const bod = document.body
    bod.style.backgroundColor = 'highlight'
    
    console.log(getComputedStyle(bod).backgroundColor)