Skip to content

Commit

Permalink
Merge branch 'development'
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBeekman committed Nov 14, 2024
2 parents 972f1c6 + 25786c6 commit 4c6ca9b
Show file tree
Hide file tree
Showing 75 changed files with 470 additions and 2,021 deletions.
3 changes: 2 additions & 1 deletion src/Artemis.Core/Artemis.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
<PackageReference Include="DryIoc.dll" />
<PackageReference Include="EmbedIO" />
<PackageReference Include="HidSharp" />
<PackageReference Include="HPPH.SkiaSharp" />
<PackageReference Include="Humanizer.Core" />
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All"/>
<PackageReference Include="JetBrains.Annotations" PrivateAssets="All" />
<PackageReference Include="McMaster.NETCore.Plugins" />
<PackageReference Include="RGB.NET.Core" />
<PackageReference Include="RGB.NET.Layout" />
Expand Down
167 changes: 0 additions & 167 deletions src/Artemis.Core/ColorScience/Quantization/ColorCube.cs

This file was deleted.

47 changes: 22 additions & 25 deletions src/Artemis.Core/ColorScience/Quantization/ColorQuantizer.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
using SkiaSharp;
using HPPH;
using SkiaSharp;
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.InteropServices;

namespace Artemis.Core.ColorScience;

Expand All @@ -10,52 +12,47 @@ namespace Artemis.Core.ColorScience;
/// </summary>
public static class ColorQuantizer
{
/// <inheritdoc cref="Quantize(Span{SKColor}, int)"/>
[Obsolete("Use Quantize(Span<SKColor> colors, int amount) in-parameter instead")]
public static SKColor[] Quantize(in Span<SKColor> colors, int amount)
{
return Quantize(colors, amount);
}

/// <inheritdoc cref="QuantizeSplit(Span{SKColor}, int)"/>
[Obsolete("Use QuantizeSplit(Span<SKColor> colors, int splits) without the in-parameter instead")]
public static SKColor[] QuantizeSplit(in Span<SKColor> colors, int splits)
{
return QuantizeSplit(colors, splits);
}

/// <summary>
/// Quantizes a span of colors into the desired amount of representative colors.
/// </summary>
/// <param name="colors">The colors to quantize</param>
/// <param name="amount">How many colors to return. Must be a power of two.</param>
/// <returns><paramref name="amount"/> colors.</returns>
public static SKColor[] Quantize(in Span<SKColor> colors, int amount)
public static SKColor[] Quantize(Span<SKColor> colors, int amount)
{
if (!BitOperations.IsPow2(amount))
throw new ArgumentException("Must be power of two", nameof(amount));

int splits = BitOperations.Log2((uint)amount);
return QuantizeSplit(colors, splits);
}

/// <summary>
/// Quantizes a span of colors, splitting the average <paramref name="splits"/> number of times.
/// </summary>
/// <param name="colors">The colors to quantize</param>
/// <param name="splits">How many splits to execute. Each split doubles the number of colors returned.</param>
/// <returns>Up to (2 ^ <paramref name="splits"/>) number of colors.</returns>
public static SKColor[] QuantizeSplit(in Span<SKColor> colors, int splits)
public static SKColor[] QuantizeSplit(Span<SKColor> colors, int splits)
{
if (colors.Length < (1 << splits)) throw new ArgumentException($"The color array must at least contain ({(1 << splits)}) to perform {splits} splits.");

Span<ColorCube> cubes = new ColorCube[1 << splits];
cubes[0] = new ColorCube(colors, 0, colors.Length, SortTarget.None);

int currentIndex = 0;
for (int i = 0; i < splits; i++)
{
int currentCubeCount = 1 << i;
Span<ColorCube> currentCubes = cubes.Slice(0, currentCubeCount);
for (int j = 0; j < currentCubes.Length; j++)
{
currentCubes[j].Split(colors, out ColorCube a, out ColorCube b);
currentCubes[j] = a;
cubes[++currentIndex] = b;
}
}

SKColor[] result = new SKColor[cubes.Length];
for (int i = 0; i < cubes.Length; i++)
result[i] = cubes[i].GetAverageColor(colors);

return result;
// DarthAffe 22.07.2024: This is not ideal as it allocates an additional array, but i don't see a way to get SKColors out here
return MemoryMarshal.Cast<ColorBGRA, SKColor>(MemoryMarshal.Cast<SKColor, ColorBGRA>(colors).CreateSimpleColorPalette(1 << splits)).ToArray();
}

/// <summary>
Expand Down
121 changes: 0 additions & 121 deletions src/Artemis.Core/ColorScience/Quantization/QuantizerSort.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/Artemis.Core/ColorScience/Quantization/SortTarget.cs

This file was deleted.

Loading

0 comments on commit 4c6ca9b

Please sign in to comment.