using OwlCore.WinUI.ColorExtractor.ColorSpaces; using SixLabors.ImageSharp; using SixLabors.ImageSharp.PixelFormats; using System; using System.IO; using System.Net; using System.Threading.Tasks; namespace OwlCore.WinUI.ColorExtractor { /// /// A class of methods for working with images. /// public static class ImageParser { /// /// Gets an with format from a url. /// /// The url of the image to load. /// An Argb32 image. public static async Task?> GetImage(string url) { if (string.IsNullOrEmpty(url)) return null; using var imageStreamAsync = await GetImageStreamAsync(url); var image = await Image.LoadAsync(imageStreamAsync); var clonedImage = image.CloneAs(); return clonedImage; } /// /// Gets colors out of an image. /// /// The image to get colors from. /// The approximate amount of pixels to get/ /// An array of colors that appeared as pixels in the image. public static RGBColor[] GetImageColors(Image image, int quality = 1920) { var nth = image.Width * image.Height / quality; var pixelsPerRow = image.Width / nth; var colors = new RGBColor[image.Height * pixelsPerRow]; var pos = 0; for (var row = 0; row < image.Height; row++) { var rowPixels = image.GetPixelRowSpan(row); for (var i = 0; i < pixelsPerRow; i++) { var b = rowPixels[i * nth].B / 255f; var g = rowPixels[i * nth].G / 255f; var r = rowPixels[i * nth].R / 255f; //float a = rowPixels[i].A / 255; var color = new RGBColor(r, g, b); colors[pos] = color; pos++; } } return colors; } private static async Task GetImageStreamAsync(string url) { var uri = new Uri(url); if (uri.IsFile) return File.Open(uri.LocalPath, FileMode.Open, FileAccess.Read); var response = await WebRequest.CreateHttp(url).GetResponseAsync(); return response.GetResponseStream(); } } }