//public static int ConvertColor(double opacity, ColorInt color) //{ // if (opacity < 0.0 || opacity > 1.0) // { // throw new ArgumentOutOfRangeException("opacity", "Opacity must be between 0.0 and 1.0"); // } // color.A = (byte)(color.A * opacity); // return ConvertColor(color); //} /// <summary> /// Fills the whole WriteableBitmap with a color. /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="color">The color used for filling.</param> public static void Clear(this BitmapBuffer bmp, ColorInt color) { int colr = color.ToPreMultAlphaColor(); using (var context = bmp.GetBitmapContext()) { int[] pixels = context.Pixels; int w = context.Width; int h = context.Height; int len = w * ARGB_SIZE; // Fill first line for (int x = 0; x < w; x++) { pixels[x] = colr; } // Copy first line int blockHeight = 1; int y = 1; while (y < h) { BitmapContext.BlockCopy(context, 0, context, y * len, blockHeight * len); y += blockHeight; blockHeight = Math.Min(2 * blockHeight, h - y); } } }
/// <summary> /// Sets the color of the pixel using an extra alpha value and a precalculated index (faster). /// For best performance this method should not be used in iterative real-time scenarios. Implement the code directly inside a loop. /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="index">The coordinate index.</param> /// <param name="a">The alpha value of the color.</param> /// <param name="color">The color.</param> public static void SetPixeli(this BitmapBuffer bmp, int index, byte a, ColorInt color) { using (var context = bmp.GetBitmapContext()) { context.Pixels[index] = color.ToPreMultAlphaColor(); } }
/// <summary> /// Sets the color of the pixel. /// For best performance this method should not be used in iterative real-time scenarios. Implement the code directly inside a loop. /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x">The x coordinate (row).</param> /// <param name="y">The y coordinate (column).</param> /// <param name="color">The color.</param> public static void SetPixel(this BitmapBuffer bmp, int x, int y, ColorInt color) { using (var context = bmp.GetBitmapContext()) { context.Pixels[y * context.Width + x] = color.ToPreMultAlphaColor(); } }
/// <summary> /// Sets the color of the pixel using an extra alpha value. /// For best performance this method should not be used in iterative real-time scenarios. Implement the code directly inside a loop. /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x">The x coordinate (row).</param> /// <param name="y">The y coordinate (column).</param> /// <param name="a">The alpha value of the color.</param> /// <param name="color">The color.</param> public static void SetPixel(this BitmapBuffer bmp, int x, int y, byte a, ColorInt color) { using (var context = bmp.GetBitmapContext()) { // Add one to use mul and cheap bit shift for multiplicaltion context.Pixels[y * context.Width + x] = color.ToPreMultAlphaColor(); } }
/// <summary> /// Draws a cubic Beziér spline defined by start, end and two control points. /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="x1">The x-coordinate of the start point.</param> /// <param name="y1">The y-coordinate of the start point.</param> /// <param name="cx1">The x-coordinate of the 1st control point.</param> /// <param name="cy1">The y-coordinate of the 1st control point.</param> /// <param name="cx2">The x-coordinate of the 2nd control point.</param> /// <param name="cy2">The y-coordinate of the 2nd control point.</param> /// <param name="x2">The x-coordinate of the end point.</param> /// <param name="y2">The y-coordinate of the end point.</param> /// <param name="color">The color.</param> public static void DrawBezier(this BitmapBuffer bmp, int x1, int y1, int cx1, int cy1, int cx2, int cy2, int x2, int y2, ColorInt color) { bmp.DrawBezier(x1, y1, cx1, cy1, cx2, cy2, x2, y2, color.ToPreMultAlphaColor()); }
/// <summary> /// Draws a closed Cardinal spline (cubic) defined by a point collection. /// The cardinal spline passes through each point in the collection. /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="points">The points for the curve in x and y pairs, therefore the array is interpreted as (x1, y1, x2, y2, x3, y3, x4, y4, x1, x2 ..., xn, yn).</param> /// <param name="tension">The tension of the curve defines the shape. Usually between 0 and 1. 0 would be a straight line.</param> /// <param name="color">The color for the spline.</param> public static void DrawCurveClosed(this BitmapBuffer bmp, int[] points, float tension, ColorInt color) { bmp.DrawCurveClosed(points, tension, color.ToPreMultAlphaColor()); }
/// <summary> /// Draws a series of cubic Beziér splines each defined by start, end and two control points. /// The ending point of the previous curve is used as starting point for the next. /// Therefore the initial curve needs four points and the subsequent 3 (2 control and 1 end point). /// </summary> /// <param name="bmp">The WriteableBitmap.</param> /// <param name="points">The points for the curve in x and y pairs, therefore the array is interpreted as (x1, y1, cx1, cy1, cx2, cy2, x2, y2, cx3, cx4 ..., xn, yn).</param> /// <param name="color">The color for the spline.</param> public static void DrawBeziers(this BitmapBuffer bmp, int[] points, ColorInt color) { bmp.DrawBeziers(points, color.ToPreMultAlphaColor()); }