mirror of
https://github.com/Artemis-RGB/Artemis
synced 2026-11-12 11:56:30 +00:00
Core - Moved color sorter and quantizer to static classes
Nodes - Added colorgradient sort node Nodes - Added GradientBuilder node
This commit is contained in:
1 parent
761447b3d2
commit
d0f665aa10
16 files changed
+905
-342
No files matched your search
@@ -1,66 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.Services;
|
||||
|
||||
internal class ColorCube
|
||||
{
|
||||
private readonly List<SKColor> _colors;
|
||||
|
||||
internal ColorCube(IEnumerable<SKColor> colors)
|
||||
{
|
||||
if (colors.Count() < 2)
|
||||
{
|
||||
_colors = colors.ToList();
|
||||
return;
|
||||
}
|
||||
|
||||
int redRange = colors.Max(c => c.Red) - colors.Min(c => c.Red);
|
||||
int greenRange = colors.Max(c => c.Green) - colors.Min(c => c.Green);
|
||||
int blueRange = colors.Max(c => c.Blue) - colors.Min(c => c.Blue);
|
||||
|
||||
if (redRange > greenRange && redRange > blueRange)
|
||||
_colors = colors.OrderBy(a => a.Red).ToList();
|
||||
else if (greenRange > blueRange)
|
||||
_colors = colors.OrderBy(a => a.Green).ToList();
|
||||
else
|
||||
_colors = colors.OrderBy(a => a.Blue).ToList();
|
||||
}
|
||||
|
||||
internal bool TrySplit([NotNullWhen(true)] out ColorCube? a, [NotNullWhen(true)] out ColorCube? b)
|
||||
{
|
||||
if (_colors.Count < 2)
|
||||
{
|
||||
a = null;
|
||||
b = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
int median = _colors.Count / 2;
|
||||
|
||||
a = new ColorCube(_colors.GetRange(0, median));
|
||||
b = new ColorCube(_colors.GetRange(median, _colors.Count - median));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
internal SKColor GetAverageColor()
|
||||
{
|
||||
int r = 0, g = 0, b = 0;
|
||||
|
||||
for (int i = 0; i < _colors.Count; i++)
|
||||
{
|
||||
r += _colors[i].Red;
|
||||
g += _colors[i].Green;
|
||||
b += _colors[i].Blue;
|
||||
}
|
||||
|
||||
return new SKColor(
|
||||
(byte) (r / _colors.Count),
|
||||
(byte) (g / _colors.Count),
|
||||
(byte) (b / _colors.Count)
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,227 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.Services;
|
||||
|
||||
/// <inheritdoc />
|
||||
internal class ColorQuantizerService : IColorQuantizerService
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public SKColor[] Quantize(IEnumerable<SKColor> colors, int amount)
|
||||
{
|
||||
if ((amount & (amount - 1)) != 0)
|
||||
throw new ArgumentException("Must be power of two", nameof(amount));
|
||||
|
||||
Queue<ColorCube> cubes = new(amount);
|
||||
cubes.Enqueue(new ColorCube(colors));
|
||||
|
||||
while (cubes.Count < amount)
|
||||
{
|
||||
ColorCube cube = cubes.Dequeue();
|
||||
if (cube.TrySplit(out ColorCube? a, out ColorCube? b))
|
||||
{
|
||||
cubes.Enqueue(a);
|
||||
cubes.Enqueue(b);
|
||||
}
|
||||
}
|
||||
|
||||
return cubes.Select(c => c.GetAverageColor()).ToArray();
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public SKColor FindColorVariation(IEnumerable<SKColor> colors, ColorType type, bool ignoreLimits = false)
|
||||
{
|
||||
float bestColorScore = 0;
|
||||
SKColor bestColor = SKColor.Empty;
|
||||
|
||||
foreach (SKColor clr in colors)
|
||||
{
|
||||
float score = GetScore(clr, type, ignoreLimits);
|
||||
if (score > bestColorScore)
|
||||
{
|
||||
bestColorScore = score;
|
||||
bestColor = clr;
|
||||
}
|
||||
}
|
||||
|
||||
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 (SKColor 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 ColorSwatch
|
||||
{
|
||||
Vibrant = bestVibrantColor,
|
||||
LightVibrant = bestLightVibrantColor,
|
||||
DarkVibrant = bestDarkVibrantColor,
|
||||
Muted = bestMutedColor,
|
||||
LightMuted = bestLightMutedColor,
|
||||
DarkMuted = bestDarkMutedColor
|
||||
};
|
||||
}
|
||||
|
||||
#region Constants
|
||||
|
||||
private const float targetDarkLuma = 0.26f;
|
||||
private const float maxDarkLuma = 0.45f;
|
||||
private const float minLightLuma = 0.55f;
|
||||
private const float targetLightLuma = 0.74f;
|
||||
private const float minNormalLuma = 0.3f;
|
||||
private const float targetNormalLuma = 0.5f;
|
||||
private const float maxNormalLuma = 0.7f;
|
||||
private const float targetMutesSaturation = 0.3f;
|
||||
private const float maxMutesSaturation = 0.3f;
|
||||
private const float targetVibrantSaturation = 1.0f;
|
||||
private const float minVibrantSaturation = 0.35f;
|
||||
private const float weightSaturation = 3f;
|
||||
private const float weightLuma = 5f;
|
||||
|
||||
private static float GetTargetLuma(ColorType colorType)
|
||||
{
|
||||
return 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)
|
||||
{
|
||||
return 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)
|
||||
{
|
||||
return 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)
|
||||
{
|
||||
return 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)
|
||||
{
|
||||
return 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)
|
||||
{
|
||||
return 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
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
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; }
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
namespace Artemis.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// The types of relevant colors in an image.
|
||||
/// </summary>
|
||||
public enum ColorType
|
||||
{
|
||||
/// <summary>
|
||||
/// Represents a saturated color.
|
||||
/// </summary>
|
||||
Vibrant,
|
||||
|
||||
/// <summary>
|
||||
/// Represents a saturated and light color.
|
||||
/// </summary>
|
||||
LightVibrant,
|
||||
|
||||
/// <summary>
|
||||
/// Represents a saturated and dark color.
|
||||
/// </summary>
|
||||
DarkVibrant,
|
||||
|
||||
/// <summary>
|
||||
/// Represents a desaturated color.
|
||||
/// </summary>
|
||||
Muted,
|
||||
|
||||
/// <summary>
|
||||
/// Represents a desaturated and light color.
|
||||
/// </summary>
|
||||
LightMuted,
|
||||
|
||||
/// <summary>
|
||||
/// Represents a desaturated and dark color.
|
||||
/// </summary>
|
||||
DarkMuted
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
using System.Collections.Generic;
|
||||
using SkiaSharp;
|
||||
|
||||
namespace Artemis.Core.Services;
|
||||
|
||||
/// <summary>
|
||||
/// A service providing a pallette of colors in a bitmap based on vibrant.js
|
||||
/// </summary>
|
||||
public interface IColorQuantizerService : IArtemisService
|
||||
{
|
||||
/// <summary>
|
||||
/// Reduces an <see cref="IEnumerable{SKColor}" /> to a given amount of relevant colors. Based on the Median Cut
|
||||
/// algorithm
|
||||
/// </summary>
|
||||
/// <param name="colors">The colors to quantize.</param>
|
||||
/// <param name="amount">The number of colors that should be calculated. Must be a power of two.</param>
|
||||
/// <returns>The quantized colors.</returns>
|
||||
public SKColor[] Quantize(IEnumerable<SKColor> colors, int amount);
|
||||
|
||||
/// <summary>
|
||||
/// Finds colors with certain characteristics in a given <see cref="IEnumerable{SKColor}" />.
|
||||
/// <para />
|
||||
/// Vibrant variants are more saturated, while Muted colors are less.
|
||||
/// <para />
|
||||
/// Light and Dark colors have higher and lower lightness values, respectively.
|
||||
/// </summary>
|
||||
/// <param name="colors">The colors to find the variations in</param>
|
||||
/// <param name="type">Which type of color to find</param>
|
||||
/// <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);
|
||||
}
|
||||
Reference in new issue
Block a user