public ColorizeViewer()
        {
            InitializeComponent();
            colorizePixel = new ColorTransform(ColorizePixelExpandMult);
            colorizeModeCombobox.SelectedIndex = 1;

            refColorModeComboBox.SelectedIndex = 0;

            InitRefColorMenu();

            library = new ColorLibrary();

            // initialize color array
            baseColors    = new HSVColor[4];
            baseColors[0] = HSVColor.FromRGB(255, 0, 0);
            baseColors[1] = HSVColor.FromRGB(0, 255, 0);
            baseColors[2] = HSVColor.FromRGB(0, 0, 255);
            baseColors[3] = HSVColor.FromRGB(255, 255, 255);

            fileColors = new HSVColor[4];

            // save buttons in an array for easy lookup
            colorButtons    = new Button[4];
            colorButtons[0] = color0Button;
            colorButtons[1] = color1Button;
            colorButtons[2] = color2Button;
            colorButtons[3] = color3Button;

            // init button background colors
            UpdateButtons();
        }
        protected void AddColorItem(int red, int green, int blue)
        {
            Color             c    = Color.FromArgb(red, green, blue);
            ToolStripMenuItem item = new ToolStripMenuItem();

            item.BackColor = c;
            item.Text      = String.Format("{0}, {1}, {2}", red, green, blue);
            item.Click    += new EventHandler(refColorMenuItem_Click);
            refColorMenu.Items.Add(item);

            refColors.Add(HSVColor.FromRGB(red, green, blue));
        }
예제 #3
0
        public void FillTile(DDSFile ddsDest)
        {
            for (int y = 0; y < dds.Height; y++)
            {
                for (int x = 0; x < dds.Width; x++)
                {
                    HSVColor hsv = HSVColor.FromRGB(dds.GetPixel(x, y));

                    float[] distances = km.DistanceToCenters(hsv);
                    int     nearest, second;
                    float   nearestWeight;
                    if (distances[0] < distances[1])
                    {
                        nearest = 0;
                        second  = 1;
                    }
                    else
                    {
                        nearest = 1;
                        second  = 0;
                    }
                    if (distances[2] < distances[nearest])
                    {
                        second  = nearest;
                        nearest = 2;
                    }
                    else if (distances[2] < distances[second])
                    {
                        second = 2;
                    }
                    if (distances[3] < distances[nearest])
                    {
                        second  = nearest;
                        nearest = 3;
                    }
                    else if (distances[3] < distances[second])
                    {
                        second = 3;
                    }
                    // only weight between the two colors if the current point is nearer to each of them
                    // than they are to each other.  otherwise just weight to neartest.
                    if (km.CenterDistances[nearest, second] < distances[second])
                    {
                        nearestWeight = 1.0f;
                    }
                    else
                    {
                        nearestWeight = distances[second] / (distances[nearest] + distances[second]);
                    }

                    // init color components
                    int[] components = new int[4];
                    components[0] = 0;
                    components[1] = 0;
                    components[2] = 0;
                    components[3] = 0;

                    nearest = kmColorMap[nearest];
                    second  = kmColorMap[second];

                    components[nearest] = (int)(hsv.V * colors[nearest].VMult * 255 * nearestWeight);
                    components[second]  = (int)(hsv.V * colors[second].VMult * 255 * (1.0f - nearestWeight));
                    Color c = Color.FromArgb(components[3], components[0], components[1], components[2]);

                    int index = kmColorMap[km.ClosestIndex(hsv)];

                    ddsDest.SetColor(x, y, c);
                }
            }
        }
예제 #4
0
        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;
            }
        }