File
Implements
Index
Properties
|
|
Methods
|
|
Inputs
|
|
Methods
extractInitial
|
extractInitial()
|
|
It will extract the first character of the user name and return with different BG color
|
getBgColor
|
getBgColor(pname)
|
|
|
getContrast
|
getContrast(hexcolor)
|
|
Parameters :
Name |
Optional |
hexcolor |
No
|
Returns : "#000000" | "#ffffff"
|
ngOnChanges
|
ngOnChanges(changes: any)
|
|
It will detect the changes of username and call the extractInitial() method
Parameters :
Name |
Type |
Optional |
changes |
any
|
No
|
|
stringToColor
|
stringToColor(str)
|
|
|
import { Component, Input, OnInit, OnChanges } from '@angular/core';
import { CommonUtilService } from '@app/services';
@Component({
selector: 'app-profile-avatar',
templateUrl: './profile-avatar.component.html',
styleUrls: ['./profile-avatar.component.scss'],
})
export class ProfileAvatarComponent implements OnInit, OnChanges {
@Input() username: string;
@Input() isStateUser: boolean;
bgColor: string;
color: string;
initial: string;
constructor(
private commonUtilService: CommonUtilService,
) { }
ngOnInit() {
this.extractInitial();
}
/**
* It will detect the changes of username and call the extractInitial() method
*/
ngOnChanges(changes: any) {
this.username = changes.username.currentValue;
this.extractInitial();
}
getBgColor(pname) {
this.bgColor = this.stringToColor(pname.toLowerCase());
this.color = this.getContrast(this.bgColor);
}
/*Get color Hex code by passing a string*/
stringToColor(str) {
let hash = 0;
for (let i = 0; i < str.length; i++) {
// tslint:disable-next-line:no-bitwise
hash = str.charCodeAt(i) + ((hash << 5) - hash);
}
let colour = '#';
for (let i = 0; i < 3; i++) {
// tslint:disable-next-line:no-bitwise
const value = (hash >> (i * 8)) & 0xFF;
colour += ('00' + value.toString(16)).substr(-2);
}
return colour;
}
/*Get text contrast by passing background hex color*/
getContrast(hexcolor) {
const r = parseInt(hexcolor.substr(1, 2), 16);
const g = parseInt(hexcolor.substr(3, 2), 16);
const b = parseInt(hexcolor.substr(5, 2), 16);
const color = ((r * 299) + (g * 587) + (b * 114)) / 1000;
return (color >= 128) ? '#000000' : '#ffffff';
}
/**
* It will extract the first character of the user name and return with different BG color
*/
extractInitial() {
this.initial = this.commonUtilService.extractInitial(this.username);
if (this.initial) {
this.getBgColor(this.username);
}
}
}
<!-- Generated template for the ProfileAvatarComponent component -->
<div class="profile-avatar" *ngIf="!isStateUser" >
<span [style.color]="bgColor">
{{initial | titlecase}}
</span>
</div>
<div class="profile-avatar profileAvatar" *ngIf="isStateUser" >
<span [style.color]="bgColor">
{{initial | titlecase}}
</span>
</div>
@import "src/assets/styles/_variables.scss";
.profile-avatar{
font-size: 3.125rem;
font-weight: bold !important;
line-height: 3.13rem;
color: map-get($colors, vivid_purple);
border-radius: 50%;
-webkit-box-shadow: 0px 0px 8px 1px rgba(0, 0, 0, 0.25);
box-shadow: 0px 0px 8px 1px rgba(0, 0, 0, 0.25);
padding: 5px;
text-align: center;
min-width: 5rem;
min-height: 5rem;
margin: 0 auto;
display: table;
span {
vertical-align: middle;
display: table-cell;
}
}
.profile-avatar.profileAvatar{
border: 1px solid $green !important;
box-shadow: none;
}
Legend
Html element with directive