committed by
GitHub
8 changed files with 291 additions and 41 deletions
@ -0,0 +1,41 @@ |
|||
<!-- |
|||
|
|||
Copyright © 2016-2023 The Thingsboard Authors |
|||
|
|||
Licensed under the Apache License, Version 2.0 (the "License"); |
|||
you may not use this file except in compliance with the License. |
|||
You may obtain a copy of the License at |
|||
|
|||
http://www.apache.org/licenses/LICENSE-2.0 |
|||
|
|||
Unless required by applicable law or agreed to in writing, software |
|||
distributed under the License is distributed on an "AS IS" BASIS, |
|||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
See the License for the specific language governing permissions and |
|||
limitations under the License. |
|||
|
|||
--> |
|||
<saturation-component class="saturation-component" [hue]="control.hue" [(color)]="control.value"></saturation-component> |
|||
|
|||
<div class="control-component"> |
|||
<indicator-component class="indicator-component" |
|||
[colorType]="presentations[selectedPresentation]" |
|||
[color]="control.value"> |
|||
</indicator-component> |
|||
<div class="hue-alpha-range"> |
|||
<hue-component [(hue)]="control.hue" [(color)]="control.value"></hue-component> |
|||
<alpha-component [(color)]="control.value"></alpha-component> |
|||
</div> |
|||
</div> |
|||
|
|||
<div class="color-input-block"> |
|||
<div class="color-input" [ngSwitch]="presentations[selectedPresentation]"> |
|||
<rgba-input-component *ngSwitchCase="'rgba'" label |
|||
[(color)]="control.value" [(hue)]="control.hue"></rgba-input-component> |
|||
<hsla-input-component *ngSwitchCase="'hsla'" label |
|||
[(color)]="control.value" [(hue)]="control.hue"></hsla-input-component> |
|||
<hex-input-component *ngSwitchCase="'hex'" label prefix="#" [(color)]="control.value" |
|||
[(hue)]="control.hue"></hex-input-component> |
|||
</div> |
|||
<div class="type-btn" (click)="changePresentation()"></div> |
|||
</div> |
|||
@ -0,0 +1,104 @@ |
|||
/** |
|||
* Copyright © 2016-2023 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0 |
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
:host { |
|||
width: 100%; |
|||
display: flex; |
|||
flex-direction: column; |
|||
gap: 16px; |
|||
|
|||
.saturation-component { |
|||
height: 100%; |
|||
min-height: 200px; |
|||
max-height: 300px; |
|||
border-radius: 8px; |
|||
} |
|||
|
|||
.control-component { |
|||
max-height: 48px; |
|||
display: flex; |
|||
gap: 16px; |
|||
|
|||
.indicator-component { |
|||
height: 48px; |
|||
width: 48px; |
|||
border-radius: 8px; |
|||
background: transparent url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQAQMAAAAlPW0iAAAABlBMVEUAAAAAAAClZ7nPAAAAAnRSTlMUAFg9Gm0AAAAPSURBVAjXY2D4jxXhEgYAfr8P8QhChVEAAAAASUVORK5CYII=') repeat; |
|||
} |
|||
|
|||
.hue-alpha-range { |
|||
display: flex; |
|||
justify-content: space-between; |
|||
flex-direction: column; |
|||
flex: 1; |
|||
|
|||
> * { |
|||
height: 18px; |
|||
overflow: hidden; |
|||
border-radius: 9px; |
|||
border: 1px solid rgba(0, 0, 0, 0.1); |
|||
} |
|||
} |
|||
} |
|||
|
|||
.color-input-block { |
|||
display: flex; |
|||
|
|||
.color-input { |
|||
flex: 1; |
|||
} |
|||
|
|||
.type-btn { |
|||
height: 26px; |
|||
width: 20px; |
|||
background: transparent url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAAgCAMAAAAootjDAAAAM1BMVEUAAAAzMzMzMzMzMzMzMzM0NDQzMzMzMzM0NDQzMzMzMzM0NDQzMzMzMzMyMjIrKyszMzPF8UZlAAAAEHRSTlMA1fHr4ZxxSRP45sG+sCkGH2+Z6QAAAHJJREFUKM+9kkkSgCAQA0FEVLb5/2tViqgQvNrHviSzKGCt6nDGuNass8i8NsrLiX+bZbrUtDwm7VLYE0zWUtEZ+RvUZpEvN8YhH9QmQRoC8kFpEnVHVP/DJUZVeSAem5fDKxwtms/BR+PT8gN8vwk/0wE1gQzNVYryIwAAAABJRU5ErkJggg==') no-repeat center; |
|||
background-size: 6px 12px; |
|||
|
|||
&:hover { |
|||
background-color: #eee; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
|
|||
:host ::ng-deep { |
|||
.saturation-component { |
|||
.pointer { |
|||
border-width: 2px; |
|||
width: 16px; |
|||
height: 16px; |
|||
} |
|||
} |
|||
|
|||
indicator-component { |
|||
svg { |
|||
vertical-align: -70% !important; |
|||
} |
|||
} |
|||
|
|||
.hue-alpha-range { |
|||
alpha-component, hue-component { |
|||
.pointer { |
|||
height: 18px; |
|||
width: 18px; |
|||
background: none; |
|||
border: 2px solid #fff; |
|||
} |
|||
.gradient-color { |
|||
border-radius: 9px; |
|||
} |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,124 @@ |
|||
///
|
|||
/// Copyright © 2016-2023 The Thingsboard Authors
|
|||
///
|
|||
/// Licensed under the Apache License, Version 2.0 (the "License");
|
|||
/// you may not use this file except in compliance with the License.
|
|||
/// You may obtain a copy of the License at
|
|||
///
|
|||
/// http://www.apache.org/licenses/LICENSE-2.0
|
|||
///
|
|||
/// Unless required by applicable law or agreed to in writing, software
|
|||
/// distributed under the License is distributed on an "AS IS" BASIS,
|
|||
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|||
/// See the License for the specific language governing permissions and
|
|||
/// limitations under the License.
|
|||
///
|
|||
|
|||
import { Component, forwardRef, OnDestroy } from '@angular/core'; |
|||
import { Color, ColorPickerControl } from '@iplab/ngx-color-picker'; |
|||
import { Subscription } from 'rxjs'; |
|||
import { ControlValueAccessor, NG_VALUE_ACCESSOR } from '@angular/forms'; |
|||
|
|||
export enum ColorType { |
|||
hex = 'hex', |
|||
hexa = 'hexa', |
|||
rgba = 'rgba', |
|||
rgb = 'rgb', |
|||
hsla = 'hsla', |
|||
hsl = 'hsl', |
|||
cmyk = 'cmyk' |
|||
} |
|||
|
|||
@Component({ |
|||
selector: `tb-color-picker`, |
|||
templateUrl: `./color-picker.component.html`, |
|||
styleUrls: [`./color-picker.component.scss`], |
|||
providers: [ |
|||
{ |
|||
provide: NG_VALUE_ACCESSOR, |
|||
useExisting: forwardRef(() => ColorPickerComponent), |
|||
multi: true |
|||
} |
|||
] |
|||
}) |
|||
export class ColorPickerComponent implements ControlValueAccessor, OnDestroy { |
|||
|
|||
selectedPresentation = 0; |
|||
presentations = [ColorType.hex, ColorType.rgba, ColorType.hsla]; |
|||
control = new ColorPickerControl(); |
|||
|
|||
private modelValue: string; |
|||
|
|||
private subscriptions: Array<Subscription> = []; |
|||
|
|||
private propagateChange = (v: any) => {}; |
|||
|
|||
private setValue = false; |
|||
|
|||
constructor() { |
|||
this.subscriptions.push( |
|||
this.control.valueChanges.subscribe(() => { |
|||
if (!this.setValue) { |
|||
this.updateModel(); |
|||
} else { |
|||
this.setValue = false; |
|||
} |
|||
}) |
|||
); |
|||
} |
|||
|
|||
registerOnChange(fn: any): void { |
|||
this.propagateChange = fn; |
|||
} |
|||
|
|||
registerOnTouched(fn: any): void { |
|||
} |
|||
|
|||
writeValue(value: string): void { |
|||
this.setValue = !!value; |
|||
this.control.setValueFrom(value || '#fff'); |
|||
this.modelValue = value; |
|||
|
|||
if (this.control.initType === ColorType.hexa) { |
|||
this.control.initType = ColorType.hex; |
|||
} else if (this.control.initType === ColorType.rgb) { |
|||
this.control.initType = ColorType.rgba; |
|||
} else if (this.control.initType === ColorType.hsl) { |
|||
this.control.initType = ColorType.hsla; |
|||
} |
|||
|
|||
this.selectedPresentation = this.presentations.indexOf(this.control.initType); |
|||
} |
|||
|
|||
private updateModel() { |
|||
const color: string = this.getValueByType(this.control.value, this.presentations[this.selectedPresentation]); |
|||
if (this.modelValue !== color) { |
|||
this.modelValue = color; |
|||
this.propagateChange(color); |
|||
} |
|||
} |
|||
|
|||
public ngOnDestroy(): void { |
|||
this.subscriptions.forEach((subscription) => subscription.unsubscribe()); |
|||
this.subscriptions.length = 0; |
|||
} |
|||
|
|||
public changePresentation(): void { |
|||
this.selectedPresentation = |
|||
this.selectedPresentation === this.presentations.length - 1 ? 0 : this.selectedPresentation + 1; |
|||
this.updateModel(); |
|||
} |
|||
|
|||
getValueByType(color: Color, type: ColorType): string { |
|||
switch (type) { |
|||
case ColorType.hex: |
|||
return color.toHexString(this.control.value.getRgba().getAlpha() !== 1); |
|||
case ColorType.rgba: |
|||
return this.control.value.getRgba().getAlpha() !== 1 ? color.toRgbaString() : color.toRgbString(); |
|||
case ColorType.hsla: |
|||
return this.control.value.getRgba().getAlpha() !== 1 ? color.toHslaString() : color.toHslString(); |
|||
default: |
|||
return color.toRgbaString(); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue