/// <summary> /// Swap the colors in an image based on a color mapping provided by the ColorCallback</summary> /// <param name="imageSource">Original image</param> /// <param name="colorCallback">Callback to provide the color mapping</param> /// <returns>Color-swapped image</returns> public static ImageSource SwapColors(ImageSource imageSource, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } ImageSource source = imageSource; if (imageSource == null) { return(source); } DrawingImage image = imageSource as DrawingImage; if (image != null) { source = image = image.Clone(); SwapColorsWithoutCloning(image.Drawing, colorCallback); source.Freeze(); return(source); } BitmapSource bitmapSource = imageSource as BitmapSource; if (bitmapSource == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "UnexpectedImageSourceType", new object[] { imageSource.GetType().Name })); } return(SwapColors(bitmapSource, colorCallback)); }
public static ImageSource SwapColors(ImageSource imageSource, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } ImageSource imageSource1 = imageSource; if (imageSource != null) { DrawingImage drawingImage; if ((drawingImage = imageSource as DrawingImage) != null) { ColorSwapper.SwapColorsWithoutCloning(((DrawingImage)(imageSource1 = (ImageSource)drawingImage.Clone())).Drawing, colorCallback); imageSource1.Freeze(); } else { BitmapSource bitmapSource; if ((bitmapSource = imageSource as BitmapSource) != null) { imageSource1 = (ImageSource)ColorSwapper.SwapColors(bitmapSource, colorCallback); } else { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, ExceptionStringTable.UnexpectedImageSourceType, new object[1] { (object)imageSource.GetType().Name })); } } } return(imageSource1); }
private static void SwapColorsWithoutCloning(Drawing drawing, ColorCallback colorCallback) { if (drawing == null) { return; } DrawingGroup drawingGroup; if ((drawingGroup = drawing as DrawingGroup) != null) { for (int index = 0; index < drawingGroup.Children.Count; ++index) { ColorSwapper.SwapColorsWithoutCloning(drawingGroup.Children[index], colorCallback); } } else { GeometryDrawing geometryDrawing; if ((geometryDrawing = drawing as GeometryDrawing) != null) { ColorSwapper.SwapColorsWithoutCloning(geometryDrawing.Brush, colorCallback); if (geometryDrawing.Pen == null) { return; } ColorSwapper.SwapColorsWithoutCloning(geometryDrawing.Pen.Brush, colorCallback); } else { GlyphRunDrawing glyphRunDrawing; if ((glyphRunDrawing = drawing as GlyphRunDrawing) != null) { ColorSwapper.SwapColorsWithoutCloning(glyphRunDrawing.ForegroundBrush, colorCallback); } else { ImageDrawing imageDrawing; if ((imageDrawing = drawing as ImageDrawing) != null) { imageDrawing.ImageSource = ColorSwapper.SwapColorsWithoutCloningIfPossible(imageDrawing.ImageSource, colorCallback); } else if (!(drawing is VideoDrawing)) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, ExceptionStringTable.UnexpectedDrawingType, new object[1] { (object)drawing.GetType().Name })); } } } } }
/// <summary> /// Swap the color of a brush based on a color mapping provided by the ColorCallback</summary> /// <param name="brush">Original brush</param> /// <param name="colorCallback">Callback to provide the color mapping</param> /// <returns>Color-swapped brush</returns> public static Brush SwapColors(Brush brush, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } Brush brush2 = brush; if (brush != null) { brush2 = brush.Clone(); SwapColorsWithoutCloning(brush2, colorCallback); brush2.Freeze(); } return(brush2); }
/// <summary> /// Swap the colors in a drawing based on a color mapping provided by the ColorCallback</summary> /// <param name="drawing">Original drawing</param> /// <param name="colorCallback">Callback to provide the color mapping</param> /// <returns>Color-swapped drawing</returns> public static Drawing SwapColors(Drawing drawing, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } Drawing drawing2 = drawing; if (drawing != null) { drawing2 = drawing.Clone(); SwapColorsWithoutCloning(drawing2, colorCallback); drawing2.Freeze(); } return(drawing2); }
private static void SwapColorsWithoutCloning(Brush brush, ColorCallback colorCallback) { if (brush == null) { return; } SolidColorBrush solidColorBrush; if ((solidColorBrush = brush as SolidColorBrush) != null) { solidColorBrush.Color = colorCallback(solidColorBrush.Color); } else { GradientBrush gradientBrush; if ((gradientBrush = brush as GradientBrush) != null) { foreach (GradientStop gradientStop in gradientBrush.GradientStops) { gradientStop.Color = colorCallback(gradientStop.Color); } } else { DrawingBrush drawingBrush; if ((drawingBrush = brush as DrawingBrush) != null) { ColorSwapper.SwapColorsWithoutCloning(drawingBrush.Drawing, colorCallback); } else { ImageBrush imageBrush; if ((imageBrush = brush as ImageBrush) != null) { imageBrush.ImageSource = ColorSwapper.SwapColorsWithoutCloningIfPossible(imageBrush.ImageSource, colorCallback); } else if (!(brush is VisualBrush)) { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, ExceptionStringTable.UnexpectedBrushType, new object[1] { (object)brush.GetType().Name })); } } } } }
private static void SwapColorsWithoutCloning(Drawing drawing, ColorCallback colorCallback) { if (drawing != null) { DrawingGroup group = drawing as DrawingGroup; if (group != null) { for (int i = 0; i < group.Children.Count; i++) { SwapColorsWithoutCloning(group.Children[i], colorCallback); } } else { GeometryDrawing drawing2 = drawing as GeometryDrawing; if (drawing2 != null) { SwapColorsWithoutCloning(drawing2.Brush, colorCallback); if (drawing2.Pen != null) { SwapColorsWithoutCloning(drawing2.Pen.Brush, colorCallback); } } else { GlyphRunDrawing drawing3 = drawing as GlyphRunDrawing; if (drawing3 != null) { SwapColorsWithoutCloning(drawing3.ForegroundBrush, colorCallback); } else { ImageDrawing drawing4 = drawing as ImageDrawing; if (drawing4 != null) { drawing4.ImageSource = SwapColorsWithoutCloningIfPossible(drawing4.ImageSource, colorCallback); } else if (!(drawing is VideoDrawing)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "UnexpectedDrawingType", new object[] { drawing.GetType().Name })); } } } } } }
private static void SwapColorsWithoutCloning(Brush brush, ColorCallback colorCallback) { if (brush != null) { SolidColorBrush brush2 = brush as SolidColorBrush; if (brush2 != null) { brush2.Color = colorCallback(brush2.Color); } else { GradientBrush brush3 = brush as GradientBrush; if (brush3 == null) { DrawingBrush brush4 = brush as DrawingBrush; if (brush4 != null) { SwapColorsWithoutCloning(brush4.Drawing, colorCallback); } else { ImageBrush brush5 = brush as ImageBrush; if (brush5 != null) { brush5.ImageSource = SwapColorsWithoutCloningIfPossible(brush5.ImageSource, colorCallback); } else if (!(brush is VisualBrush)) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "UnexpectedBrushType", new object[] { brush.GetType().Name })); } } } else { foreach (GradientStop stop in brush3.GradientStops) { stop.Color = colorCallback(stop.Color); } } } } }
public static BitmapSource SwapColors(BitmapSource bitmapSource, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } BitmapSource bitmapSource1 = bitmapSource; if (bitmapSource != null) { PixelFormat bgra32 = PixelFormats.Bgra32; BitmapPalette bitmapPalette = (BitmapPalette)null; double alphaThreshold = 0.0; FormatConvertedBitmap formatConvertedBitmap = new FormatConvertedBitmap(bitmapSource, bgra32, bitmapPalette, alphaThreshold); int pixelWidth = formatConvertedBitmap.PixelWidth; int pixelHeight = formatConvertedBitmap.PixelHeight; int stride = 4 * pixelWidth; byte[] numArray = new byte[stride * pixelHeight]; formatConvertedBitmap.CopyPixels((Array)numArray, stride, 0); int index = 0; while (index < numArray.Length) { Color color1 = Color.FromArgb(numArray[index + 3], numArray[index + 2], numArray[index + 1], numArray[index]); Color color2 = colorCallback(color1); if (color2 != color1) { numArray[index] = color2.B; numArray[index + 1] = color2.G; numArray[index + 2] = color2.R; numArray[index + 3] = color2.A; } index += 4; } bitmapSource1 = BitmapSource.Create(pixelWidth, pixelHeight, formatConvertedBitmap.DpiX, formatConvertedBitmap.DpiY, bgra32, bitmapPalette, (Array)numArray, stride); bitmapSource1.Freeze(); } return(bitmapSource1); }
/// <summary> /// Swap the colors in a bitmap based on a color mapping provided by the ColorCallback</summary> /// <param name="bitmapSource">Original bitmap</param> /// <param name="colorCallback">Callback to provide the color mapping</param> /// <returns>Color-swapped bitmap</returns> public static BitmapSource SwapColors(BitmapSource bitmapSource, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } BitmapSource source = bitmapSource; if (bitmapSource != null) { PixelFormat destinationFormat = PixelFormats.Bgra32; BitmapPalette destinationPalette = null; double alphaThreshold = 0.0; FormatConvertedBitmap bitmap = new FormatConvertedBitmap(bitmapSource, destinationFormat, destinationPalette, alphaThreshold); int pixelWidth = bitmap.PixelWidth; int pixelHeight = bitmap.PixelHeight; int stride = 4 * pixelWidth; byte[] pixels = new byte[stride * pixelHeight]; bitmap.CopyPixels(pixels, stride, 0); for (int i = 0; i < pixels.Length; i += 4) { Color color = Color.FromArgb(pixels[i + 3], pixels[i + 2], pixels[i + 1], pixels[i]); Color color2 = colorCallback(color); if (color2 != color) { pixels[i] = color2.B; pixels[i + 1] = color2.G; pixels[i + 2] = color2.R; pixels[i + 3] = color2.A; } } source = BitmapSource.Create(pixelWidth, pixelHeight, bitmap.DpiX, bitmap.DpiY, destinationFormat, destinationPalette, pixels, stride); source.Freeze(); } return(source); }
/// <summary> /// Swap the colors in an image based on a color mapping provided by the ColorCallback</summary> /// <param name="imageSource">Original image</param> /// <param name="colorCallback">Callback to provide the color mapping</param> /// <returns>Color-swapped image</returns> public static ImageSource SwapColors(ImageSource imageSource, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } ImageSource source = imageSource; if (imageSource == null) { return source; } DrawingImage image = imageSource as DrawingImage; if (image != null) { source = image = image.Clone(); SwapColorsWithoutCloning(image.Drawing, colorCallback); source.Freeze(); return source; } BitmapSource bitmapSource = imageSource as BitmapSource; if (bitmapSource == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "UnexpectedImageSourceType", new object[] { imageSource.GetType().Name })); } return SwapColors(bitmapSource, colorCallback); }
/// <summary> /// Swap the colors in a drawing based on a color mapping provided by the ColorCallback</summary> /// <param name="drawing">Original drawing</param> /// <param name="colorCallback">Callback to provide the color mapping</param> /// <returns>Color-swapped drawing</returns> public static Drawing SwapColors(Drawing drawing, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } Drawing drawing2 = drawing; if (drawing != null) { drawing2 = drawing.Clone(); SwapColorsWithoutCloning(drawing2, colorCallback); drawing2.Freeze(); } return drawing2; }
private static ImageSource SwapColorsWithoutCloningIfPossible(ImageSource imageSource, ColorCallback colorCallback) { ImageSource imageSource1 = imageSource; if (imageSource != null) { DrawingImage drawingImage; if ((drawingImage = imageSource as DrawingImage) != null) { ColorSwapper.SwapColorsWithoutCloning(drawingImage.Drawing, colorCallback); } else { BitmapSource bitmapSource; if ((bitmapSource = imageSource as BitmapSource) != null) { imageSource1 = (ImageSource)ColorSwapper.SwapColors(bitmapSource, colorCallback); } else { throw new ArgumentException(string.Format((IFormatProvider)CultureInfo.CurrentCulture, ExceptionStringTable.UnexpectedImageSourceType, new object[1] { (object)imageSource.GetType().Name })); } } } return(imageSource1); }
private static ImageSource SwapColorsWithoutCloningIfPossible(ImageSource imageSource, ColorCallback colorCallback) { ImageSource source = imageSource; if (imageSource == null) { return source; } DrawingImage image = imageSource as DrawingImage; if (image != null) { SwapColorsWithoutCloning(image.Drawing, colorCallback); return source; } BitmapSource bitmapSource = imageSource as BitmapSource; if (bitmapSource == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "UnexpectedImageSourceType", new object[] { imageSource.GetType().Name })); } return SwapColors(bitmapSource, colorCallback); }
/// <summary> /// Swap the colors in a bitmap based on a color mapping provided by the ColorCallback</summary> /// <param name="bitmapSource">Original bitmap</param> /// <param name="colorCallback">Callback to provide the color mapping</param> /// <returns>Color-swapped bitmap</returns> public static BitmapSource SwapColors(BitmapSource bitmapSource, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } BitmapSource source = bitmapSource; if (bitmapSource != null) { PixelFormat destinationFormat = PixelFormats.Bgra32; BitmapPalette destinationPalette = null; double alphaThreshold = 0.0; FormatConvertedBitmap bitmap = new FormatConvertedBitmap(bitmapSource, destinationFormat, destinationPalette, alphaThreshold); int pixelWidth = bitmap.PixelWidth; int pixelHeight = bitmap.PixelHeight; int stride = 4 * pixelWidth; byte[] pixels = new byte[stride * pixelHeight]; bitmap.CopyPixels(pixels, stride, 0); for (int i = 0; i < pixels.Length; i += 4) { Color color = Color.FromArgb(pixels[i + 3], pixels[i + 2], pixels[i + 1], pixels[i]); Color color2 = colorCallback(color); if (color2 != color) { pixels[i] = color2.B; pixels[i + 1] = color2.G; pixels[i + 2] = color2.R; pixels[i + 3] = color2.A; } } source = BitmapSource.Create(pixelWidth, pixelHeight, bitmap.DpiX, bitmap.DpiY, destinationFormat, destinationPalette, pixels, stride); source.Freeze(); } return source; }
private static ImageSource SwapColorsWithoutCloningIfPossible(ImageSource imageSource, ColorCallback colorCallback) { ImageSource source = imageSource; if (imageSource == null) { return(source); } DrawingImage image = imageSource as DrawingImage; if (image != null) { SwapColorsWithoutCloning(image.Drawing, colorCallback); return(source); } BitmapSource bitmapSource = imageSource as BitmapSource; if (bitmapSource == null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "UnexpectedImageSourceType", new object[] { imageSource.GetType().Name })); } return(SwapColors(bitmapSource, colorCallback)); }
/// <summary> /// Swap the color of a brush based on a color mapping provided by the ColorCallback</summary> /// <param name="brush">Original brush</param> /// <param name="colorCallback">Callback to provide the color mapping</param> /// <returns>Color-swapped brush</returns> public static Brush SwapColors(Brush brush, ColorCallback colorCallback) { if (colorCallback == null) { throw new ArgumentNullException("colorCallback"); } Brush brush2 = brush; if (brush != null) { brush2 = brush.Clone(); SwapColorsWithoutCloning(brush2, colorCallback); brush2.Freeze(); } return brush2; }