public int CompareTo(object obj)
        {
            ColorInfo comp = obj as ColorInfo;

            if (midV == comp.midV)
            {
                return(0);
            }
            else if (midV < comp.midV)
            {
                return(-1);
            }
            else
            {
                return(1);
            }
        }
        public TileMaker(string filename, int numColors)
        {
            dds = DDSFile.LoadFile(filename);

            int numPixels = dds.Width * dds.Height;

            HSVColor[] pixels = new HSVColor[numPixels];

            // create the array of colors in this image
            int offset = 0;

            for (int y = 0; y < dds.Height; y++)
            {
                for (int x = 0; x < dds.Width; x++)
                {
                    pixels[offset] = HSVColor.FromRGB(dds.GetPixel(x, y));
                    offset++;
                }
            }

            // compute the clustering
            km = new Kmeans(pixels, numColors);

            float[] minv = new float[numColors];
            float[] maxv = new float[numColors];

            for (int i = 0; i < numColors; i++)
            {
                minv[i] = float.MaxValue;
                maxv[i] = float.MinValue;
            }

            // compute min and max v for each color cluster
            for (int y = 0; y < dds.Height; y++)
            {
                for (int x = 0; x < dds.Width; x++)
                {
                    HSVColor hsv   = HSVColor.FromRGB(dds.GetPixel(x, y));
                    int      index = km.ClosestIndex(hsv);

                    // record min and max v for each channel
                    float v = hsv.V;
                    if (v < minv[index])
                    {
                        minv[index] = v;
                    }
                    if (v > maxv[index])
                    {
                        maxv[index] = v;
                    }
                }
            }

            for (int i = 0; i < numColors; i++)
            {
                if (minv[i] == float.MaxValue)
                {
                    minv[i] = 0.0f;
                }
                if (maxv[i] == float.MinValue)
                {
                    maxv[i] = 0.0f;
                }

                ColorInfo ci = new ColorInfo(i, minv[i], maxv[i], km.CenterColors[i]);
                colors.Add(ci);
            }

            colors.Sort();

            // create the mapping from the kmeans returned colors to the sorted colors
            kmColorMap = new int[numColors];
            for (int i = 0; i < numColors; i++)
            {
                kmColorMap[colors[i].KMIndex] = i;
            }
        }