-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathshade.component.ts
136 lines (128 loc) · 2.92 KB
/
shade.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import { CommonModule } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
Input,
NgModule,
OnChanges,
Output,
} from '@angular/core';
import { CoordinatesModule } from './coordinates.directive';
import { HSLA, RGBA } from './helpers/color.interfaces';
import { TinyColor } from '@ctrl/tinycolor';
@Component({
selector: 'color-shade',
template: `
<div class="shade" [style.border-radius]="radius">
<div
class="shade-gradient"
[ngStyle]="gradient"
[style.box-shadow]="shadow"
[style.border-radius]="radius"
></div>
<div
ngx-color-coordinates
(coordinatesChange)="handleChange($event)"
class="shade-container"
>
<div
class="shade-pointer"
[style.left.%]="pointerLeft"
[style.top.%]="pointerTop"
>
<div class="shade-slider" [ngStyle]="pointer"></div>
</div>
</div>
</div>
`,
styles: [
`
.shade {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.shade-gradient {
position: absolute;
top: 0;
bottom: 0;
left: 0;
right: 0;
}
.shade-container {
position: relative;
height: 100%;
margin: 0 3px;
}
.shade-pointer {
position: absolute;
}
.shade-slider {
width: 4px;
border-radius: 1px;
height: 8px;
box-shadow: 0 0 2px rgba(0, 0, 0, .6);
background: #fff;
margin-top: 1px;
transform: translateX(-2px);
},
`,
],
changeDetection: ChangeDetectionStrategy.OnPush,
preserveWhitespaces: false,
})
export class ShadeComponent implements OnChanges {
@Input() hsl!: HSLA;
@Input() rgb!: RGBA;
@Input() pointer!: { [key: string]: string };
@Input() shadow!: string;
@Input() radius!: string;
@Output() onChange = new EventEmitter<any>();
gradient!: { [key: string]: string };
pointerLeft!: number;
pointerTop?: number;
ngOnChanges() {
this.gradient = {
background: `linear-gradient(to right,
hsl(${this.hsl.h}, 90%, 55%),
#000)`,
};
const hsv = new TinyColor(this.hsl).toHsv();
this.pointerLeft = 100 - (hsv.v * 100);
}
handleChange({ left, containerWidth, $event }): void {
let data;
let v: number;
if (left < 0) {
v = 0;
} else if (left > containerWidth) {
v = 1;
} else {
v = Math.round((left * 100) / containerWidth) / 100;
}
const hsv = new TinyColor(this.hsl).toHsv();
if (hsv.v !== v) {
data = {
h: this.hsl.h,
s: 100,
v: 1 - v,
l: this.hsl.l,
a: this.hsl.a,
source: 'rgb',
};
}
if (!data) {
return;
}
this.onChange.emit({ data, $event });
}
}
@NgModule({
declarations: [ShadeComponent],
exports: [ShadeComponent],
imports: [CommonModule, CoordinatesModule],
})
export class ShadeModule {}