Hey everyone.
I am trying to get values for colors that correspond to --ion-color-*
CSS variables at run-time, from code.
I only need base color value, i.e. the one defined by --ion-color-base
.
CSS
.ion-color-foo{
--ion-color-base: #7044ff; // << need this value when I ask for 'foo'
--ion-color-base-rgb: 112,68,255;
--ion-color-contrast: #eeeeee;
--ion-color-contrast-rgb: 238,238,238;
--ion-color-shade: #633ce0;
--ion-color-tint: #7e57ff;
}
HTML
<ion-button color="foo">Text</ion-button>
Code
// for now this is imaginary function I am trying to figure out
let fooColor = getValueFromPredefinedColor('foo'); // returns hex string of foo's base color, "#7044ff" in this example
Since there is a neat way in Ionic 4 to add more custom colors to predefined ones (see here), I would like to define my own palette of colors using .ion-color-name
classes like described in above link, and then use those colors throughout my app, and it works great. I can even add more colors at runtime by using this neat little method I wrote.
Now, for the problem: I can’t seem to get color values from named colors. I need these values so that I can pass them to third-party libraries (i.e. chart.js) to have consistent colors in the chart and in other UI elements.
Plan B
If that can’t be done, my second option was to pick up the background color from ion-button
or other component styled by color
directive, by using
window.getComputedStyle(document.getElementById('myButton')).getPropertyValue('background-color');
but it works only in Ionic 3 (Stackblitz example). In Ionic 4, this method always returns rgba(0,0,0,0)
as the result.
Any help appreciated.