public static StandardRgbBitmap ToStandardRgbBitmap(this BitmapSource bitmap) { BitmapSource convertedSource = bitmap; if (bitmap.Format != PixelFormats.Bgra32) { convertedSource = new FormatConvertedBitmap(bitmap, PixelFormats.Bgra32, null, 1.0); } int stride = convertedSource.PixelWidth * sizeof(int); byte[] rawPixels = new byte[stride * convertedSource.PixelHeight]; convertedSource.CopyPixels(rawPixels, stride, offset: 0); StandardRgbColor[] pixels = new StandardRgbColor[convertedSource.PixelWidth * convertedSource.PixelHeight]; for (int i = 0; i < rawPixels.Length; i += 4) { int target = i / 4; pixels[target].B = rawPixels[i]; pixels[target].G = rawPixels[i + 1]; pixels[target].R = rawPixels[i + 2]; } return(new StandardRgbBitmap(pixels, bitmap.PixelWidth, bitmap.PixelHeight, bitmap.DpiX, bitmap.DpiY)); }
public Task <StandardRgbColor[]> RenderAsync() { return(Task.Run(() => { StandardRgbColor[] bitmap = new StandardRgbColor[pixelClusters.Length]; Parallel.For(0, pixelClusters.Length, i => { bitmap[i] = colorSpace.ConvertToStandardRgb(clusterMeans[pixelClusters[i]]); }); return bitmap; })); }
public static bool TryParse(string raw, out StandardRgbColor color) { if (ColorConverter.ConvertFromString(raw) is Color c) { color.R = c.R; color.G = c.G; color.B = c.B; return(true); } else { color = default(StandardRgbColor); return(false); } }
public static LinearRgbColor ToLinearRgb(this StandardRgbColor source) { float sR = source.R / 255.0f; float sG = source.G / 255.0f; float sB = source.B / 255.0f; return(new LinearRgbColor { R = convertGammaToLinear(sR), G = convertGammaToLinear(sG), B = convertGammaToLinear(sB) }); float convertGammaToLinear(float u) { return(u <= 0.04045f ? u / 12.92f : (float)Math.Pow((u + 0.055) / 1.055, 2.4)); } }
public Vector3 ConvertFromStandardRgb(StandardRgbColor pixel) { return((Vector3)pixel.ToCieLuv()); }
public static Color ToWindowsColor(this StandardRgbColor pixel) { return(Color.FromRgb(pixel.R, pixel.G, pixel.B)); }
public static CieLuvColor ToCieLuv(this StandardRgbColor source) { return(source.ToLinearRgb().ToCieLuv()); }
public WeightedColor(int pixelCount, StandardRgbColor color) { PixelCount = pixelCount; Color = color; }