Example #1
0
        void buttonLoad_Click(object sender, EventArgs e)
        {
            using (var openDialog = new OpenFileDialog {
                Filter = "Thread kits (*.thread)|*.thread|Comma separated values file (*.csv)|*.csv"
            })
            {
                if (openDialog.ShowDialog(this) == DialogResult.OK)
                {
                    var extension = Path.GetExtension(openDialog.FileName);

                    if (!string.IsNullOrEmpty(extension) && extension.Equals(".csv", StringComparison.OrdinalIgnoreCase))
                    {
                        Palette = PaletteCsvImporter.ImportFromCsv(File.ReadAllLines(openDialog.FileName), name: Path.GetFileNameWithoutExtension(openDialog.FileName), indexName: -1);
                        Colors  = Palette.OrderByCode().ToList();
                        listViewPalette.VirtualListSize = Colors.Count;
                        listViewPalette.Invalidate();
                        listViewPalette.Update();
                    }
                    else
                    {
                        using (var stream = new FileStream(openDialog.FileName, FileMode.Open))
                            using (var reader = XmlReader.Create(stream))
                            {
                                Palette = PaletteSerializer.ReadFromXml(reader);
                                Colors  = Palette.OrderByCode().ToList();
                                listViewPalette.VirtualListSize = Colors.Count;
                                listViewPalette.Invalidate();
                                listViewPalette.Update();
                            }
                    }
                }
            }
        }
        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);
            }
        }
