本文整理汇总了C#中Canvas.SetColor方法的典型用法代码示例。如果您正苦于以下问题:C# Canvas.SetColor方法的具体用法?C# Canvas.SetColor怎么用?C# Canvas.SetColor使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Canvas
的用法示例。
在下文中一共展示了Canvas.SetColor方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Test_PatternMapGenerator_Generate
public void Test_PatternMapGenerator_Generate()
{
Canvas expectedPattern = new Canvas(new Resolution(3, 2));
#region FillingPattern
for (int y = 0; y < 2; y++)
for (int x = 0; x < 3; x++)
{
if (x == 2 && y == 0)
expectedPattern.SetColor(x, y, Color.Green);
else if (x == 1 && y == 1)
expectedPattern.SetColor(x, y, Color.Blue);
else
expectedPattern.SetColor(x, y, Color.Red);
}
#endregion
Canvas inputCanvas = new Canvas(new Resolution(6, 4));
#region FillingFillingExpectedCanvas
for (int y = 0; y < 4; y++)
for (int x = 0; x < 6; x++)
{
if ((x == 2 && y == 0) ||
(x == 4 && y == 0) ||
(x == 5 && y == 0) ||
(x == 5 && y == 1)
)
inputCanvas.SetColor(x, y, Color.Green);
else if ((x == 0 && y == 2) ||
(x == 1 && y == 2) ||
(x == 2 && y == 2) ||
(x == 3 && y == 2) ||
(x == 2 && y == 3)
)
inputCanvas.SetColor(x, y, Color.Blue);
else
inputCanvas.SetColor(x, y, Color.Red);
}
#endregion
Settings settings = new Settings();
settings.CellsCount = 3;
settings.Coefficient = 2;
settings.Palette = new Palette(new Color[]{Color.Red, Color.Green, Color.Blue});
PatternMapGenerator mapGenerator = new PatternMapGenerator();
Canvas actual = mapGenerator.Generate(inputCanvas, settings);
Assert.IsTrue(actual.Height == expectedPattern.Height && actual.Width == expectedPattern.Width);
for (int y = 0; y < expectedPattern.Height; y++)
for (int x = 0; x < expectedPattern.Width; x++)
Assert.IsTrue(actual.GetColor(x, y) == expectedPattern.GetColor(x, y));
}
示例2: ConvertBitmapToCanvas
public Canvas ConvertBitmapToCanvas(Bitmap image)
{
if (image == null) throw new NullReferenceException("'image' can't be null");
int width = image.Width;
int height = image.Height;
Canvas canvas = new Canvas(new Resolution(width, height));
LockBitmap lockBitmap = new LockBitmap(image);
lockBitmap.LockBits();
if (height * width > 6000)
{
Parallel.ForEach(Partitioner.Create(0, height), rangeHeight =>
{
for (int y = rangeHeight.Item1; y < rangeHeight.Item2; y++)
for (int x = 0; x < width; x++)
canvas.SetColor(x, y, lockBitmap.GetPixel(x, y));
});
}
else
{
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
canvas.SetColor(x, y, lockBitmap.GetPixel(x, y));
}
/*
Parallel.ForEach(Partitioner.Create(0, height), rangeHeight =>
{
for (int y = rangeHeight.Item1; y < rangeHeight.Item2; y++)
Parallel.ForEach(Partitioner.Create(0, width), rangeWidth =>
{
for (int x = 0; x < width; x++)
canvas.SetColor(x, y, lockBitmap.GetPixel(x, y));
}
});
*/
lockBitmap.UnlockBits();
#region Obsolete
/*
for (int y = 0; y < height; y++)
for (int x = 0; x < width; x++)
canvas.SetColor(x, y, image.GetPixel(x, y));
*/
#endregion
return canvas;
}
示例3: Test_CanvasConverter_ConvertBitmapToCanvas
public void Test_CanvasConverter_ConvertBitmapToCanvas()
{
Bitmap inputImage = new Bitmap(6, 4);
#region Filling input image
for(int y = 0; y < 4; y++)
for (int x = 0; x < 6; x++)
{
if ((x == 0 && y == 0) ||
(x == 5 && y == 1))
inputImage.SetPixel(x, y, Color.White);
else if ((x == 1 && y == 1) ||
(x == 5 && y == 3))
inputImage.SetPixel(x, y, Color.Blue);
else if ((x == 3 && y == 1) ||
(x == 2 && y == 2))
inputImage.SetPixel(x, y, Color.Green);
else inputImage.SetPixel(x, y, Color.Black);
}
#endregion
Canvas expectedCanvas = new Canvas(6, 4);
#region Filling expected canvas
for (int y = 0; y < 4; y++)
for (int x = 0; x < 6; x++)
{
if ((x == 0 && y == 0) ||
(x == 5 && y == 1))
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 255, 255, 255));
else if ((x == 1 && y == 1) ||
(x == 5 && y == 3))
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 0, 0, 255));
else if ((x == 3 && y == 1) ||
(x == 2 && y == 2))
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 0, 128, 0));
else expectedCanvas.SetColor(x, y, Color.FromArgb(255, 0, 0, 0));
}
#endregion
CanvasConverter converter = new CanvasConverter();
Canvas actual = converter.ConvertBitmapToCanvas(inputImage);
Assert.IsTrue(expectedCanvas.Width == actual.Width);
Assert.IsTrue(expectedCanvas.Height == actual.Height);
for (int y = 0; y < expectedCanvas.Height; y++)
for (int x = 0; x < expectedCanvas.Width; x++)
Assert.AreEqual(expectedCanvas.GetColor(x, y), actual.GetColor(x, y));
}
示例4: Test_Canvas_GetEnumerator
public void Test_Canvas_GetEnumerator()
{
Canvas canvas = new Canvas(new Resolution(4, 3));
#region Filling canvas
for (int y = 0; y < 3; y++)
for (int x = 0; x < 4; x++)
{
if (x == 1 && y == 1) canvas.SetColor(x, y, Color.Red);
else if (x == 2 && y == 0) canvas.SetColor(x, y, Color.White);
else canvas.SetColor(x, y, Color.Black);
}
#endregion
Color[] actualColors = new Color[4 * 3];
int i = 0;
foreach (Color color in canvas)
actualColors[i++] = color;
i = 0;
for (int y = 0; y < 3; y++)
for (int x = 0; x < 4; x++)
Assert.IsTrue(actualColors[i++] == canvas.GetColor(x, y));
}
示例5: Test_CanvasConverter_ConvertCanvasToBitmap
public void Test_CanvasConverter_ConvertCanvasToBitmap()
{
Canvas inputCanvas = new Canvas(7, 3);
#region Filling input canvas
for (int y = 0; y < 3; y++)
for (int x = 0; x < 7; x++)
{
if ((x == 0 && y == 0) ||
(x == 5 && y == 1))
inputCanvas.SetColor(x, y, Color.White);
else if ((x == 1 && y == 1) ||
(x == 6 && y == 2))
inputCanvas.SetColor(x, y, Color.Blue);
else if ((x == 3 && y == 1) ||
(x == 2 && y == 2))
inputCanvas.SetColor(x, y, Color.Green);
else inputCanvas.SetColor(x, y, Color.Black);
}
#endregion
Bitmap expectedImage = new Bitmap(7, 3);
#region Filling expected image
for (int y = 0; y < 3; y++)
for (int x = 0; x < 7; x++)
{
if ((x == 0 && y == 0) ||
(x == 5 && y == 1))
expectedImage.SetPixel(x, y, Color.FromArgb(255, 255, 255, 255));
else if ((x == 1 && y == 1) ||
(x == 6 && y == 2))
expectedImage.SetPixel(x, y, Color.FromArgb(255, 0, 0, 255));
else if ((x == 3 && y == 1) ||
(x == 2 && y == 2))
expectedImage.SetPixel(x, y, Color.FromArgb(255, 0, 128, 0));
else expectedImage.SetPixel(x, y, Color.FromArgb(255, 0, 0, 0));
}
#endregion
CanvasConverter converter = new CanvasConverter();
Bitmap actual = converter.ConvertCanvasToBitmap(inputCanvas);
Assert.AreEqual(expectedImage.Width, actual.Width);
Assert.AreEqual(expectedImage.Height, actual.Height);
for (int y = 0; y < expectedImage.Height; y++)
for (int x = 0; x < expectedImage.Width; x++)
Assert.AreEqual(expectedImage.GetPixel(x, y), actual.GetPixel(x, y));
}
示例6: Test_Canvas_SetInnerCanvasException
public void Test_Canvas_SetInnerCanvasException()
{
Canvas canvas = new Canvas(new Resolution(10, 8));
#region Filling canvas
for (int y = 0; y < 8; y++)
for (int x = 0; x < 10; x++)
canvas.SetColor(x, y, Color.Black);
#endregion
Canvas inputCanvas = new Canvas(new Resolution(7, 3));
#region Filling input canvas
for (int y = 0; y < 3; y++)
for (int x = 0; x < 7; x++)
{
if (x == 0 && y == 0) inputCanvas.SetColor(x, y, Color.Red);
else if (x == 3 && y == 2) inputCanvas.SetColor(x, y, Color.Green);
else inputCanvas.SetColor(x, y, Color.Black);
}
#endregion
canvas.SetCanvas(5, 2, inputCanvas);
}
示例7: Test_Canvas_SetInnerCanvas
public void Test_Canvas_SetInnerCanvas()
{
Canvas canvas = new Canvas(new Resolution(10, 8));
#region Filling canvas
for (int y = 0; y < 8; y++)
for (int x = 0; x < 10; x++)
canvas.SetColor(x, y, Color.Black);
#endregion
Canvas expected = new Canvas(new Resolution(10, 8));
#region Filling expected canvas
for (int y = 0; y < 8; y++)
for (int x = 0; x < 10; x++)
{
if (x == 5 && y == 2) expected.SetColor(x, y, Color.Red);
else if (x == 8 && y == 4) expected.SetColor(x, y, Color.Green);
else expected.SetColor(x, y, Color.Black);
}
#endregion
Canvas inputCanvas = new Canvas(new Resolution(4, 3));
#region Filling input canvas
for (int y = 0; y < 3; y++)
for (int x = 0; x < 4; x++)
{
if (x == 0 && y == 0) inputCanvas.SetColor(x, y, Color.Red);
else if (x == 3 && y == 2) inputCanvas.SetColor(x, y, Color.Green);
else inputCanvas.SetColor(x, y, Color.Black);
}
#endregion
canvas.SetCanvas(5, 2, inputCanvas);
Assert.IsTrue(canvas.Width == expected.Width && canvas.Height == expected.Height);
for (int y = 0; y < canvas.Height; y++)
for (int x = 0; x < canvas.Width; x++)
{
Color actualColor = canvas.GetColor(x, y);
Color expectedColor = expected.GetColor(x, y);
Assert.IsTrue(actualColor == expectedColor);
}
}
示例8: Test_Canvas_SetColor
public void Test_Canvas_SetColor()
{
Canvas canvas = new Canvas(4, 2);
canvas.SetColor(3, 0, Color.Red);
Assert.AreEqual(Color.Red, canvas.Color[0, 3]);
}
示例9: Test_Canvas_SetBorderTop
public void Test_Canvas_SetBorderTop()
{
Canvas canvas = new Canvas(4, 3);
Canvas expectedCanvas = new Canvas(4, 3);
expectedCanvas.SetColor(0, 0, Color.Black);
expectedCanvas.SetColor(1, 0, Color.Black);
expectedCanvas.SetColor(2, 0, Color.Black);
expectedCanvas.SetColor(3, 0, Color.Black);
canvas.SetBorder(0, 0, 4, 3, Color.Black, Aligns.Top, GridType.SolidLine);
Assert.IsTrue(canvas.Width == expectedCanvas.Width && canvas.Height == expectedCanvas.Height);
for (int y = 0; y < canvas.Height; y++)
for (int x = 0; x < canvas.Width; x++)
{
Color actualColor = canvas.GetColor(x, y);
Color expectedColor = expectedCanvas.GetColor(x, y);
Assert.IsTrue(actualColor == expectedColor);
}
}
示例10: Test_Canvas_SetBorderPointsLeft
public void Test_Canvas_SetBorderPointsLeft()
{
Canvas canvas = new Canvas(4, 4);
Canvas expectedCanvas = new Canvas(4, 4);
expectedCanvas.SetColor(0, 0, Color.Black);
expectedCanvas.SetColor(0, 3, Color.Black);
canvas.SetBorder(0, 0, 4, 4, Color.Black, Aligns.Left, GridType.Points);
Assert.IsTrue(canvas.Width == expectedCanvas.Width && canvas.Height == expectedCanvas.Height);
for (int y = 0; y < canvas.Height; y++)
for (int x = 0; x < canvas.Width; x++)
{
Color actualColor = canvas.GetColor(x, y);
Color expectedColor = expectedCanvas.GetColor(x, y);
Assert.IsTrue(actualColor == expectedColor);
}
}
示例11: Test_Canvas_ReduceResolution
public void Test_Canvas_ReduceResolution()
{
Canvas canvas = new Canvas(new Resolution(8, 5));
#region Filling canvas
for (int y = 0; y < 4; y++)
for (int x = 0; x < 6; x++)
{
if((x == 4 && y == 0) ||
(x == 5 && y == 0) ||
(x == 5 && y == 1))
canvas.SetColor(x, y, Color.Green);
else if ((x == 0 && y == 2) ||
(x == 1 && y == 2) ||
(x == 2 && y == 2) ||
(x == 3 && y == 2) ||
(x == 2 && y == 3))
canvas.SetColor(x, y, Color.Blue);
else canvas.SetColor(x, y, Color.Red);
}
#endregion
Canvas expectedCanvas = new Canvas(new Resolution(3, 2));
#region Filling expected canvas
for(int y = 0; y < 2; y++)
for (int x = 0; x < 3; x++)
{
if (x == 2 && y == 0)
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 63, 96, 0));
else if (x == 1 && y == 1)
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 63, 0, 191));
else if (x == 0 && y == 1)
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 127, 0, 127));
else
expectedCanvas.SetColor(x, y, Color.FromArgb(255, 255, 0, 0));
}
#endregion
Canvas actualCanvas = canvas.ReduceResolution(3, 2, 2);
Assert.IsTrue(actualCanvas.Width == expectedCanvas.Width && actualCanvas.Height == expectedCanvas.Height);
for (int y = 0; y < actualCanvas.Height; y++)
for (int x = 0; x < actualCanvas.Width; x++)
{
Color actualColor = actualCanvas.GetColor(x, y);
Color expectedColor = expectedCanvas.GetColor(x, y);
Assert.IsTrue(actualColor == expectedColor);
}
}
示例12: Test_Canvas_GetInnerCanvasException
public void Test_Canvas_GetInnerCanvasException()
{
Canvas canvas = new Canvas(new Resolution(10, 8));
#region Filling canvas
for (int y = 0; y < 8; y++)
for (int x = 0; x < 10; x++)
{
if (x == 5 && y == 2) canvas.SetColor(x, y, Color.Red);
else canvas.SetColor(x, y, Color.Black);
}
#endregion
Canvas actual = canvas.GetInnerCanvas(5, 2, new Resolution(10, 4));
}
示例13: Test_Canvas_GetInnerCanvas
public void Test_Canvas_GetInnerCanvas()
{
Canvas canvas = new Canvas(new Resolution(10, 8));
#region Filling canvas
for(int y = 0; y < 8; y++)
for (int x = 0; x < 10; x++)
{
if (x == 5 && y == 2) canvas.SetColor(x, y, Color.Red);
else canvas.SetColor(x, y, Color.Black);
}
#endregion
Canvas expectedCanvas = new Canvas(new Resolution(3, 4));
#region Filling canvas
for (int y = 0; y < 4; y++)
for (int x = 0; x < 3; x++)
{
if (x == 0 && y == 0) expectedCanvas.SetColor(x, y, Color.Red);
else expectedCanvas.SetColor(x, y, Color.Black);
}
#endregion
Canvas actual = canvas.GetInnerCanvas(5, 2, new Resolution(3, 4));
Assert.IsTrue(actual.Width == expectedCanvas.Width && actual.Height == expectedCanvas.Height);
for (int y = 0; y < actual.Height; y++)
for (int x = 0; x < actual.Width; x++)
Assert.IsTrue(actual.GetColor(x, y) == expectedCanvas.GetColor(x, y));
}
示例14: Decorate
public void Decorate(Canvas embroidery, Canvas pattern, Settings settings)
{
int squareWidth = embroidery.Width / settings.CellsCount;
if (embroidery.Height < pattern.Height * squareWidth)
throw new WrongResolutionException("Resolution.Height has to be higher");
/*Parallel.For with Partitioner*/
Parallel.ForEach(Partitioner.Create(0, pattern.Height), (rangeHeight) =>
{
for (int i = rangeHeight.Item1; i < rangeHeight.Item2; i++)
Parallel.ForEach(Partitioner.Create(0, pattern.Width), (rangeWidth) =>
{
for (int j = rangeWidth.Item1; j < rangeWidth.Item2; j++)
{
int startX = j * squareWidth;
int endX = startX + squareWidth;
int startY = i * squareWidth;
int endY = startY + squareWidth;
for (int y = startY; y < endY; y++)
for (int x = startX; x < endX; x++)
embroidery.SetColor(x, y, pattern.GetColor(j, i));
}
});
});
#region Just parallel.For
/*
Parallel.For(0, pattern.Height, i =>
{
Parallel.For(0, pattern.Width, j =>
{
int startX = j * squareWidth;
int endX = startX + squareWidth;
int startY = i * squareWidth;
int endY = startY + squareWidth;
for (int y = startY; y < endY; y++)
for (int x = startX; x < endX; x++)
embroidery.SetColor(x, y, pattern.GetColor(j, i));
});
});
*/
#endregion
#region Obsolete
/*
int newX = 0;
int newY = 0;
for (int i = 0; i < pattern.Height; i++)
{
for (int j = 0; j < pattern.Width; j++)
{
for (int y = newY; y < newY + squareWidth; y++)
for (int x = newX; x < newX + squareWidth; x++)
embroidery.SetColor(x, y, pattern.GetColor(j, i));
newX += squareWidth;
}
newY += squareWidth;
newX = 0;
}
*/
#endregion
}
示例15: Test_GridDecorator
public void Test_GridDecorator()
{
Settings settings = new Settings();
settings.CellsCount = 3;
settings.Coefficient = 2;
settings.Palette = new Palette(new Color[] { Color.Red, Color.Green, Color.Blue });
settings.GridType = GridType.SolidLine;
Canvas pattern = new Canvas(new Resolution(3, 2));
#region FillingPattern
for (int y = 0; y < 2; y++)
for (int x = 0; x < 3; x++)
{
if (x == 2 && y == 0)
pattern.SetColor(x, y, Color.Green);
else if (x == 1 && y == 1)
pattern.SetColor(x, y, Color.Blue);
else
pattern.SetColor(x, y, Color.Red);
}
#endregion
Canvas expectedCanvas = new Canvas(new Resolution(6, 4));
#region FillingExpectedCanvas
for (int y = 0; y < 4; y++)
for (int x = 0; x < 6; x++)
{
if ((x == 0 && y == 0) ||
(x == 0 && y == 2) ||
(x == 4 && y == 2) ||
(x == 2 && y == 0) ||
(x == 2 && y == 2) ||
(x == 4 && y == 0) )
expectedCanvas.SetColor(x, y, Color.Green);
else
expectedCanvas.SetColor(x, y, Color.Black);
}
#endregion
Canvas actual = new Canvas(new Resolution(6, 4));
#region Filling actual
for (int y = 0; y < 4; y++)
for (int x = 0; x < 6; x++)
actual.SetColor(x, y, Color.Green);
#endregion
GridDecorator decoratorGrid = new GridDecorator();
decoratorGrid.Decorate(actual, pattern, settings);
Assert.IsTrue(actual.Height == expectedCanvas.Height && actual.Width == expectedCanvas.Width);
for (int y = 0; y < expectedCanvas.Height; y++)
for (int x = 0; x < expectedCanvas.Width; x++)
{
Color actualColor = actual.GetColor(x, y);
Color expectedColor = expectedCanvas.GetColor(x, y);
Assert.IsTrue( actualColor == expectedColor);
}
}