/// <summary> /// Traverse the tree, building up the color palette /// </summary> /// <param name="palette">The palette</param> /// <param name="index">The current palette index</param> public void ConstructPalette(List <TColor> palette, ref int index) { if (this.leaf) { // Consume the next palette index this.paletteIndex = index++; byte r = (this.red / this.pixelCount).ToByte(); byte g = (this.green / this.pixelCount).ToByte(); byte b = (this.blue / this.pixelCount).ToByte(); byte a = (this.alpha / this.pixelCount).ToByte(); // And set the color of the palette entry TColor pixel = default(TColor); pixel.PackFromVector4(new Color(r, g, b, a).ToVector4()); palette.Add(pixel); } else { // Loop through children looking for leaves for (int i = 0; i < 8; i++) { if (this.children[i] != null) { this.children[i].ConstructPalette(palette, ref index); } } } }
/// <summary> /// Fills the bottom left quadrent with 3 horizental bars in Red, Green and Blue with a alpha gradient from left (transparent) to right (solid). /// </summary> /// <param name="pixels"></param> private static void TransparentGradients(PixelAccessor <TColor> pixels) { // topLeft int left = 0; int right = pixels.Width / 2; int top = pixels.Height / 2; int bottom = pixels.Height; int height = (int)Math.Ceiling(pixels.Height / 6f); Vector4 red = Color.Red.ToVector4(); // use real color so we can see har it translates in the test pattern Vector4 green = Color.Green.ToVector4(); // use real color so we can see har it translates in the test pattern Vector4 blue = Color.Blue.ToVector4(); // use real color so we can see har it translates in the test pattern TColor c = default(TColor); for (int x = left; x < right; x++) { blue.W = red.W = green.W = (float)x / (float)right; c.PackFromVector4(red); int topBand = top; for (int y = topBand; y < top + height; y++) { pixels[x, y] = c; } topBand = topBand + height; c.PackFromVector4(green); for (int y = topBand; y < topBand + height; y++) { pixels[x, y] = c; } topBand = topBand + height; c.PackFromVector4(blue); for (int y = topBand; y < bottom; y++) { pixels[x, y] = c; } } }
public static Image <TColor, TPacked> CreateTestImage <TColor, TPacked>() where TColor : struct, IPackedPixel <TPacked> where TPacked : struct, IEquatable <TPacked> { Image <TColor, TPacked> image = new Image <TColor, TPacked>(10, 10); using (var pixels = image.Lock()) { for (int i = 0; i < 10; i++) { for (int j = 0; j < 10; j++) { Vector4 v = new Vector4(i, j, 0, 1); v /= 10; TColor color = default(TColor); color.PackFromVector4(v); pixels[i, j] = color; } } } return(image); }
/// <summary> /// Fills the bottom right quadrant with all the colors producable by converting itterating over a uint and unpacking it. /// A better algorithm could be used but it works /// </summary> /// <param name="pixels"></param> private static void Rainbow(PixelAccessor <TColor> pixels) { int left = pixels.Width / 2; int right = pixels.Width; int top = pixels.Height / 2; int bottom = pixels.Height; int pixelCount = left * top; uint stepsPerPixel = (uint)(uint.MaxValue / pixelCount); TColor c = default(TColor); Color t = new Color(0); for (int x = left; x < right; x++) { for (int y = top; y < bottom; y++) { t.PackedValue += stepsPerPixel; Vector4 v = t.ToVector4(); //v.W = (x - left) / (float)left; c.PackFromVector4(v); pixels[x, y] = c; } } }