using ArcaneLibs.Extensions;
using ImageMagick;
using Microsoft.AspNetCore.Mvc.ModelBinding.Binders;
namespace Spacebar.AdminApi.TestClient.Services.Helpers;
public class DefaultAvatarRenderer {
public static readonly (byte r, byte g, byte b)[] DefaultAvatarColors = [
(70, 73, 236),
(150, 150, 150),
(236, 52, 83),
(211, 52, 236),
(45, 202, 76),
(236, 103, 52)
];
public static readonly (byte r, byte g, byte b) SpacebarLogoColor = (0x01, 0x85, 0xFF);
// Slower at runtime, but doesnt depend on filesystem
private static string GetDefaultAvatarSvg(byte r, byte g, byte b, byte rf = 0xff, byte gf = 0xff, byte bf = 0xff) {
return $"""
""";
}
public static async Task GetDefaultAvatarImage(byte r, byte g, byte b, byte rf = 0xff, byte gf = 0xff, byte bf = 0xff, int size = 4096) {
var img = new MagickImageCollection(GetDefaultAvatarSvg(r, g, b, rf, gf, bf).AsBytes().ToArray(), new MagickReadSettings() {
Width = (uint?)size,
Height = (uint?)size,
// Verbose = true,
// ColorSpace = ColorSpace.RGB
});
return img;
}
public static async Task GetDefaultAvatar(byte r, byte g, byte b, byte rf = 0xff, byte gf = 0xff, byte bf = 0xff, MagickFormat format = MagickFormat.Png,
int size = 4096) {
var img = await GetDefaultAvatarImage(r, g, b, rf, gf, bf, size);
var ms = new MemoryStream();
await img.WriteAsync(ms, format);
ms.Position = 0;
return ms;
}
}