Browse Source

UI: Refactoring ColorProcessor added new method fromColorProcessingSettings

pull/10557/head
Vladyslav_Prykhodko 2 years ago
parent
commit
1d316b9db1
  1. 24
      ui-ngx/src/app/modules/home/components/widget/lib/indicator/battery-level-widget.component.ts
  2. 37
      ui-ngx/src/app/shared/models/widget-settings.models.ts

24
ui-ngx/src/app/modules/home/components/widget/lib/indicator/battery-level-widget.component.ts

@ -37,8 +37,7 @@ import {
getDataKey,
getSingleTsValue,
overlayStyle,
textStyle,
updateGradientMinMaxValues
textStyle
} from '@shared/models/widget-settings.models';
import { WidgetComponent } from '@home/components/widget/widget.component';
import {
@ -192,11 +191,26 @@ export class BatteryLevelWidgetComponent implements OnInit, OnDestroy, AfterView
this.showValue = this.settings.showValue;
this.autoScaleValueSize = this.showValue && this.settings.autoScaleValueSize;
this.valueStyle = textStyle(this.settings.valueFont);
this.valueColor = ColorProcessor.fromSettings(updateGradientMinMaxValues(this.settings.valueColor, 0, 100), this.ctx);
this.valueColor = ColorProcessor.fromColorProcessingSettings({
settings: this.settings.valueColor,
ctx: this.ctx,
minGradientValue: 0,
maxGradientValue: 0
});
this.batteryLevelColor = ColorProcessor.fromSettings(updateGradientMinMaxValues(this.settings.batteryLevelColor, 0, 100), this.ctx);
this.batteryLevelColor = ColorProcessor.fromColorProcessingSettings({
settings: this.settings.batteryLevelColor,
ctx: this.ctx,
minGradientValue: 0,
maxGradientValue: 0
});
this.batteryShapeColor = ColorProcessor.fromSettings(updateGradientMinMaxValues(this.settings.batteryShapeColor, 0, 100), this.ctx);
this.batteryShapeColor = ColorProcessor.fromColorProcessingSettings({
settings: this.settings.batteryShapeColor,
ctx: this.ctx,
minGradientValue: 0,
maxGradientValue: 0
});
this.backgroundStyle$ = backgroundStyle(this.settings.background, this.imagePipe, this.sanitizer);
this.overlayStyle = overlayStyle(this.settings.background.overlay);

37
ui-ngx/src/app/shared/models/widget-settings.models.ts

@ -297,14 +297,15 @@ export const defaultGradient = (minValue?: number, maxValue?: number): ColorGrad
maxValue: isDefinedAndNotNull(maxValue) ? maxValue : 100
});
export const updateGradientMinMaxValues = (colorSettings: ColorSettings, minValue: number, maxValue: number): ColorSettings => {
const updateGradientMinMaxValues = (colorSettings: ColorSettings, minValue?: number, maxValue?: number): void => {
if (isDefinedAndNotNull(colorSettings.gradient)) {
colorSettings.gradient.minValue = minValue;
colorSettings.gradient.maxValue = maxValue;
} else {
colorSettings.gradient = defaultGradient(minValue, maxValue);
if (isDefinedAndNotNull(minValue)) {
colorSettings.gradient.minValue = minValue;
}
if (isDefinedAndNotNull(maxValue)) {
colorSettings.gradient.maxValue = maxValue;
}
}
return colorSettings;
};
export const defaultColorFunction = 'var temperature = value;\n' +
@ -350,6 +351,13 @@ export const validateCssSize = (strSize?: string): string | undefined => {
type ValueColorFunction = (value: any) => string;
export interface ColorProcessorSettings {
settings: ColorSettings;
ctx?: WidgetContext;
minGradientValue?: number;
maxGradientValue?: number;
}
export abstract class ColorProcessor {
static fromSettings(color: ColorSettings, ctx?: WidgetContext): ColorProcessor {
@ -368,6 +376,23 @@ export abstract class ColorProcessor {
}
}
static fromColorProcessingSettings(setting: ColorProcessorSettings): ColorProcessor {
const settings = setting.settings || constantColor(null);
switch (settings.type) {
case ColorType.constant:
return new ConstantColorProcessor(settings);
case ColorType.range:
return new RangeColorProcessor(settings, setting.ctx);
case ColorType.function:
return new FunctionColorProcessor(settings);
case ColorType.gradient:
updateGradientMinMaxValues(settings, setting.minGradientValue, setting.maxGradientValue);
return new GradientColorProcessor(settings, setting.ctx);
default:
return new ConstantColorProcessor(settings);
}
}
color: string;
colorUpdated: EventEmitter<void> = new EventEmitter<void>();

Loading…
Cancel
Save