scttcper / ngx-color

🎨 Color Pickers from Sketch, Photoshop, Chrome, Github, Twitter & more
https://ngx-color.vercel.app
MIT License
435 stars 55 forks source link

Get hex + alpha #292

Open MichaelPruefer opened 4 years ago

MichaelPruefer commented 4 years ago

Is it possible to retrieve the alpha value when using color.hex on ColorEvent?

it would be better than doing

let hexValue = colorEvent.color.hex || '';
  if (hexValue.length === 7) {
    hexValue += Math.round(colorEvent.color.rgb.a * 255).toString(16);
}
nzbin commented 2 years ago

I am also confused with this issue. It's really inconvenient without hex8 format. I have solved it with @ctrl/tinycolor which used by ngx-color.

new TinyColor(e.color.rgb).toHex8String()

You can check the colorpicker example.

ecancil commented 2 months ago

This still persists, with how amazing this library is, this is a weird 'feature'

ecancil commented 2 months ago

For anyone else who ran into this, this is the method that actually worked for me

  public getRGBWithAlphaFromColorChangeEvent(event: ColorEvent) {
    let hexValue = event.color.hex || '';

    if (hexValue.length === 7) {
      const alphaHex = Math.round(event.color.rgb.a * 255).toString(16).padStart(2, '0');
      hexValue += alphaHex;
    }

    return hexValue;
  }