/// <summary> /// Return the palette index for the passed color /// </summary> /// <param name="pixel">The <see cref="TColor"/> representing the pixel.</param> /// <param name="level">The level.</param> /// <returns> /// The <see cref="int"/> representing the index of the pixel in the palette. /// </returns> public int GetPaletteIndex(TColor pixel, int level) { int index = this.paletteIndex; if (!this.leaf) { int shift = 7 - level; Color color = new Color(pixel.ToVector4()); int pixelIndex = ((color.A & Mask[0]) >> (shift - 3)) | ((color.B & Mask[level + 1]) >> (shift - 2)) | ((color.G & Mask[level + 1]) >> (shift - 1)) | ((color.R & Mask[level + 1]) >> shift); if (this.children[pixelIndex] != null) { index = this.children[pixelIndex].GetPaletteIndex(pixel, level + 1); } else { throw new Exception($"Cannot retrive a pixel at the given index {pixelIndex}."); } } return(index); }
/// <summary> /// Add a color into the tree /// </summary> /// <param name="pixel">The color</param> /// <param name="colorBits">The number of significant color bits</param> /// <param name="level">The level in the tree</param> /// <param name="octree">The tree to which this node belongs</param> public void AddColor(TColor pixel, int colorBits, int level, Octree octree) { // Update the color information if this is a leaf if (this.leaf) { this.Increment(pixel); // Setup the previous node octree.TrackPrevious(this); } else { // Go to the next level down in the tree int shift = 7 - level; Color color = new Color(pixel.ToVector4()); int index = ((color.A & Mask[0]) >> (shift - 3)) | ((color.B & Mask[level + 1]) >> (shift - 2)) | ((color.G & Mask[level + 1]) >> (shift - 1)) | ((color.R & Mask[level + 1]) >> shift); OctreeNode child = this.children[index]; if (child == null) { // Create a new child node and store it in the array child = new OctreeNode(level + 1, colorBits, octree); this.children[index] = child; } // Add the color to the child node child.AddColor(pixel, colorBits, level + 1, octree); } }
/// <summary> /// Increment the pixel count and add to the color information /// </summary> /// <param name="pixel"> /// The pixel to add. /// </param> public void Increment(TColor pixel) { this.pixelCount++; Color color = new Color(pixel.ToVector4()); this.red += color.R; this.green += color.G; this.blue += color.B; this.alpha += color.A; }