mirror of
https://github.com/DarthAffe/HPPH.git
synced 2026-11-09 18:36:34 +00:00
Made colors equatable; added more tests; small improvements; changed test-data to cover more edge-cases
This commit is contained in:
1 parent
9999681574
commit
2a26c4404f
26 files changed
+378
-152
No files matched your search
@@ -6,8 +6,9 @@ public static partial class ReferencePixelHelper
|
||||
{
|
||||
#region Methods
|
||||
|
||||
public static IColor[] CreateColorPalette(IImage image, int paletteSize)
|
||||
=> CreateColorPalette(image.ToArray(), paletteSize);
|
||||
public static T[] CreateColorPalette<T>(Image<T> image, int paletteSize)
|
||||
where T : unmanaged, IColor
|
||||
=> CreateColorPalette<T>(image.ToArray(), paletteSize);
|
||||
|
||||
public static T[] CreateColorPalette<T>(RefImage<T> image, int paletteSize)
|
||||
where T : unmanaged, IColor
|
||||
@@ -15,17 +16,20 @@ public static partial class ReferencePixelHelper
|
||||
|
||||
public static T[] CreateColorPalette<T>(Span<T> colors, int paletteSize)
|
||||
where T : unmanaged, IColor =>
|
||||
CreateColorPalette(colors.ToArray().Cast<IColor>().ToArray(), paletteSize).Select(x => T.Create(x.R, x.G, x.B, x.A)).Cast<T>().ToArray();
|
||||
CreateColorPalette<T>(colors.ToArray().Cast<IColor>().ToArray(), paletteSize).Select(x => T.Create(x.R, x.G, x.B, x.A)).Cast<T>().ToArray();
|
||||
|
||||
private static IColor[] CreateColorPalette(IColor[] colors, int paletteSize)
|
||||
private static IColor[] CreateColorPalette<T>(IColor[] colors, int paletteSize)
|
||||
where T : struct, IColor
|
||||
{
|
||||
if (paletteSize == 0) return [];
|
||||
|
||||
int splits = BitOperations.Log2((uint)paletteSize);
|
||||
|
||||
List<ColorCube> cubes = [new ColorCube(colors)];
|
||||
HashSet<ColorCube> cubes = [new ColorCube(colors)];
|
||||
|
||||
for (int i = 0; i < splits; i++)
|
||||
{
|
||||
List<ColorCube> currentCubes = [.. cubes];
|
||||
HashSet<ColorCube> currentCubes = [.. cubes];
|
||||
foreach (ColorCube currentCube in currentCubes)
|
||||
{
|
||||
currentCube.Split(out ColorCube a, out ColorCube b);
|
||||
@@ -35,7 +39,7 @@ public static partial class ReferencePixelHelper
|
||||
}
|
||||
}
|
||||
|
||||
return cubes.Select(c => c.GetAverageColor()).ToArray();
|
||||
return cubes.Select(c => c.GetAverageColor<T>()).ToArray();
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -53,6 +57,12 @@ internal class ColorCube
|
||||
|
||||
internal ColorCube(IList<IColor> colors)
|
||||
{
|
||||
if (colors.Count == 0)
|
||||
{
|
||||
_colors = [];
|
||||
return;
|
||||
}
|
||||
|
||||
int redRange = colors.Max(c => c.R) - colors.Min(c => c.R);
|
||||
int greenRange = colors.Max(c => c.G) - colors.Min(c => c.G);
|
||||
int blueRange = colors.Max(c => c.B) - colors.Min(c => c.B);
|
||||
@@ -77,17 +87,20 @@ internal class ColorCube
|
||||
b = new ColorCube(_colors.GetRange(median, _colors.Count - median));
|
||||
}
|
||||
|
||||
internal IColor GetAverageColor()
|
||||
internal IColor GetAverageColor<T>()
|
||||
where T : struct, IColor
|
||||
{
|
||||
if (_colors.Count == 0) return T.Create(0, 0, 0, 0);
|
||||
|
||||
int r = _colors.Sum(x => x.R);
|
||||
int g = _colors.Sum(x => x.G);
|
||||
int b = _colors.Sum(x => x.B);
|
||||
int a = _colors.Sum(x => x.A);
|
||||
|
||||
return new ColorRGBA((byte)(r / _colors.Count),
|
||||
(byte)(g / _colors.Count),
|
||||
(byte)(b / _colors.Count),
|
||||
(byte)(a / _colors.Count));
|
||||
return T.Create((byte)(r / _colors.Count),
|
||||
(byte)(g / _colors.Count),
|
||||
(byte)(b / _colors.Count),
|
||||
(byte)(a / _colors.Count));
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
Reference in new issue
Block a user