public void AddPattern(IColourInfo c, Bitmap b) { // find an index i that is unique in patternList. int i = 0; string key = ("" + i).PadLeft(4, '0'); bool keyFree = false; while (!keyFree) { keyFree = true; foreach (ListViewItem lvi in patternList.Items) { key = ("" + i).PadLeft(4, '0'); string imageKey = lvi.ImageKey; if (key.Equals(imageKey)) { keyFree = false; break; } } i++; } AddPattern(c, b, key); }
/// <summary> /// Associates each of the colours in the pattern list with the number of times the colour appears. /// Will not set if number of entries exceeds MainForm.MAX_COLOURS /// </summary> public void UpdateColourMap() { patternList.Items.Clear(); if (mParent.ColourMap.Count > MainForm.MAX_COLOURS) { MessageBox.Show("Too many colours!"); return; } patternCreator = new PatternCreator(); List <IColourInfo> temp = mParent.ColourMap.ToList(); // loop through ColourMap, adding max frequency first. while (temp.Count > 0) { int max = int.MinValue; IColourInfo maxC = temp[0]; foreach (IColourInfo col in temp) { if (col.Frequency > max) { max = col.Frequency; maxC = col; } } AddPattern(maxC); temp.Remove(maxC); } patternList.Sort(); }
public void RemoveFromPalette(IColourInfo colorToRemove) { if (colorToRemove != null) { RemoveFromPalette(colorToRemove.Colour); } }
public void AddPattern(IColourInfo c, Bitmap bm, string imageKey) { ListViewItem lvi = new ListViewItem(" ", imageKey); patternList.SmallImageList.Images.Add(imageKey, bm); Color col = c.Colour; string r = String.Format("{0:X}", col.R); string g = String.Format("{0:X}", col.G); string b = String.Format("{0:X}", col.B); lvi.ToolTipText = String.Format("Colour: 0x" + r.PadLeft(2, '0') + g.PadLeft(2, '0') + b.PadLeft(2, '0') + ", frequency: " + c.Frequency + ", imageKey: " + lvi.ImageKey); lvi.BackColor = col; lvi.Tag = c.Frequency; lvi.SubItems.Add(c.Name); lvi.SubItems.Add("" + c.Frequency); if (c is DMCColour) { lvi.SubItems.Add("DMC: " + (c as DMCColour).DMCNumber); } lvi.UseItemStyleForSubItems = false; patternList.Items.Add(lvi); }
private void listView_ItemChecked(object sender, ItemCheckedEventArgs e) { if (enableEvents) { IColourInfo c = (IColourInfo)e.Item.Tag; c.IsChecked = e.Item.Checked; updateInfo(e.Item); } }
public IColourInfo[] ToArray() { IColourInfo[] ret = new IColourInfo[Colours.Count]; int i = 0; foreach (KeyValuePair <Color, IColourInfo> pair in Colours) { ret[i] = pair.Value.Clone(); i++; } return(ret); }
public void AddPattern(IColourInfo c) { // find out if Color already exists in list. foreach (ListViewItem lvi in patternList.Items) { if (lvi.BackColor == c.Colour) { lvi.Tag = c.Frequency; return; } } // Colour wasn't in list. Add a new one. AddPattern(c, CreateNewPattern()); }
public object ReduceColourDepth(object o, BackgroundWorker w, DoWorkEventArgs e) { Console.WriteLine("Worker thread: " + this + "," + TargetColours + "," + ColourMap + "," + ColourMap.Count); // This case we're removing colours one at a time, replacing them with closest colours if (TargetColours > 0) { Console.WriteLine("ReduceColourDepth: initial nColours = " + ColourMap.Count + ", Target: " + TargetColours); int toRemove = ColourMap.Count - TargetColours; for (int i = 0; i < toRemove; i++) { if (w.CancellationPending) { e.Cancel = true; return(this); } IColourInfo least = ColourMap.GetLeastCommonColourInfo(true); RemoveFromPalette(least); UpdateColourMapFromImage(); w.ReportProgress(i * 100 / toRemove); } Console.WriteLine("ReduceColourDepth: final number of colours = " + ColourMap.Count); } else // This case we have no idea how many colours, we just want to fit the colour map. { Bitmap orig = mOutput; Bitmap b = new Bitmap(orig.Width, orig.Height, PixelFormat.Format24bppRgb); Console.WriteLine("ReduceColourDepth: initial nColours = " + ColourMap.Count); int i = 0; for (int x = 0; x < b.Width; x++) { for (int y = 0; y < b.Height; y++) { if (w.CancellationPending) { e.Cancel = true; return(this); } Color c = orig.GetPixel(x, y); Color newC = ColourMap.GetNearestColour(c); b.SetPixel(x, y, newC); w.ReportProgress((i++) * 100 / (b.Width * b.Height)); } } mOutput = b; } return(this); }
private void updateInfo(ListViewItem lvi) { if (lvi == null) { pictureBox.Image = null; infoBox.Text = ""; return; } IColourInfo c = (IColourInfo)lvi.Tag; //c.IsChecked = lvi.Checked; infoBox.Text = c.PrintInfo(); statusLabel.Text = c.Name; Bitmap b = new Bitmap(pictureBox.Width, pictureBox.Height); Graphics g = Graphics.FromImage(b); g.FillRectangle(new SolidBrush(lvi.BackColor), 0, 0, b.Width, b.Height); g.Dispose(); pictureBox.Image = b; }
public IColourInfo[] ToArray() { IColourInfo[] ret = new IColourInfo[Colours.Count]; int i = 0; foreach (KeyValuePair<Color, IColourInfo> pair in Colours) { ret[i] = pair.Value.Clone(); i++; } return ret; }
public void RemoveFromPalette(IColourInfo colorToRemove) { if(colorToRemove != null) RemoveFromPalette(colorToRemove.Colour); }
public void AddPattern(IColourInfo c, Bitmap bm, string imageKey) { ListViewItem lvi = new ListViewItem(" ",imageKey); patternList.SmallImageList.Images.Add(imageKey,bm); Color col = c.Colour; string r = String.Format("{0:X}", col.R); string g = String.Format("{0:X}", col.G); string b = String.Format("{0:X}", col.B); lvi.ToolTipText = String.Format("Colour: 0x" + r.PadLeft(2, '0') + g.PadLeft(2, '0') + b.PadLeft(2, '0')+ ", frequency: "+c.Frequency+", imageKey: "+lvi.ImageKey); lvi.BackColor = col; lvi.Tag = c.Frequency; lvi.SubItems.Add(c.Name); lvi.SubItems.Add(""+c.Frequency); if(c is DMCColour) { lvi.SubItems.Add("DMC: "+(c as DMCColour).DMCNumber); } lvi.UseItemStyleForSubItems = false; patternList.Items.Add(lvi); }