using System.Collections.Generic; using System.Threading.Tasks; using System.Windows.Media; using Artemis.Core; using Artemis.Core.Services; using Artemis.UI.Shared.Screens.GradientEditor; using SkiaSharp; namespace Artemis.UI.Shared.Services { internal class ColorPickerService : IColorPickerService { private readonly ICoreService _coreService; private readonly IDialogService _dialogService; private bool _mustRenderOverlay; private SKColor _overlayColor; private float _overlayOpacity; public ColorPickerService(IDialogService dialogService, ICoreService coreService, ISettingsService settingsService) { _dialogService = dialogService; _coreService = coreService; PreviewSetting = settingsService.GetSetting("UI.PreviewColorPickerOnDevices", false); PreviewSetting.AutoSave = true; } public PluginSetting PreviewSetting { get; } public Task ShowGradientPicker(ColorGradient colorGradient, string dialogHost) { if (!string.IsNullOrWhiteSpace(dialogHost)) return _dialogService.ShowDialogAt(dialogHost, new Dictionary {{"colorGradient", colorGradient}}); return _dialogService.ShowDialog(new Dictionary {{"colorGradient", colorGradient}}); } public void StartColorDisplay() { if (_mustRenderOverlay) return; _overlayOpacity = 0f; _mustRenderOverlay = true; _coreService.FrameRendering += RenderColorPickerOverlay; } public void StopColorDisplay() { _mustRenderOverlay = false; } public void UpdateColorDisplay(Color color) { _overlayColor = new SKColor(color.R, color.G, color.B, color.A); } private void RenderColorPickerOverlay(object? sender, FrameRenderingEventArgs e) { if (_mustRenderOverlay) _overlayOpacity += 0.2f; else _overlayOpacity -= 0.2f; if (_overlayOpacity <= 0f) { _coreService.FrameRendering -= RenderColorPickerOverlay; return; } if (_overlayOpacity > 1f) _overlayOpacity = 1f; using SKPaint overlayPaint = new() {Color = new SKColor(0, 0, 0, (byte) (255 * _overlayOpacity))}; overlayPaint.Color = _overlayColor.WithAlpha((byte) (_overlayColor.Alpha * _overlayOpacity)); e.Canvas.DrawRect(0, 0, e.Canvas.LocalClipBounds.Width, e.Canvas.LocalClipBounds.Height, overlayPaint); } } }