0

Is it possible to add custom (HEX) color to material component in Angular 4? For example something like this:

<div *ngFor="let factor of factors">
   <button md-button color="factor.color">Button</button>
</div>

Where factor.color is string in hex format (for example '#CCC')

Edric
  • 24,639
  • 13
  • 81
  • 91
Maros2710
  • 51
  • 2
  • 7

1 Answers1

0

You can use [style.color] attribute and some helpful posts about converting hexa code to RGB:

DEMO

HTML:

<button mat-button [style.color]="hexToRgb(factor.color)">Click me</button>

Typescript:

....

hexToRgb(hex) {
  hex = hex.replace("#",'');
  let arrBuff = new ArrayBuffer(4);
  let vw = new DataView(arrBuff);
  vw.setUint32(0,parseInt(hex, 16),false);
  let arrByte = new Uint8Array(arrBuff);

  return 'rgb(' + arrByte[1] + "," + arrByte[2] + "," + arrByte[3] +')';
}
Vega
  • 27,856
  • 27
  • 95
  • 103