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 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 }
void SaveChangesInCurrentPalette() { if (currentPalette != null && !currentPalette.IsSystem) { var editedColors = gridViewThreads.DataSource as ICollection <CodedColor>; Debug.Assert(editedColors != null || gridViewThreads.DataSource == null, "gridViewThreads.DataSource has value of unsupported type."); if (editedColors != null) { if (currentPalette.Count != editedColors.Count) { HasChanges = true; } if (!HasChanges) { foreach (var newColor in editedColors) { var oldColor = currentPalette[newColor.GetHashCode()]; if (oldColor == null || oldColor.Argb != newColor.Argb || oldColor.ColorCode != newColor.ColorCode || oldColor.ColorName != newColor.ColorName) { HasChanges = true; break; } } } currentPalette.Clear(); foreach (var color in editedColors) { currentPalette.Add(color); } } } }
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 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."); }
/// <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); }