Jump to content

JavaScript: Emby's Computed styles for plugins development.


chef

Recommended Posts

This is most likely old hat to advanced programmers, but perhaps this trick might be useful to someone at some point....

 

Recently I took another look at the Disk Space plugin. 

 

When I had originally written it, my JavaScript programming skill were somewhat new, and after a couple years of practice, I thought  there might be some advancements that could be added to the plugin.

 

One of the aspects of the plugin I didn't really like was how the colors added to the drive cards (by default) were black and white.

Ewww....

disk_space_computer_style_0.png.941e38a39661eecd673daabb2d3e1dc2.png

 

Mind you the plugin always had the ability to change the colors of the charts,  But the default black and white color scheme is gross! 🤮

 

I thought that the first change that should happen is to have the default color scheme match the applied  theme colors set in dashboard.

disk_space_computer_style_3.png.ffba560180fd87594c23313d977a5acd.png

 

Enter javascripts 'getComputedStyles()'.

 

The 'getComputedStyles()' function allows you to access the css in the DOM in javavscript.

For example, getting the 'primary color' for the currently applied dashboard theme, can be easily obtained like this.

getComputedStyle(document.documentElement).getPropertyValue('--theme-primary-color')

It is requesting the custom css variable for emby's applied theme's primary color.

 

For more information on  custom css variables, I would recommended watching Kevin Powell's explanation on them found here.

A css master for sure. 😃

 

 

Things got a little more tricky with the DiskSpace plugin code.

 

Adding the custom variable for the theme primary color wasn't as simple as requesting the computed style.

The code used in Diskspace's color values is expecting  hexadecimal color format, and emby is using HSL.

                                                                                                                                                                                                                                                                         

Emby's applied theme css custom variables:

    --theme-primary-color-hue: 116;
    --theme-primary-color-saturation: 41.7%;
    --theme-primary-color-lightness: 50.2%;
    --theme-primary-color: hsl(var(--theme-primary-color-hue), var(--theme-primary-color-saturation), var(--theme-primary-color-lightness));

The theme layouts are looking at colors in a color wheel, like this:

hsl-color-wheel.png.97601607a8c42f3288b305f393712403.png

In essence, the numbers are just telling us how far to traverse the circle's circumference  to find the color, and how far towards the center the color's origin is located.

                                                                                                                                                                                                                                                                       

 

In order to use these values with DiskSpace, the HSL would have to be converted into the Hexadecimal counterpart.

 

Luckily someone had done this before, and posted an example on StackOverflow! 😆

https://stackoverflow.com/questions/36721830/convert-hsl-to-rgb-and-hex

This code it's great!

This code return the hexadecimal value (very close...in most cases exactly... but... not always exact because it is being rounded) to the HSL color.

//From StackOverflow
function hslToHex(h, s, l) {
  l /= 100;
  const a = s * Math.min(l, 1 - l) / 100;
  const f = n => {
    const k = (n + h / 30) % 12;
    const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
    return Math.round(255 * color).toString(16).padStart(2, '0');   // convert to Hex and prefix "0" if needed
  };
  return `#${f(0)}${f(8)}${f(4)}`;
}

 

There is one extra step to take here in order to make the conversion usable.

Using the getComputedStyles() function will return the value of "--primary-theme-color" as a string.

"hsl(116,41.7%,50.2%)"

 

The function found on StackOverflow, wants individual values of the color code split up as: hue, saturation, lightness .

 

Doing some edits using string.replace, splits, and parseInt's work! 👍

         //Edited code to handle the string value of HSL
         function hslToHex(hsl) {
            hsl.replace('%', '');
            let hslValues = hsl.split(',')
            let h = parseInt(hslValues[0].split('(')[1]);
            let s = parseInt(hslValues[1]);
            let l = parseInt(hslValues[2].split(')')[0]);
            l /= 100;
            const a = s * Math.min(l, 1 - l) / 100;
            const f = n => {
                const k = (n + h / 30) % 12;
                const color = l - a * Math.max(Math.min(k - 3, 9 - k, 1), -1);
                return Math.round(255 * color).toString(16).padStart(2, '0');   // convert to Hex and prefix "0" if needed
            };
            return `#${f(0)}${f(8)}${f(4)}`;
        }

 

Now calling the function like so:

hslToHex(getComputedStyle(document.documentElement).getPropertyValue('--theme-primary-color'));

result:

#52B54B

 

Cool,  applying the primary color  as a default when the plugin loads works now. 

disk_space_computeredStyles.png.91a678d6ebd17abafec95813430dd2ac.png

 

The entire DiskSpace plugin code can be found here: https://github.com/chefbennyj1/Emby.DiskSpace

Maybe this is useful info.  :) 

 

Edited by chef
Link to comment
Share on other sites

Yea that works but it's slow so you wouldn't use it in performance critical code that runs frequently. But for something that only runs once when the page loads it's fine 

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...