Example #3
0
        private void kbtnLoadPalette_Click(object sender, EventArgs e)
        {
            using (FileDialog fd = new OpenFileDialog {
                Filter = PaletteSerializer.DefaultOpenFilter, DefaultExt = "pal", Title = "Open a custom palette file:"
            })
            {
                if (fd.ShowDialog(this) == DialogResult.OK)
                {
                    try
                    {
                        IPaletteSerializer serializer;

                        serializer = PaletteSerializer.GetSerializer(fd.FileName);

                        if (serializer != null)
                        {
                            ColorCollection colours;

                            if (!serializer.CanRead)
                            {
                                throw new InvalidOperationException("Serializer does not support reading palettes.");
                            }

                            using (FileStream fs = File.OpenRead(fd.FileName))
                            {
                                colours = serializer.Deserialize(fs);
                            }

                            if (colours != null)
                            {
                                while (colours.Count > 96)
                                {
                                    colours.RemoveAt(colours.Count - 1);
                                }

                                while (colours.Count < 96)
                                {
                                    colours.Add(Color.White);
                                }

                                cgColours.Colors = colours;
                            }
                        }
                        else
                        {
                            KryptonMessageBox.Show("Sorry, unable to open palette, the file format is not supported or is not recognized.", "Load Palette", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                        }
                    }
                    catch (Exception exc)
                    {
                        KryptonMessageBox.Show($@"Sorry, unable to open palette. { exc.GetBaseException().Message }", "Load Palette", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }
Example #4
0
        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 GetSerializerTest()
        {
            // arrange
            IPaletteSerializer actual;
            string             workFileName;

            workFileName = Path.GetTempFileName();
            File.Copy(Path.Combine(this.DataPath, "db32.aco"), workFileName, true);

            // act
            actual = PaletteSerializer.GetSerializer(workFileName);
            File.Delete(workFileName);

            // assert
            actual.Should().BeOfType <AdobePhotoshopColorSwatchSerializer>();
        }
        public void GetSerializerTest()
        {
            // arrange
            IPaletteSerializer actual;
            string             workFileName;

            workFileName = Path.GetTempFileName();
            File.Copy(Path.Combine(this.DataPath, "background.lbm"), workFileName, true);

            // act
            actual = PaletteSerializer.GetSerializer(workFileName);
            File.Delete(workFileName);

            // assert
            actual.Should().BeOfType <InterleavedBitmapPaletteSerializer>();
        }
        public void GetSerializerTest()
        {
            // arrange
            IPaletteSerializer actual;
            string             workFileName;

            workFileName = Path.GetTempFileName();
            File.Copy(Path.Combine(this.DataPath, "PaintNet.txt"), workFileName, true);

            // act
            actual = PaletteSerializer.GetSerializer(workFileName);
            File.Delete(workFileName);

            // assert
            actual.Should().BeOfType <PaintNetPaletteSerializer>();
        }
Example #8
0
        void buttonImportPalette_Click(object sender, EventArgs e)
        {
            using (var openDialog = new OpenFileDialog {
                Filter = Resources.FileFilterAllPalettes
            })
            {
                if (openDialog.ShowDialog(this) == DialogResult.OK)
                {
                    try
                    {
                        CodedPalette newPalette;

                        var extension = Path.GetExtension(openDialog.FileName);

                        if (!string.IsNullOrEmpty(extension) && extension.Equals(".csv", StringComparison.OrdinalIgnoreCase))
                        {
                            var newName = Path.GetFileNameWithoutExtension(openDialog.FileName);
                            newPalette          = PaletteCsvImporter.ImportFromCsv(File.ReadAllLines(openDialog.FileName), name: newName);
                            newPalette.IsSystem = false;
                            newPalette.FileName = newPalette.FileName = Path.Combine(Settings.Default.UserPalettesLocationSafe, newName + SAEResources.ThreadFileExtension);
                        }
                        else
                        {
                            using (var stream = new FileStream(openDialog.FileName, FileMode.Open))
                                using (var reader = XmlReader.Create(stream))
                                {
                                    var newName = Path.GetFileNameWithoutExtension(openDialog.FileName);
                                    newPalette          = PaletteSerializer.ReadFromXml(reader);
                                    newPalette.Name     = newName;
                                    newPalette.IsSystem = false;
                                    newPalette.FileName = Path.Combine(Settings.Default.UserPalettesLocationSafe, newName + SAEResources.ThreadFileExtension);
                                }
                        }

                        AddNewPalette(newPalette);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(Resources.ErrorCannotOpenFile + Environment.NewLine + ex.Message);
                    }
                }
            }
        }
Example #9
0
        private void cmPalette_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem is ToolStripMenuItem)
            {
                if (e.ClickedItem.Text.StartsWith(this._("Load Palette")))
                {
                    cmPalette.Close();
                    #region Load Palette
                    using (FileDialog dialog = new OpenFileDialog
                    {
                        Filter = PaletteSerializer.DefaultOpenFilter,
                        DefaultExt = "pal",
                        Title = this._("Open Palette File")
                    })
                    {
                        if (dialog.ShowDialog(this) == DialogResult.OK)
                        {
                            try
                            {
                                IPaletteSerializer serializer;

                                serializer = PaletteSerializer.GetSerializer(dialog.FileName);
                                if (serializer != null)
                                {
                                    ColorCollection palette;

                                    if (!serializer.CanRead)
                                    {
                                        throw new InvalidOperationException(this._("Serializer does not support reading palettes."));
                                    }

                                    using (FileStream file = File.OpenRead(dialog.FileName))
                                    {
                                        palette = serializer.Deserialize(file);
                                    }

                                    if (palette != null)
                                    {
                                        // we can only display 96 colors in the color grid due to it's size, so if there's more, bin them
                                        //while (palette.Count > 96)
                                        //{
                                        //    palette.RemoveAt(palette.Count - 1);
                                        //}

                                        // or if we have less, fill in the blanks
                                        //while (palette.Count < 96)
                                        //{
                                        //    palette.Add(Color.White);
                                        //}

                                        colorGrid.Colors = palette;

                                        //var item = cmPalette.Items[cmPalette.Items.Count-1];
                                        //if ( item.GetType() == typeof( ToolStripSeparator ) ) item.Visible = true;

                                        var  pal_name = Path.GetFileNameWithoutExtension(dialog.FileName);
                                        bool exists   = false;
                                        foreach (var item in cmPalette.Items)
                                        {
                                            //if ( item.GetType() == typeof( ToolStripSeparator ) ) continue;
                                            if (item.GetType() == typeof(ToolStripSeparator))
                                            {
                                                (item as ToolStripSeparator).Visible = true;
                                            }
                                            else if (item.GetType() == typeof(ToolStripMenuItem))
                                            {
                                                if ((item as ToolStripMenuItem).Text.Equals(pal_name, StringComparison.CurrentCultureIgnoreCase))
                                                {
                                                    exists = true;
                                                    (item as ToolStripMenuItem).Checked = true;
                                                }
                                                else
                                                {
                                                    (item as ToolStripMenuItem).Checked = false;
                                                }
                                            }
                                        }
                                        if (!exists)
                                        {
                                            var mi_pal = new ToolStripMenuItem(pal_name);
                                            mi_pal.Tag          = true;
                                            mi_pal.CheckOnClick = true;
                                            mi_pal.Checked      = true;
                                            cmPalette.Items.Add(mi_pal);
                                            CustomPalette.Add(pal_name, palette);
                                        }
                                    }
                                }
                                else
                                {
                                    MessageBox.Show(this._("Sorry, unable to open palette, the file format is not supported or is not recognized."), this._("Load Palette"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                                }
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(string.Format(this._("Sorry, unable to open palette. {0}"), ex.GetBaseException().Message), this._("Load Palette"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                        }
                    }
                    #endregion
                }
                else if (e.ClickedItem.Text.StartsWith(this._("Save Palette")))
                {
                    cmPalette.Close();
                    #region Save Palette
                    using (FileDialog dialog = new SaveFileDialog
                    {
                        Filter = PaletteSerializer.DefaultSaveFilter,
                        DefaultExt = "pal",
                        Title = this._("Save Palette File As")
                    })
                    {
                        if (dialog.ShowDialog(this) == DialogResult.OK)
                        {
                            IPaletteSerializer serializer;

                            serializer = PaletteSerializer.AllSerializers.Where(s => s.CanWrite).ElementAt(dialog.FilterIndex - 1);
                            if (serializer != null)
                            {
                                if (!serializer.CanWrite)
                                {
                                    throw new InvalidOperationException(this._("Serializer does not support writing palettes."));
                                }

                                try
                                {
                                    using (FileStream file = File.OpenWrite(dialog.FileName))
                                    {
                                        serializer.Serialize(file, colorGrid.Colors);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(string.Format(this._("Sorry, unable to save palette. {0}"), ex.GetBaseException().Message), this._("Save Palette"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                            else
                            {
                                MessageBox.Show(this._("Sorry, unable to save palette, the file format is not supported or is not recognized."), this._("Save Palette"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                    #endregion
                }
                else if (e.ClickedItem.Text.StartsWith(this._("Save Custom Palette")))
                {
                    cmPalette.Close();
                    #region Save Custom Palette
                    using (FileDialog dialog = new SaveFileDialog
                    {
                        Filter = PaletteSerializer.DefaultSaveFilter,
                        DefaultExt = "pal",
                        Title = this._("Save Palette File As")
                    })
                    {
                        if (dialog.ShowDialog(this) == DialogResult.OK)
                        {
                            IPaletteSerializer serializer;

                            serializer = PaletteSerializer.AllSerializers.Where(s => s.CanWrite).ElementAt(dialog.FilterIndex - 1);
                            if (serializer != null)
                            {
                                if (!serializer.CanWrite)
                                {
                                    throw new InvalidOperationException(this._("Serializer does not support writing palettes."));
                                }

                                try
                                {
                                    using (FileStream file = File.OpenWrite(dialog.FileName))
                                    {
                                        var custom = new ColorCollection(colorGrid.CustomColors.Skip(1));
                                        serializer.Serialize(file, custom);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show(string.Format(this._("Sorry, unable to save palette. {0}"), ex.GetBaseException().Message), this._("Save Custom Palette"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                                }
                            }
                            else
                            {
                                MessageBox.Show(this._("Sorry, unable to save palette, the file format is not supported or is not recognized."), this._("Save Custom Palette"), MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            }
                        }
                    }
                    #endregion
                }
                else if (e.ClickedItem.Text.StartsWith(this._("Reset Custom Palette")))
                {
                    cmPalette.Close();
                    colorGrid.CustomColors.Clear();
                    colorGrid.CustomColors.Add(this.Color);
                }
                else if (e.ClickedItem.Text.StartsWith("-"))
                {
                    return;
                }
                else
                {
                    foreach (var item in cmPalette.Items)
                    {
                        if (item.GetType() == typeof(ToolStripSeparator))
                        {
                            continue;
                        }
                        if (item != e.ClickedItem)
                        {
                            (item as ToolStripMenuItem).Checked = false;
                        }
                    }
                    if ((bool)e.ClickedItem.Tag)
                    {
                        var pal_name = e.ClickedItem.Text;
                        if (CustomPalette.ContainsKey(pal_name))
                        {
                            colorGrid.Colors = CustomPalette[pal_name];
                        }
                    }
                    else
                    {
                        colorGrid.Palette = (ColorPalette)Enum.Parse(typeof(ColorPalette), e.ClickedItem.Text);
                    }
                }
            }
        }