public void TestMaxColorsCountInCorrectLimits() { var image = new CodedImage { Size = new Size(5, 5) }; var colorer = new ImageColorsController4Test(new ImageColorsManipulator(image)); Assert.AreEqual(ImageColorsController.MaximumAvailableColorsCountWithoutPalette, colorer.MaxAvailableColorsCount, "Maximum available colors count without palette."); colorer.MaxColorsCount = 1; Assert.AreEqual(2, colorer.MaxColorsCount, "Minimum valid max colors count is 2."); colorer.MaxColorsCount = 1000; Assert.AreEqual(ImageColorsController.MaximumAvailableColorsCountWithoutPalette, colorer.MaxColorsCount, "Max colors count should not go above maximum value without palette."); var palette = new CodedPalette { Name = "Palette 1" }; for (int i = 0; i < 10; i++) { palette.Add(new CodedColor(i)); } colorer = new ImageColorsController4Test(new ImageColorsManipulator(image), new[] { palette }); Assert.AreEqual(10, colorer.MaxAvailableColorsCount, "Maximum available colors count should equal colors count in palette."); colorer.MaxColorsCount = 1000; Assert.AreEqual(10, colorer.MaxColorsCount, "Max colors count should not go above colors count in palette."); }
public void TestNewColor() { CodedPalette palette = new CodedPalette { new Color(1), new CodedColor(2) }; Assert.AreEqual(typeof(CodedColor), palette[1].GetType()); Assert.AreEqual(typeof(CodedColor), palette[2].GetType()); IndexedImage image = new IndexedImage { Size = new Size(2, 2) }; int[] pixels; using (image.LockPixels(out pixels)) { pixels[0] = 0; pixels[1] = 1; pixels[2] = 2; pixels[3] = 1; } image.Palette = new CodedPalette(); image.CompletePalette(); Assert.AreEqual(3, image.Palette.Count); foreach (var color in image.Palette) { Assert.AreEqual(typeof(CodedColor), color.GetType()); } }
void buttonAddPalette_Click(object sender, EventArgs e) { using (var inputDialog = new InputDialog(Resources.QueryEnterNewPaletteName)) { if (inputDialog.ShowDialog(this) == DialogResult.OK) { var newName = inputDialog.Answer.Trim(); if (string.IsNullOrEmpty(newName)) { return; } if (AllPalettes.Any(p => p.PaletteName.Equals(newName, StringComparison.CurrentCultureIgnoreCase))) { MessageBox.Show(Resources.ErrorSelectedPaletteNameAlreadyUsed); return; } var palette = new CodedPalette { Name = newName, IsSystem = false, FileName = Path.Combine(Settings.Default.UserPalettesLocationSafe, newName + SAEResources.ThreadFileExtension) }; AddNewPalette(palette); } } }
public void TestChangeColorAttributes() { CodedColor color = new CodedColor(0) { SymbolChar = 'A', ColorCode = "100", ColorName = "Black" }; CodedPalette palette = new CodedPalette(); bool eventFired = false; palette.ColorAttributesChanged += (sender, e) => { eventFired = true; Assert.AreSame(palette, sender); Assert.AreEqual('A', e.OldSymbol); Assert.AreEqual('B', e.NewSymbol); Assert.AreEqual("100", e.OldCode); Assert.AreEqual("200", e.NewCode); Assert.AreEqual("Black", e.OldName); Assert.AreEqual("White", e.NewName); }; Assert.IsFalse(eventFired, "Precondition"); palette.ChangeColorAttributes(color, 'B', "200", "White"); Assert.IsTrue(eventFired, "Event should have been fired"); }
public void TestSerialize() { var palette = new CodedPalette { Name = "Palette 1", SymbolsFont = "Times New Roman" }; palette.Add(new CodedColor(1)); palette.Add(new CodedColor(2)); var sb = new StringBuilder(); using (var writer = XmlWriter.Create(sb)) { palette.WriteToXml(writer); } using (var reader = XmlReader.Create(new StringReader(sb.ToString()))) { var reloadedPalette = PaletteSerializer.ReadFromXml(reader); Assert.AreEqual("Palette 1", reloadedPalette.Name); Assert.AreEqual("Times New Roman", reloadedPalette.SymbolsFont); Assert.AreEqual(2, reloadedPalette.Count); Assert.IsTrue(reloadedPalette[1] is CodedColor); Assert.IsTrue(reloadedPalette[2] is CodedColor); } }
public void TestOrderByCode() { var palette = new CodedPalette { new CodedColor(1) { ColorCode = "2" }, new CodedColor(2) { ColorCode = "120" }, new CodedColor(3) { ColorCode = "0999" }, new CodedColor(4) { ColorCode = "A" }, new CodedColor(5) { ColorCode = "998" }, }; AssertPaletteOrder(palette.OrderByCode(), new CodedColor(1), new CodedColor(2), new CodedColor(5), new CodedColor(3), new CodedColor(4)); AssertPaletteOrder(palette.OrderByCode(true), new CodedColor(4), new CodedColor(3), new CodedColor(5), new CodedColor(2), new CodedColor(1)); }
public void TestOrderBySymbol() { var palette = new CodedPalette { new CodedColor(1) { SymbolChar = 'Z' }, new CodedColor(2) { SymbolChar = 'B' }, new CodedColor(3) { SymbolChar = 'W' }, new CodedColor(4) { SymbolChar = 'C' }, new CodedColor(5) { SymbolChar = 'A' }, }; AssertPaletteOrder(palette.OrderBySymbol(), new CodedColor(5), new CodedColor(2), new CodedColor(4), new CodedColor(3), new CodedColor(1)); AssertPaletteOrder(palette.OrderBySymbol(true), new CodedColor(1), new CodedColor(3), new CodedColor(4), new CodedColor(2), new CodedColor(5)); }
public void TestPaletteName() { var image = new CodedImage { Size = new Size(5, 5) }; var palette1 = new CodedPalette { Name = "Palette 1" }; var palette2 = new CodedPalette { Name = "Palette 2" }; var colorer = new ImageColorsController4Test(new ImageColorsManipulator(image), new[] { palette1, palette2 }); Assert.AreSame(palette1, colorer.SelectedPalette, "First palette should be selected by default."); Assert.AreEqual("Palette 1", colorer.PaletteName, "Name of current palette should be returned."); colorer.PaletteName = "Palette 2"; Assert.AreSame(palette2, colorer.SelectedPalette, "Second palette should be selected."); Assert.AreEqual("Palette 2", colorer.PaletteName, "Name of current palette should be returned."); colorer.PaletteName = "Palette 3"; Assert.AreSame(palette2, colorer.SelectedPalette, "Previous palette should remain selected."); Assert.AreEqual("Palette 2", colorer.PaletteName, "Name of current palette should be returned."); colorer.PaletteName = "pAlEtTe 1"; Assert.AreSame(palette1, colorer.SelectedPalette, "First palette should be selected ignoring characters case."); Assert.AreEqual("Palette 1", colorer.PaletteName, "Name of current palette should be returned."); }
void buttonCancelAll_Click(object sender, EventArgs e) { allPalettes = null; currentPalette = null; BindThreadsGridView(null); HasChanges = false; }
void AddNewPalette(CodedPalette newPalette) { var paletteData = new PaletteData(newPalette); AllPalettes.Add(paletteData); gridViewPalettes.CurrentCell = gridViewPalettes.Rows[AllPalettes.IndexOf(paletteData)].Cells[0]; HasChanges = true; }
public void TestPalette() { var image = new CodedImage(); Assert.IsTrue(image.Palette is CodedPalette); var newPalette = new CodedPalette(); image.Palette = newPalette; Assert.AreEqual(newPalette, image.Palette); }
/// <summary> /// Exportns <see cref="Palette"/> object to CSV. /// </summary> /// <param name="palette"><see cref="Palette"/> object to be exported.</param> /// <param name="includeCaptions">Specifies if first line should contain values captions.</param> /// <returns>Enumeration of CSV string lines.</returns> public static IEnumerable <string> ExportToCsv(this CodedPalette palette, bool includeCaptions = false) { if (includeCaptions) { yield return("Code,Name,Reg,Green,Blue"); } foreach (var color in palette) { yield return(string.Join(",", color.ColorCode, color.ColorName, color.R.ToString(), color.G.ToString(), color.B.ToString())); } }
public void TestDefaultSettings() { var image = new CodedImage { Size = new Size(5, 5) }; var colorer = new ImageColorsController4Test(new ImageColorsManipulator(image)); var palette1 = new CodedPalette { Name = "Palette 1" }; var palette2 = new CodedPalette { Name = "Palette 2" }; palette2.Add(1); palette2.Add(2); palette2.Add(3); using (colorer.SuspendCallManipulations()) { colorer.AddColorPalettes(new[] { palette1, palette2 }); colorer.PaletteName = "Palette 2"; colorer.MaxColorsCount = 2; colorer.DitherLevel = 5; colorer.ColorComparisonType = ImageColorsController.ColorComparisonTypes.Exact; colorer.EnsureBlackAndWhiteColors = true; } var newColorer = new ImageColorsController4Test(new ImageColorsManipulator(image), new[] { palette1, palette2 }, palette1); // Pass different initial palette // Precondition checks Assert.AreNotEqual("Palette 2", newColorer.PaletteName); Assert.AreNotSame(palette2, newColorer.SelectedPalette); Assert.AreNotEqual(2, newColorer.MaxColorsCount); Assert.AreNotEqual(5, newColorer.DitherLevel); Assert.AreNotEqual(ImageColorsController.ColorComparisonTypes.Exact, newColorer.ColorComparisonType); Assert.IsFalse(newColorer.EnsureBlackAndWhiteColors); colorer.SaveDefaults(); newColorer = new ImageColorsController4Test(new ImageColorsManipulator(image), new[] { palette1, palette2 }, palette1); // Pass different initial palette Assert.AreEqual("Palette 2", newColorer.PaletteName); Assert.AreSame(palette2, newColorer.SelectedPalette); Assert.AreEqual(2, newColorer.MaxColorsCount); Assert.AreEqual(5, newColorer.DitherLevel); Assert.AreEqual(ImageColorsController.ColorComparisonTypes.Exact, newColorer.ColorComparisonType); Assert.IsTrue(newColorer.EnsureBlackAndWhiteColors); SAEWizardSettings.Default.Reset(); // Restore defaults }
public PaletteController(CodedPalette palette, TrueTypeFont symbolsFont) { Palette = palette; SymbolsFont = symbolsFont; ColorsOrderMode setOrderMode; OrderMode = Enum.TryParse(GridPainterSettings.Default.PaletteOrderMode, out setOrderMode) ? setOrderMode : ColorsOrderMode.Code; Palette.ColorAttributesChanged += Palette_ColorAttributesChanged; Palette.ColorAdded += Palette_ColorChanged; Palette.ColorRemoved += Palette_ColorChanged; Palette.OccurrencesChanged += Palette_ColorChanged; }
public void TestClone() { CodedPalette palette = new CodedPalette { Name = "Test1", SymbolsFont = "Arial" }; CodedPalette clonedPalette = palette.Clone(); Assert.AreEqual(typeof(CodedPalette), clonedPalette.GetType()); Assert.AreNotSame(palette, clonedPalette); Assert.AreEqual(palette.Count, clonedPalette.Count); Assert.IsTrue(((IEnumerable <CodedColor>)palette).All(color => clonedPalette.Contains(color))); Assert.AreEqual("Test1", clonedPalette.Name); Assert.AreEqual("Arial", clonedPalette.SymbolsFont); }
public static IEnumerable <CodedPalette> GetAllPalettes(string additionalPalettesPath) { var assembly = Assembly.GetExecutingAssembly(); foreach (var name in assembly.GetManifestResourceNames()) { if (name.EndsWith(ThreadFileExtension, StringComparison.OrdinalIgnoreCase)) { using (var stream = assembly.GetManifestResourceStream(name)) { if (stream != null) { using (var reader = XmlReader.Create(stream)) { var palette = PaletteSerializer.ReadFromXml(reader); palette.IsSystem = true; yield return(palette); } } } } } if (!String.IsNullOrEmpty(additionalPalettesPath) && Directory.Exists(additionalPalettesPath)) { foreach (var fileName in Directory.GetFiles(additionalPalettesPath, "*" + ThreadFileExtension, SearchOption.TopDirectoryOnly)) { CodedPalette palette = null; try { using (var stream = new FileStream(fileName, FileMode.Open)) using (var reader = XmlReader.Create(stream)) { palette = PaletteSerializer.ReadFromXml(reader); palette.IsSystem = false; palette.FileName = fileName; } } catch { // Skip any file with faulty data } if (palette != null) { yield return(palette); } } } }
public void QuantizeColors(int colorsCount, CodedPalette destPalette = null, int ditherLevel = 0, bool ensureSpecialColors = false) { Debug.Assert(colorsCount >= 2, "colorsCount should be greater than 1."); lastColorsCount = colorsCount; lastDestPalette = destPalette; lastDitherLevel = ditherLevel; lastEnsureSpecialColors = ensureSpecialColors; if (IsManipulatedImageInitialized) { new NeuQuant().QuantizeColors(SourceImage, colorsCount, destPalette, 1, ditherLevel, true, ManipulatedImage, ensureSpecialColors); OnImageChanged(); } }
public void TestGetEnumerator() { CodedPalette palette = new CodedPalette { new CodedColor(1), new CodedColor(2), new CodedColor(3) }; Assert.AreEqual(3, palette.Count); foreach (CodedColor color in (IEnumerable <CodedColor>)palette) { Assert.IsNotNull(color); } }
/// <summary> /// Writes <see cref="Palette"/> object to XML. /// </summary> /// <param name="palette">Palette object to serialize.</param> /// <param name="writer">XML writer.</param> public static void WriteToXml(this CodedPalette palette, XmlWriter writer) { writer.WriteStartElement(XmlPaletteElement); writer.WriteSingleElement(XmlNameElement, palette.Name); if (!string.IsNullOrEmpty(palette.SymbolsFont)) { writer.WriteSingleElement(XmlFontElement, palette.SymbolsFont); } foreach (CodedColor color in palette) { color.WriteToXml(writer); } writer.WriteEndElement(); }
public void TestCallManipulators() { var image = new CodedImage { Size = new Size(50, 50) }; image.CompletePalette(); for (int x = 0; x < 50; x += 5) { for (int y = 0; y < 50; y++) { image[x, y] = new CodedColor(0); image[x + 1, y] = new CodedColor(255, 255, 255); image[x + 2, y] = new CodedColor(255, 0, 0); image[x + 3, y] = new CodedColor(127, 0, 0); image[x + 4, y] = new CodedColor(0, 255, 0); } } image.CompletePalette(); Assert.AreEqual(5, image.Palette.Count, "Precondition: there are 5 different colors in source image."); var palette1 = new CodedPalette { Name = "Palette 1" }; palette1.Add(new CodedColor(0)); palette1.Add(new CodedColor(255, 255, 255)); palette1.Add(new CodedColor(255, 0, 0)); var palette2 = new CodedPalette { Name = "Palette 2" }; palette2.Add(new CodedColor(0)); palette2.Add(new CodedColor(255, 255, 255)); var colorer = new ImageColorsController(new ImageColorsManipulator(image), new[] { palette1, palette2 }, palette1); colorer.Manipulator.ManipulatedImage.CompletePalette(); Assert.AreEqual(3, colorer.Manipulator.ManipulatedImage.Palette.Count, "Colors reducing manipulation should be applied."); colorer.PaletteName = "Palette 2"; colorer.Manipulator.ManipulatedImage.CompletePalette(); Assert.AreEqual(2, colorer.Manipulator.ManipulatedImage.Palette.Count, "Colors reducing manipulation should be applied with new maximum available colors number."); }
public void TestAvailablePaletteNames() { var image = new CodedImage { Size = new Size(5, 5) }; var palette1 = new CodedPalette { Name = "Palette 1" }; var palette2 = new CodedPalette { Name = "Palette 2" }; var colorer = new ImageColorsController4Test(new ImageColorsManipulator(image), new[] { palette2, palette1 }); var availablePalettes = colorer.AvailablePalettes.ToArray(); Assert.AreEqual("Palette 2", availablePalettes[0].Name, "Should return all names in original order."); Assert.AreEqual("Palette 1", availablePalettes[1].Name, "Should return all names in original order."); }
void BindThreadsGridView(CodedPalette selectedPalette) { if (selectedPalette != currentPalette || selectedPalette == null) { SaveChangesInCurrentPalette(); var oldDataSource = gridViewThreads.DataSource as BindingList <CodedColor>; if (oldDataSource != null) { oldDataSource.AddingNew -= ThreadsDataSource_AddingNew; } try { var editable = !selectedPalette?.IsSystem ?? false; currentPalette = selectedPalette; if (currentPalette != null) { var newDataSource = new SortableBindingList <CodedColor>(currentPalette.ToList <CodedColor>()) { AllowNew = editable, AllowEdit = editable, AllowRemove = editable, RaiseListChangedEvents = editable }; newDataSource.AddingNew += ThreadsDataSource_AddingNew; gridViewThreads.DataSource = newDataSource; } else { gridViewThreads.DataSource = null; } gridViewThreads.ReadOnly = !editable; gridViewThreads.SelectionMode = gridViewThreads.ReadOnly ? DataGridViewSelectionMode.FullRowSelect : DataGridViewSelectionMode.RowHeaderSelect; } catch { currentPalette = null; gridViewThreads.DataSource = null; } } }
public void TestQuantizeColors() { var image = new CodedImage { Size = new Size(30, 20), Palette = new CodedPalette() }; image.CompletePalette(); for (int x = 0; x < 30; x++) { for (int y = 0; y < 20; y++) { image[x, y] = new CodedColor((byte)(x * 8), (byte)(y * 12), (byte)((x + y) * 5)); } } var destPalette = new CodedPalette { new CodedColor(0, 0, 0) { ColorCode = "100" }, new CodedColor(255, 255, 255) { ColorCode = "200" }, new CodedColor(127, 127, 127) { ColorCode = "300" } }; var colorer = new ImageColorsManipulator(image); Assert.AreEqual(600, colorer.ManipulatedImage.Palette.Count, "Precondition"); colorer.QuantizeColors(3, destPalette); Assert.AreEqual(3, colorer.ManipulatedImage.Palette.Count); Assert.AreEqual("100", colorer.ManipulatedImage.Palette[0].ColorCode); Assert.AreEqual("200", colorer.ManipulatedImage.Palette[new CodedColor(255, 255, 255).GetHashCode()].ColorCode); Assert.AreEqual("300", colorer.ManipulatedImage.Palette[new CodedColor(127, 127, 127).GetHashCode()].ColorCode); }
/// <summary> /// Imports <see cref="Palette"/> object from CSV. /// </summary> /// <param name="csv">Enumeration of CSV string lines.</param> /// <param name="skipFirstLine">Specifies if first line should be skipped.</param> /// <param name="indexCode">Index of color code value.</param> /// <param name="indexName">Index of color name value.</param> /// <param name="indexR">Index of color Red component value.</param> /// <param name="indexG">Index of color Green component value.</param> /// <param name="indexB">Index of color Blue component value.</param> /// <param name="name">New palette name.</param> /// <returns>Instance of <see cref="Palette"/> with all <see cref="Color"/> imported from CSV.</returns> public static CodedPalette ImportFromCsv(IEnumerable <string> csv, bool skipFirstLine = false, int indexCode = 0, int indexName = 1, int indexR = 2, int indexG = 3, int indexB = 4, string name = "") { var firstLine = !skipFirstLine; var palette = new CodedPalette { Name = name }; foreach (var line in csv) { if (skipFirstLine) { skipFirstLine = false; continue; } try { byte r, g, b; string cCode, cName; GetLineValues(line, out cCode, out cName, out r, out g, out b, name, indexCode, indexName, indexR, indexG, indexB); var color = new CodedColor(r, g, b) { ColorCode = cCode, ColorName = cName }; palette.Add(color); } catch (FormatException) { // Ignore for first line - maybe it is header if (!firstLine) { throw; } } firstLine = false; } return(palette); }
/// <summary> /// Reads <see cref="Palette"/> object from XML. /// </summary> /// <param name="reader">XML reader.</param> /// <returns>New Palette object.</returns> public static CodedPalette ReadFromXml(XmlReader reader) { while (string.IsNullOrEmpty(reader.Name) && !reader.EOF) { reader.Read(); if (reader.Name == "xml") { reader.Skip(); } } Debug.Assert(reader.Name == XmlPaletteElement, "Wrong XML input for Palette deserialization."); CodedPalette palette = new CodedPalette(); reader.ReadStartElement(); while (reader.IsStartElement()) { switch (reader.Name) { case XmlNameElement: palette.Name = reader.ReadElementContentAsString(); break; case XmlFontElement: palette.SymbolsFont = reader.ReadElementContentAsString(); break; case ColorSerializer.XmlColorElement: palette.Add(ColorSerializer.ReadFromXml(reader)); break; default: reader.Skip(); break; } } reader.ReadEndElement(); return(palette); }
public void TestAddColorPalettes() { var image = new CodedImage { Size = new Size(5, 5) }; var colorer = new ImageColorsController4Test(new ImageColorsManipulator(image)); Assert.IsNull(colorer.AvailablePalettes); Assert.IsNull(colorer.AvailablePalettesNames); Assert.IsNull(colorer.SelectedPalette); var palette1 = new CodedPalette { Name = "Palette 1" }; var palette2 = new CodedPalette { Name = "Palette 2" }; colorer.AddColorPalettes(new[] { palette1, palette2 }); Assert.AreEqual(2, colorer.AvailablePalettes.Count()); Assert.AreEqual(2, colorer.AvailablePalettesNames.Count()); Assert.AreSame(palette1, colorer.SelectedPalette); Assert.AreEqual("Palette 1", colorer.PaletteName); var palette3 = new CodedPalette { Name = "Palette 3" }; colorer.AddColorPalettes(new[] { palette3 }); Assert.AreEqual(3, colorer.AvailablePalettes.Count()); Assert.AreEqual(3, colorer.AvailablePalettesNames.Count()); Assert.AreSame(palette1, colorer.SelectedPalette, "Should keep already selected palette."); Assert.AreEqual("Palette 1", colorer.PaletteName, "Should keep already selected palette."); }
void ComboBoxKit_TextChanged(object sender, EventArgs e) { paletteName = comboBoxKit.Text; selectedPalette = null; UpdateListView(); }
internal PaletteData(CodedPalette palette) { Palette = palette; }
public ImageColorsController(ImageColorsManipulator manipulator, IEnumerable <CodedPalette> availablePalettes = null, CodedPalette selectedPalette = null) : base(manipulator) { Debug.Assert(selectedPalette == null || availablePalettes != null && availablePalettes.Contains(selectedPalette), "selectedPalette should be included into availablePalettes collection."); using (SuspendCallManipulations()) { AvailablePalettes = availablePalettes?.ToList(); SelectedPalette = selectedPalette ?? AvailablePalettes?.FirstOrDefault(); RestoreDefaults(); CallManipulations(); } }
public ImageColorsController4Test(ImageColorsManipulator manipulator, IEnumerable <CodedPalette> availablePalettes = null, CodedPalette selectedPalette = null) : base(manipulator, availablePalettes, selectedPalette) { }