private void generateColorStops()
        {
            int numColorStops = PaletteManager.CurrentPalette.Count;

            PaletteColorStops = new List <ColorStop>();

            //pnl_ColorEditor.SuspendLayout();
            for (int i = 0; i < PaletteManager.CurrentPalette.Count; i++)
            {
                PaletteColor pc = PaletteManager.CurrentPalette[i];
                Color        currentColorStopColor;
                if (PaletteManager.CurrentPalette.HSL)
                {
                    currentColorStopColor = Palette.HSLtoRGBConversion(pc.Component1, pc.Component2, pc.Component3);
                }
                else
                {
                    currentColorStopColor = Color.FromArgb((int)pc.Component1, (int)pc.Component2, (int)pc.Component3);
                }

                int       ColorStopLocation = ((int)(pc.Location * NumOfDivisionsFromZero) * PaletteEditorDisplayColorWidthInPixels) + pctbx_PaletteEditor.Location.X - ColorStopOffset + PaletteEditorPaletteX;
                ColorStop newCS             = new ColorStop(i, ColorStopLocation, currentColorStopColor, i == SelectedColorStop,
                                                            new EventHandler(ColorStop_Click), new EventHandler(ColorStop_DoubleClick),
                                                            new MouseEventHandler(ColorStop_MouseDown), new MouseEventHandler(ColorStop_MouseMove));



                PaletteColorStops.Add(newCS);

                pctbx_PaletteEditor.Controls.Add(PaletteColorStops[i].Image);
            }
            //pnl_ColorEditor.ResumeLayout(false);
        }
        public Palette(string name, List <PaletteColor> clrs, bool hsl)
        {
            this.Name   = name;
            this.colors = new List <PaletteColor>();
            this.HSL    = hsl;

            PaletteColor defaultStartColor, defaultEndColor;

            if (this.HSL)
            {
                defaultStartColor = new PaletteColor(0m, 0, 1, 0.5m);
                defaultEndColor   = new PaletteColor(1m, 360, 1, 0.5m);
            }
            else
            {
                defaultStartColor = new PaletteColor(0m, 0xFF, 0xFF, 0xFF);
                defaultEndColor   = new PaletteColor(1m, 0, 0, 0);
            }

            if (clrs.Count == 0)
            {
                this.colors.Add(defaultStartColor);
                this.colors.Add(defaultEndColor);
            }
            else if (clrs.Count == 1)
            {
                this.colors.Add(clrs[0]);
                this.colors[0].Location = 0;

                this.colors.Add(defaultEndColor);
            }
            else
            {
                decimal prevLoc = -0.05m;
                foreach (PaletteColor pc in clrs)
                {
                    if (!(pc.Location - prevLoc < 0.05m || pc.Location > 1m))
                    {
                        if ((hsl && checkIfValidHSL(pc.Component1, pc.Component2, pc.Component3)) ||
                            (!hsl && checkIfValidRGB((int)pc.Component1, (int)pc.Component2, (int)pc.Component3)))
                        {
                            this.colors.Add(pc);
                        }
                    }
                }

                if (this.colors.Count == 0)
                {
                    this.colors.Add(defaultStartColor);
                    this.colors.Add(defaultEndColor);
                }
                else if (this.colors.Count == 1)
                {
                    this.colors.Add(defaultEndColor);
                }

                this.colors[0].Location = 0;
                this.colors[this.colors.Count - 1].Location = 1;
            }
        }
 public PaletteColor(PaletteColor pc)
 {
     this.Location   = pc.Location;
     this.Component1 = pc.Component1;
     this.Component2 = pc.Component2;
     this.Component3 = pc.Component3;
 }
        public void updateSelectedColorStop()
        {
            PaletteColor pc = PaletteManager.CurrentPalette[SelectedColorStop];
            Color        currentColorStopColor;

            if (PaletteManager.CurrentPalette.HSL)
            {
                currentColorStopColor = Palette.HSLtoRGBConversion(pc.Component1, pc.Component2, pc.Component3);
            }
            else
            {
                currentColorStopColor = Color.FromArgb((int)pc.Component1, (int)pc.Component2, (int)pc.Component3);
            }

            PaletteColorStops[SelectedColorStop].Color = currentColorStopColor;
            PaletteColorStops[SelectedColorStop].Image.Refresh();
        }
        public Color getColor(decimal locInPalette)
        {
            if (locInPalette > 1)
            {
                return(Color.White);
            }

            int i;

            for (i = 0; i < colors.Count; i++)
            {
                if (colors[i].Location > locInPalette)
                {
                    break;
                }
            }
            if (i == colors.Count)
            {
                i--;
            }

            int          startingColor           = i - 1;
            int          endingColor             = i;
            PaletteColor sColor                  = colors[startingColor];
            PaletteColor eColor                  = colors[endingColor];
            decimal      percentLocBetweenColors = ((locInPalette - sColor.Location) / (eColor.Location - sColor.Location));

            percentLocBetweenColors = (percentLocBetweenColors > 1) ? 1 : percentLocBetweenColors;

            decimal c1diff = eColor.Component1 - sColor.Component1;
            decimal c2diff = eColor.Component2 - sColor.Component2;
            decimal c3diff = eColor.Component3 - sColor.Component3;

            decimal newComponent1 = c1diff * percentLocBetweenColors + sColor.Component1;
            decimal newComponent2 = c2diff * percentLocBetweenColors + sColor.Component2;
            decimal newComponent3 = c3diff * percentLocBetweenColors + sColor.Component3;

            if (this.HSL)
            {
                return(HSLtoRGBConversion(newComponent1, newComponent2, newComponent3));
            }
            else
            {
                return(Color.FromArgb((int)newComponent1, (int)newComponent2, (int)newComponent3));
            }
        }
        private void pctbx_PaletteEditor_DoubleClick(object sender, EventArgs e)
        {
            Point formScreenCoordLoc   = pctbx_PaletteEditor.PointToScreen(new Point(0, 0));
            Point convertedMouseCoords = new Point(Control.MousePosition.X - formScreenCoordLoc.X, Control.MousePosition.Y - formScreenCoordLoc.Y);
            int   newCSX = convertedMouseCoords.X - PaletteEditorPaletteX;

            int newCSLowerBound, newCSUpperBound;
            int LowerBoundCS = 0, UpperBoundCS = PaletteColorStops.Count - 1;

            if (newCSX >= 0 && newCSX < pctbx_PaletteEditor.Width)
            {
                int LowerBound = 0;
                for (int clrStp = 0; clrStp < PaletteColorStops.Count; clrStp++)
                {
                    if (PaletteColorStops[clrStp].CSLocation <newCSX && PaletteColorStops[clrStp].CSLocation> LowerBound)
                    {
                        LowerBound   = PaletteColorStops[clrStp].CSLocation;
                        LowerBoundCS = clrStp;
                    }
                }
                newCSLowerBound = LowerBound + (int)(PaletteEditorPaletteWidth * 0.02);


                int UpperBound = pctbx_PaletteEditor.Width;
                for (int clrStp = 0; clrStp < PaletteColorStops.Count; clrStp++)
                {
                    if (PaletteColorStops[clrStp].CSLocation > newCSX && PaletteColorStops[clrStp].CSLocation < UpperBound)
                    {
                        UpperBound   = PaletteColorStops[clrStp].CSLocation;
                        UpperBoundCS = clrStp;
                    }
                }
                newCSUpperBound = UpperBound - (int)(PaletteEditorPaletteWidth * 0.02);

                if (newCSLowerBound < newCSX && newCSUpperBound > newCSX && UpperBoundCS > LowerBoundCS)
                {
                    decimal   newColorStopLocation = (decimal)newCSX / NumOfDivisionsFromZero;
                    Color     newCSColor           = ((Bitmap)pctbx_PaletteEditor.Image).GetPixel(newCSX, (int)(pctbx_PaletteEditor.Image.Height / 2));
                    int       ColorStopLocation    = ((int)(newColorStopLocation * NumOfDivisionsFromZero) * PaletteEditorDisplayColorWidthInPixels) + pctbx_PaletteEditor.Location.X - ColorStopOffset + PaletteEditorPaletteX;
                    ColorStop newCS = new ColorStop(100, ColorStopLocation, newCSColor, false,
                                                    new EventHandler(ColorStop_Click), new EventHandler(ColorStop_DoubleClick),
                                                    new MouseEventHandler(ColorStop_MouseDown), new MouseEventHandler(ColorStop_MouseMove));

                    PaletteColorStops.Insert(UpperBoundCS, newCS);
                    if (PaletteManager.CurrentPalette.HSL)
                    {
                        PaletteColor pc = new PaletteColor(newColorStopLocation, (decimal)newCSColor.R, (decimal)newCSColor.G, (decimal)newCSColor.B);
                        pc.convertToHSL();
                        PaletteManager.CurrentPalette.Insert(UpperBoundCS, pc);
                    }
                    else
                    {
                        PaletteColor pc = new PaletteColor(newColorStopLocation, (decimal)newCSColor.R, (decimal)newCSColor.G, (decimal)newCSColor.B);
                        PaletteManager.CurrentPalette.Insert(UpperBoundCS, pc);
                    }

                    int id = 0;
                    foreach (ColorStop cs in PaletteColorStops)
                    {
                        cs.ID         = id;
                        cs.Image.Name = id.ToString();
                        id++;
                    }

                    pctbx_PaletteEditor.Controls.Add(PaletteColorStops[UpperBoundCS].Image);
                    SelectedColorStop = UpperBoundCS;
                    selectColorStop();
                }
            }
        }
 public void Insert(int index, PaletteColor pc)
 {
     colors.Insert(index, pc);
 }