mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-11 11:26:27 +00:00
Merge branch 'development'
This commit is contained in:
commit
ab04e9a348
11 files changed
+281
-77
No files matched your search
@@ -107,7 +107,7 @@ namespace Artemis.Core
|
||||
GetRegionData(skRectI.Left, skRectI.Top, skRectI.Width, skRectI.Height, buffer);
|
||||
|
||||
Span<byte> pixelData = stackalloc byte[DATA_PER_PIXEL];
|
||||
Sampler.SampleColor(new SamplerInfo<byte>(skRectI.Width, skRectI.Height, buffer), pixelData);
|
||||
Sampler.Sample(new SamplerInfo<byte>(skRectI.Width, skRectI.Height, buffer), pixelData);
|
||||
|
||||
return GetColor(pixelData);
|
||||
}
|
||||
@@ -119,7 +119,7 @@ namespace Artemis.Core
|
||||
GetRegionData(skRectI.Left, skRectI.Top, skRectI.Width, skRectI.Height, buffer);
|
||||
|
||||
Span<byte> pixelData = stackalloc byte[DATA_PER_PIXEL];
|
||||
Sampler.SampleColor(new SamplerInfo<byte>(skRectI.Width, skRectI.Height, buffer), pixelData);
|
||||
Sampler.Sample(new SamplerInfo<byte>(skRectI.Width, skRectI.Height, buffer), pixelData);
|
||||
|
||||
ArrayPool<byte>.Shared.Return(rent);
|
||||
|
||||
|
||||
@@ -8,21 +8,6 @@ namespace Artemis.Core.Services
|
||||
/// <inheritdoc />
|
||||
internal class ColorQuantizerService : IColorQuantizerService
|
||||
{
|
||||
private static float GetComparisonValue(float sat, float targetSaturation, float luma, float targetLuma)
|
||||
{
|
||||
static float InvertDiff(float value, float target)
|
||||
{
|
||||
return 1 - Math.Abs(value - target);
|
||||
}
|
||||
|
||||
const float totalWeight = weightSaturation + weightLuma;
|
||||
|
||||
float totalValue = InvertDiff(sat, targetSaturation) * weightSaturation +
|
||||
InvertDiff(luma, targetLuma) * weightLuma;
|
||||
|
||||
return totalValue / totalWeight;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public SKColor[] Quantize(IEnumerable<SKColor> colors, int amount)
|
||||
{
|
||||
@@ -48,29 +33,12 @@ namespace Artemis.Core.Services
|
||||
/// <inheritdoc />
|
||||
public SKColor FindColorVariation(IEnumerable<SKColor> colors, ColorType type, bool ignoreLimits = false)
|
||||
{
|
||||
(float targetLuma, float minLuma, float maxLuma, float targetSaturation, float minSaturation, float maxSaturation) = type switch
|
||||
{
|
||||
ColorType.Vibrant => (targetNormalLuma, minNormalLuma, maxNormalLuma, targetVibrantSaturation, minVibrantSaturation, 1f),
|
||||
ColorType.LightVibrant => (targetLightLuma, minLightLuma, 1f, targetVibrantSaturation, minVibrantSaturation, 1f),
|
||||
ColorType.DarkVibrant => (targetDarkLuma, 0f, maxDarkLuma, targetVibrantSaturation, minVibrantSaturation, 1f),
|
||||
ColorType.Muted => (targetNormalLuma, minNormalLuma, maxNormalLuma, targetMutesSaturation, 0, maxMutesSaturation),
|
||||
ColorType.LightMuted => (targetLightLuma, minLightLuma, 1f, targetMutesSaturation, 0, maxMutesSaturation),
|
||||
ColorType.DarkMuted => (targetDarkLuma, 0, maxDarkLuma, targetMutesSaturation, 0, maxMutesSaturation),
|
||||
_ => (0.5f, 0f, 1f, 0.5f, 0f, 1f)
|
||||
};
|
||||
|
||||
float bestColorScore = float.MinValue;
|
||||
float bestColorScore = 0;
|
||||
SKColor bestColor = SKColor.Empty;
|
||||
|
||||
foreach (SKColor clr in colors)
|
||||
{
|
||||
clr.ToHsl(out float _, out float sat, out float luma);
|
||||
sat /= 100f;
|
||||
luma /= 100f;
|
||||
|
||||
if (!ignoreLimits && (sat <= minSaturation || sat >= maxSaturation || luma <= minLuma || luma >= maxLuma))
|
||||
continue;
|
||||
|
||||
float score = GetComparisonValue(sat, targetSaturation, luma, targetLuma);
|
||||
float score = GetScore(clr, type, ignoreLimits);
|
||||
if (score > bestColorScore)
|
||||
{
|
||||
bestColorScore = score;
|
||||
@@ -81,6 +49,82 @@ namespace Artemis.Core.Services
|
||||
return bestColor;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public ColorSwatch FindAllColorVariations(IEnumerable<SKColor> colors, bool ignoreLimits = false)
|
||||
{
|
||||
SKColor bestVibrantColor = SKColor.Empty;
|
||||
SKColor bestLightVibrantColor = SKColor.Empty;
|
||||
SKColor bestDarkVibrantColor = SKColor.Empty;
|
||||
SKColor bestMutedColor = SKColor.Empty;
|
||||
SKColor bestLightMutedColor = SKColor.Empty;
|
||||
SKColor bestDarkMutedColor = SKColor.Empty;
|
||||
float bestVibrantScore = 0;
|
||||
float bestLightVibrantScore = 0;
|
||||
float bestDarkVibrantScore = 0;
|
||||
float bestMutedScore = 0;
|
||||
float bestLightMutedScore = 0;
|
||||
float bestDarkMutedScore = 0;
|
||||
|
||||
//ugly but at least we only loop through the enumerable once ¯\_(ツ)_/¯
|
||||
foreach (var color in colors)
|
||||
{
|
||||
static void SetIfBetterScore(ref float bestScore, ref SKColor bestColor, SKColor newColor, ColorType type, bool ignoreLimits)
|
||||
{
|
||||
float newScore = GetScore(newColor, type, ignoreLimits);
|
||||
if (newScore > bestScore)
|
||||
{
|
||||
bestScore = newScore;
|
||||
bestColor = newColor;
|
||||
}
|
||||
}
|
||||
|
||||
SetIfBetterScore(ref bestVibrantScore, ref bestVibrantColor, color, ColorType.Vibrant, ignoreLimits);
|
||||
SetIfBetterScore(ref bestLightVibrantScore, ref bestLightVibrantColor, color, ColorType.LightVibrant, ignoreLimits);
|
||||
SetIfBetterScore(ref bestDarkVibrantScore, ref bestDarkVibrantColor, color, ColorType.DarkVibrant, ignoreLimits);
|
||||
SetIfBetterScore(ref bestMutedScore, ref bestMutedColor, color, ColorType.Muted, ignoreLimits);
|
||||
SetIfBetterScore(ref bestLightMutedScore, ref bestLightMutedColor, color, ColorType.LightMuted, ignoreLimits);
|
||||
SetIfBetterScore(ref bestDarkMutedScore, ref bestDarkMutedColor, color, ColorType.DarkMuted, ignoreLimits);
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Vibrant = bestVibrantColor,
|
||||
LightVibrant = bestLightVibrantColor,
|
||||
DarkVibrant = bestDarkVibrantColor,
|
||||
Muted = bestMutedColor,
|
||||
LightMuted = bestLightMutedColor,
|
||||
DarkMuted = bestDarkMutedColor,
|
||||
};
|
||||
}
|
||||
|
||||
private static float GetScore(SKColor color, ColorType type, bool ignoreLimits = false)
|
||||
{
|
||||
static float InvertDiff(float value, float target)
|
||||
{
|
||||
return 1 - Math.Abs(value - target);
|
||||
}
|
||||
|
||||
color.ToHsl(out float _, out float saturation, out float luma);
|
||||
saturation /= 100f;
|
||||
luma /= 100f;
|
||||
|
||||
if (!ignoreLimits &&
|
||||
(saturation <= GetMinSaturation(type) || saturation >= GetMaxSaturation(type)
|
||||
|| luma <= GetMinLuma(type) || luma >= GetMaxLuma(type)))
|
||||
{
|
||||
//if either saturation or luma falls outside the min-max, return the
|
||||
//lowest score possible unless we're ignoring these limits.
|
||||
return float.MinValue;
|
||||
}
|
||||
|
||||
float totalValue = (InvertDiff(saturation, GetTargetSaturation(type)) * weightSaturation) +
|
||||
(InvertDiff(luma, GetTargetLuma(type)) * weightLuma);
|
||||
|
||||
const float totalWeight = weightSaturation + weightLuma;
|
||||
|
||||
return totalValue / totalWeight;
|
||||
}
|
||||
|
||||
#region Constants
|
||||
|
||||
private const float targetDarkLuma = 0.26f;
|
||||
@@ -97,6 +141,72 @@ namespace Artemis.Core.Services
|
||||
private const float weightSaturation = 3f;
|
||||
private const float weightLuma = 5f;
|
||||
|
||||
private static float GetTargetLuma(ColorType colorType) => colorType switch
|
||||
{
|
||||
ColorType.Vibrant => targetNormalLuma,
|
||||
ColorType.LightVibrant => targetLightLuma,
|
||||
ColorType.DarkVibrant => targetDarkLuma,
|
||||
ColorType.Muted => targetNormalLuma,
|
||||
ColorType.LightMuted => targetLightLuma,
|
||||
ColorType.DarkMuted => targetDarkLuma,
|
||||
_ => throw new ArgumentException(nameof(colorType))
|
||||
};
|
||||
|
||||
private static float GetMinLuma(ColorType colorType) => colorType switch
|
||||
{
|
||||
ColorType.Vibrant => minNormalLuma,
|
||||
ColorType.LightVibrant => minLightLuma,
|
||||
ColorType.DarkVibrant => 0f,
|
||||
ColorType.Muted => minNormalLuma,
|
||||
ColorType.LightMuted => minLightLuma,
|
||||
ColorType.DarkMuted => 0,
|
||||
_ => throw new ArgumentException(nameof(colorType))
|
||||
};
|
||||
|
||||
private static float GetMaxLuma(ColorType colorType) => colorType switch
|
||||
{
|
||||
ColorType.Vibrant => maxNormalLuma,
|
||||
ColorType.LightVibrant => 1f,
|
||||
ColorType.DarkVibrant => maxDarkLuma,
|
||||
ColorType.Muted => maxNormalLuma,
|
||||
ColorType.LightMuted => 1f,
|
||||
ColorType.DarkMuted => maxDarkLuma,
|
||||
_ => throw new ArgumentException(nameof(colorType))
|
||||
};
|
||||
|
||||
private static float GetTargetSaturation(ColorType colorType) => colorType switch
|
||||
{
|
||||
ColorType.Vibrant => targetVibrantSaturation,
|
||||
ColorType.LightVibrant => targetVibrantSaturation,
|
||||
ColorType.DarkVibrant => targetVibrantSaturation,
|
||||
ColorType.Muted => targetMutesSaturation,
|
||||
ColorType.LightMuted => targetMutesSaturation,
|
||||
ColorType.DarkMuted => targetMutesSaturation,
|
||||
_ => throw new ArgumentException(nameof(colorType))
|
||||
};
|
||||
|
||||
private static float GetMinSaturation(ColorType colorType) => colorType switch
|
||||
{
|
||||
ColorType.Vibrant => minVibrantSaturation,
|
||||
ColorType.LightVibrant => minVibrantSaturation,
|
||||
ColorType.DarkVibrant => minVibrantSaturation,
|
||||
ColorType.Muted => 0,
|
||||
ColorType.LightMuted => 0,
|
||||
ColorType.DarkMuted => 0,
|
||||
_ => throw new ArgumentException(nameof(colorType))
|
||||
};
|
||||
|
||||
private static float GetMaxSaturation(ColorType colorType) => colorType switch
|
||||
{
|
||||
ColorType.Vibrant => 1f,
|
||||
ColorType.LightVibrant => 1f,
|
||||
ColorType.DarkVibrant => 1f,
|
||||
ColorType.Muted => maxMutesSaturation,
|
||||
ColorType.LightMuted => maxMutesSaturation,
|
||||
ColorType.DarkMuted => maxMutesSaturation,
|
||||
_ => throw new ArgumentException(nameof(colorType))
|
||||
};
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Swatch containing the known useful color variations.
|
||||
/// </summary>
|
||||
public struct ColorSwatch
|
||||
{
|
||||
/// <summary>
|
||||
/// The <see cref="ColorType.Vibrant"/> component.
|
||||
/// </summary>
|
||||
public SKColor Vibrant { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ColorType.LightVibrant"/> component.
|
||||
/// </summary>
|
||||
public SKColor LightVibrant { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ColorType.DarkVibrant"/> component.
|
||||
/// </summary>
|
||||
public SKColor DarkVibrant { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ColorType.Muted"/> component.
|
||||
/// </summary>
|
||||
public SKColor Muted { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ColorType.LightMuted"/> component.
|
||||
/// </summary>
|
||||
public SKColor LightMuted { get; init; }
|
||||
|
||||
/// <summary>
|
||||
/// The <see cref="ColorType.DarkMuted"/> component.
|
||||
/// </summary>
|
||||
public SKColor DarkMuted { get; init; }
|
||||
}
|
||||
}
|
||||
@@ -26,5 +26,13 @@ namespace Artemis.Core.Services
|
||||
/// <param name="ignoreLimits">Ignore hard limits on whether a color is considered for each category. Result may be <see cref="SKColor.Empty"/> if this is false</param>
|
||||
/// <returns>The color found</returns>
|
||||
public SKColor FindColorVariation(IEnumerable<SKColor> colors, ColorType type, bool ignoreLimits = false);
|
||||
|
||||
/// <summary>
|
||||
/// Finds all the color variations available and returns a struct containing them all.
|
||||
/// </summary>
|
||||
/// <param name="colors">The colors to find the variations in</param>
|
||||
/// <param name="ignoreLimits">Ignore hard limits on whether a color is considered for each category. Some colors may be <see cref="SKColor.Empty"/> if this is false</param>
|
||||
/// <returns>A swatch containing all color variations</returns>
|
||||
public ColorSwatch FindAllColorVariations(IEnumerable<SKColor> colors, bool ignoreLimits = false);
|
||||
}
|
||||
}
|
||||
@@ -13,7 +13,6 @@ using RGB.NET.Core;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using SkiaSharp;
|
||||
using Module = Artemis.Core.Modules.Module;
|
||||
|
||||
namespace Artemis.Core.Services
|
||||
{
|
||||
@@ -107,6 +106,13 @@ namespace Artemis.Core.Services
|
||||
if (_rgbService.IsRenderPaused)
|
||||
return;
|
||||
|
||||
if (_rgbService.FlushLeds)
|
||||
{
|
||||
_rgbService.FlushLeds = false;
|
||||
_rgbService.Surface.Update(true);
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_frameStopWatch.Restart();
|
||||
|
||||
@@ -41,6 +41,11 @@ namespace Artemis.Core.Services
|
||||
/// </summary>
|
||||
bool RenderOpen { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a boolean indicating whether to flush the RGB.NET LEDs during next update
|
||||
/// </summary>
|
||||
bool FlushLeds { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Opens the render pipeline
|
||||
/// </summary>
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using Artemis.Core.DeviceProviders;
|
||||
@@ -53,7 +52,7 @@ namespace Artemis.Core.Services
|
||||
|
||||
UpdateTrigger = new TimerUpdateTrigger {UpdateFrequency = 1.0 / _targetFrameRateSetting.Value};
|
||||
Surface.RegisterUpdateTrigger(UpdateTrigger);
|
||||
|
||||
|
||||
Utilities.ShutdownRequested += UtilitiesOnShutdownRequested;
|
||||
}
|
||||
|
||||
@@ -137,8 +136,14 @@ namespace Artemis.Core.Services
|
||||
public bool IsRenderPaused { get; set; }
|
||||
public bool RenderOpen { get; private set; }
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool FlushLeds { get; set; }
|
||||
|
||||
public void AddDeviceProvider(IRGBDeviceProvider deviceProvider)
|
||||
{
|
||||
if (RenderOpen)
|
||||
throw new ArtemisCoreException("Cannot add a device provider while rendering");
|
||||
|
||||
lock (_devices)
|
||||
{
|
||||
try
|
||||
@@ -199,6 +204,9 @@ namespace Artemis.Core.Services
|
||||
|
||||
public void RemoveDeviceProvider(IRGBDeviceProvider deviceProvider)
|
||||
{
|
||||
if (RenderOpen)
|
||||
throw new ArtemisCoreException("Cannot update the remove device provider while rendering");
|
||||
|
||||
lock (_devices)
|
||||
{
|
||||
try
|
||||
@@ -267,36 +275,39 @@ namespace Artemis.Core.Services
|
||||
if (RenderOpen)
|
||||
throw new ArtemisCoreException("Cannot update the texture while rendering");
|
||||
|
||||
IManagedGraphicsContext? graphicsContext = Constants.ManagedGraphicsContext = _newGraphicsContext;
|
||||
if (!ReferenceEquals(graphicsContext, _newGraphicsContext))
|
||||
graphicsContext = _newGraphicsContext;
|
||||
|
||||
if (graphicsContext != null)
|
||||
_logger.Debug("Creating SKTexture with graphics context {graphicsContext}", graphicsContext.GetType().Name);
|
||||
else
|
||||
_logger.Debug("Creating SKTexture with software-based graphics context");
|
||||
|
||||
float evenWidth = Surface.Boundary.Size.Width;
|
||||
if (evenWidth % 2 != 0)
|
||||
evenWidth++;
|
||||
float evenHeight = Surface.Boundary.Size.Height;
|
||||
if (evenHeight % 2 != 0)
|
||||
evenHeight++;
|
||||
|
||||
float renderScale = (float) _renderScaleSetting.Value;
|
||||
int width = Math.Max(1, MathF.Min(evenWidth * renderScale, 4096).RoundToInt());
|
||||
int height = Math.Max(1, MathF.Min(evenHeight * renderScale, 4096).RoundToInt());
|
||||
|
||||
_texture?.Dispose();
|
||||
_texture = new SKTexture(graphicsContext, width, height, renderScale);
|
||||
_textureBrush.Texture = _texture;
|
||||
|
||||
|
||||
if (!ReferenceEquals(_newGraphicsContext, Constants.ManagedGraphicsContext = _newGraphicsContext))
|
||||
lock (_devices)
|
||||
{
|
||||
Constants.ManagedGraphicsContext?.Dispose();
|
||||
Constants.ManagedGraphicsContext = _newGraphicsContext;
|
||||
_newGraphicsContext = null;
|
||||
IManagedGraphicsContext? graphicsContext = Constants.ManagedGraphicsContext = _newGraphicsContext;
|
||||
if (!ReferenceEquals(graphicsContext, _newGraphicsContext))
|
||||
graphicsContext = _newGraphicsContext;
|
||||
|
||||
if (graphicsContext != null)
|
||||
_logger.Debug("Creating SKTexture with graphics context {graphicsContext}", graphicsContext.GetType().Name);
|
||||
else
|
||||
_logger.Debug("Creating SKTexture with software-based graphics context");
|
||||
|
||||
float evenWidth = Surface.Boundary.Size.Width;
|
||||
if (evenWidth % 2 != 0)
|
||||
evenWidth++;
|
||||
float evenHeight = Surface.Boundary.Size.Height;
|
||||
if (evenHeight % 2 != 0)
|
||||
evenHeight++;
|
||||
|
||||
float renderScale = (float) _renderScaleSetting.Value;
|
||||
int width = Math.Max(1, MathF.Min(evenWidth * renderScale, 4096).RoundToInt());
|
||||
int height = Math.Max(1, MathF.Min(evenHeight * renderScale, 4096).RoundToInt());
|
||||
|
||||
_texture?.Dispose();
|
||||
_texture = new SKTexture(graphicsContext, width, height, renderScale);
|
||||
_textureBrush.Texture = _texture;
|
||||
|
||||
|
||||
if (!ReferenceEquals(_newGraphicsContext, Constants.ManagedGraphicsContext = _newGraphicsContext))
|
||||
{
|
||||
Constants.ManagedGraphicsContext?.Dispose();
|
||||
Constants.ManagedGraphicsContext = _newGraphicsContext;
|
||||
_newGraphicsContext = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user