dolanmiu / ng-color

Simple Angular color picker components which follow Angular best practices. No Frills.
https://stackblitz.com/edit/ng-color-demo
12 stars 6 forks source link

how to set dynamic picker color from ts file #18

Open melalitesh opened 5 years ago

melalitesh commented 5 years ago

Hi,

I want to set the color of picker from .ts file, but changing the hexstring doesn't change the picker color.

Please suggest

dolanmiu commented 5 years ago

Show me your code

melalitesh commented 5 years ago
getColorFromRGB(){
        let hex = this.rgbToHex(this.coolColor.rgb.red, this.coolColor.rgb.green, this.coolColor.rgb.blue);
        this.coolColor.hexString = '#'+hex;
        this.coolColor.rgb.red = Number(this.coolColor.rgb.red)
        this.coolColor.rgb.green = Number(this.coolColor.rgb.green)
        this.coolColor.rgb.blue = Number(this.coolColor.rgb.blue)
        let hsl = this.rgbToHsl(this.coolColor.rgb.red, this.coolColor.rgb.green, this.coolColor.rgb.blue);
        this.coolColor.hsl.hue = hsl[0];
        this.coolColor.hsl.saturation = hsl[1];
        this.coolColor.hsl.lightness = hsl[2];
    }
<ng-color-box (ngModelChange)="colorChange($event)" [(ngModel)]="coolColor" startHex="coolColor.hexString"></ng-color-box>
dolanmiu commented 5 years ago

Try setting everything in one go, like this:

getColorFromRGB(){
        let hex = this.rgbToHex(this.coolColor.rgb.red, this.coolColor.rgb.green, this.coolColor.rgb.blue);

        this.coolColor = {
                    hexString: ...,
                    rgb: {
                       red: ...
                    }
               }

this look a little bit wrong to me:

this.coolColor.rgb.red = Number(this.coolColor.rgb.red)

It is using itself to set itself? Inception