static Rgba32 GetColorFromHexString(string hexString) { Rgba32 outColor = new Rgba32(); SixLabors.ImageSharp.PixelFormats.RgbaVector.FromHex(hexString).ToRgba32(ref outColor); return(outColor); }
protected virtual Bitmap FillBitmap(bool[,] sprite, Color background, Color foreground) { int side = sprite.GetLength(0); int imageSide = side * Options.Scale + Options.Border * 2; Bitmap img = new Bitmap(imageSide, imageSide); img.Mutate(context => { context.Fill(background); for (int y = 0; y < side; ++y) { for (int x = 0; x < side; ++x) { if (sprite[y, x]) { context.Fill(foreground, new RectangleF(Options.Border + x * Options.Scale, Options.Border + y * Options.Scale, Options.Scale, Options.Scale)); } } } }); if (imageSide != Options.Size) { img = img.Resize(Options.Size, Options.Size); } return(img); }
public static float GetPercentDifference(Bitmap imageA, Bitmap imageB, float perPixelThreshold = 0.05f) { if (imageA.Height != imageB.Height || imageA.Width != imageB.Width) { return(100.0f); } float totalPixels = imageA.Height * imageA.Width; float amountOfPixelDifference = 0.0f; for (int y = 0; y < imageA.Height; y++) { Span <PixelColor> aSpan = imageA.GetPixelRowSpan(y); Span <PixelColor> bSpan = imageB.GetPixelRowSpan(y); for (int x = 0; x < imageA.Width; x++) { PixelColor aPixel = aSpan[x]; PixelColor bPixel = bSpan[x]; float pixelDifference = GetPixelDifference(ref aPixel, ref bPixel); if (pixelDifference >= perPixelThreshold) { amountOfPixelDifference += pixelDifference; } } } return(amountOfPixelDifference / totalPixels); }
private static unsafe void CheckImageSharp <TPixel, TRefPixel>() where TPixel : unmanaged, Pixel.IConvertTo where TRefPixel : unmanaged, SixLabors.ImageSharp.PixelFormats.IPixel <TRefPixel> { Assert.AreEqual(sizeof(TRefPixel), sizeof(TPixel)); Span <byte> tmp = stackalloc byte[sizeof(TPixel)]; var pixVal = System.Runtime.InteropServices.MemoryMarshal.Cast <byte, TPixel>(tmp); var pixRef = System.Runtime.InteropServices.MemoryMarshal.Cast <byte, TRefPixel>(tmp); var cr = new SixLabors.ImageSharp.PixelFormats.Rgba32(255, 127, 63, 31); pixRef[0].FromRgba32(cr); var cv = pixVal[0].To <Pixel.BGRA32>(); var f = PixelFormat.TryIdentifyFormat <TPixel>(); TestContext.WriteLine($"{cv.R} {cv.G} {cv.B} {cv.A}"); Assert.Greater(cv.R, cv.G); Assert.Greater(cv.G, cv.B); if (f.HasUnpremulAlpha) { Assert.Greater(cv.B, cv.A); } }
protected virtual SvgBuilder FillSvg(bool[,] sprite, Color background, Color foreground) { int side = sprite.GetLength(0); decimal offset = Options.Border / (decimal)Options.Scale; decimal fullSide = side + 2m * offset; SvgBuilder svg = new SvgBuilder() .SetViewBox(0, 0, fullSide, fullSide) .Append(new SvgRect { PercentageWidth = 100, PercentageHeight = 100, Color = background }); SvgPathBuilder path = new SvgPathBuilder { Color = foreground }; for (int y = 0; y < side; ++y) { for (int x = 0; x < side; ++x) { if (sprite[y, x]) { path.AppendPoint(x + offset, y + offset) .AppendRelativeHorizontalLine(1) .AppendRelativeVerticalLine(1) .AppendRelativeHorizontalLine(-1) .AppendClosePath(); } } } return(svg.Append(path)); }
public static void SinglePixelComparison() { PixelColor white = Color.White; PixelColor transparent = Color.Transparent; Assert.Equal(1.0f, TestUtils.GetPixelDifference(ref white, ref transparent)); Assert.Equal(0.0f, TestUtils.GetPixelDifference(ref white, ref white)); }
public static unsafe Bitmap CreateRecordingFrame(Framebuffer fbuf, float startX, float startY, float drawWidth, float drawHeight) { GraphicsDevice gd = _controller !.GraphicsDevice; Texture ftex = fbuf.ColorTargets[0].Target; if (_recordingStager == null || _recordingStager.Width != ftex.Width || _recordingStager.Height != ftex.Height) { VeldridGraphBuffers.DoDispose(_recordingStager); _recordingStager = gd.ResourceFactory.CreateTexture(new TextureDescription(ftex.Width, ftex.Height, 1, 1, 1, PixelFormat.B8_G8_R8_A8_UNorm, TextureUsage.Staging, TextureType.Texture2D)); } _commandList !.Begin(); _commandList.CopyTexture(ftex, _recordingStager); _commandList.End(); gd.SubmitCommands(_commandList); gd.WaitForIdle(); if (drawWidth == -1 || drawHeight == -1) { drawHeight = _recordingStager.Height; drawWidth = _recordingStager.Width; } //draw it onto a bitmap Bitmap bmp = new Bitmap((int)drawWidth, (int)drawHeight, System.Drawing.Imaging.PixelFormat.Format32bppArgb); System.Drawing.Imaging.BitmapData data = bmp.LockBits(new System.Drawing.Rectangle(0, 0, (int)drawWidth, (int)drawHeight), System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format32bppArgb); byte *scan0 = (byte *)data.Scan0; MappedResourceView <SixLabors.ImageSharp.PixelFormats.Rgba32> res = gd.Map <SixLabors.ImageSharp.PixelFormats.Rgba32>(_recordingStager, MapMode.Read); for (int y = 0; y < drawHeight; y += 1) { for (int x = 0; x < drawWidth; x += 1) { int xPixel = (int)startX + x; int yPixel = (int)startY + y; SixLabors.ImageSharp.PixelFormats.Rgba32 px = res[xPixel, yPixel]; byte *ptr = scan0 + y * data.Stride + (x * 4); ptr[0] = px.R; ptr[1] = px.G; ptr[2] = px.B; ptr[3] = 255; } } bmp.UnlockBits(data); gd.Unmap(_recordingStager); return(bmp); }
public static IMAGE ToImageSharp(this Bitmap image) { var dst = new IMAGE(image.Width, image.Height); for (int y = 0; y < dst.Height; ++y) { for (int x = 0; x < dst.Width; ++x) { dst[x, y] = new SixLabors.ImageSharp.PixelFormats.Rgba32(image[x, y]); } } return(dst); }
static string GetClosestColorName(Rgba32 color) { double closestSquaredDistance = SquaredColorDistance(color, table[0].color); ColorData closestColor = table[0]; foreach (ColorData data in table) { double squaredDistance = SquaredColorDistance(color, data.color); if (squaredDistance < closestSquaredDistance) { closestSquaredDistance = squaredDistance; closestColor = data; } } return(closestColor.name); }
static int GetYear(Bitmap image, bool isOld, bool isBlackAndWhitePhoto, int?extractedYear) { if (extractedYear.HasValue) { return(extractedYear.Value); } int maxYear = 1917; int minYear = 500; if (isOld && isBlackAndWhitePhoto) { maxYear = 1900; minYear = 1830; } else if (isBlackAndWhitePhoto) { // Estimate of the timeline of black and white photos maxYear = 1930; minYear = 1830; } else if (isOld) { // Make a guess that old stuff isn't modern but also isn't ancient maxYear = 1900; minYear = 1000; } PixelColor pixelSampleColor = image[image.Width / 3, image.Height / 3]; float red = (float)pixelSampleColor.R / 255.0f; float green = (float)pixelSampleColor.G / 255.0f; float blue = (float)pixelSampleColor.B / 255.0f; float scale = (red + green + blue) / 3.0f; int year = (int)(minYear + (maxYear - minYear) * scale); return(year); }
public static Color FromHex(uint argb) => Color.FromArgb((int)argb);
public static Color FromRgb(double r, double g, double b) => Color.FromArgb((byte)r, (byte)g, (byte)b);
public static Color FromRgb(int r, int g, int b) => Color.FromArgb((byte)r, (byte)g, (byte)b);
public static Color FromRgb(byte r, byte g, byte b) => Color.FromArgb(r, g, b);
static double SquaredColorDistance(Rgba32 color1, Rgba32 color2) { return(System.Numerics.Vector4.DistanceSquared(color1.ToVector4(), color2.ToVector4())); }
public static float GetPixelDifference(ref PixelColor a, ref PixelColor b) { float pixelDistance = GetManhattanDistanceInRgbaSpace(ref a, ref b); return(pixelDistance / (255.0f * 4.0f)); }
public ColorData(Rgba32 inColor, string inName) { color = inColor; name = inName; }
private static int GetManhattanDistanceInRgbaSpace(ref PixelColor a, ref PixelColor b) { return(Diff(a.R, b.R) + Diff(a.G, b.G) + Diff(a.B, b.B) + Diff(a.A, b.A)); }
static int GetManhattanDistanceInRgbaSpace(ref ImageSharpRgba32 a, ref ImageSharpRgba32 b) {