<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace Colours
{
    /// <summary>
    /// Represents a color palette, using the GIMP's format as the backend.
    /// </summary>
    [DataContract]
    public class Palette
    {
        const string magic = "GIMP Palette";

        /// <summary>
        /// Gets or sets the name of the palette.
        /// </summary>
        [DataMember]
        public string Name { get; set; }
        /// <summary>
        /// Gets or sets how many columns will the palette have if it is
        /// being displayed in a grid. If the amount of columns are 0, then
        /// the grid's logic will choose how many columns it wants.
        /// </summary>
        [DataMember]
        public int Columns { get; set; }
        /// <summary>
        /// Gets or sets a list of colors.
        /// </summary>
        [DataMember]
        public List<PaletteColor> Colors { get; set; }
        /// <summary>
        /// Gets or sets comments in a palette.
        /// </summary>
        [DataMember]
        public List<string> Comments { get; set; }

        /// <summary>
        /// Creates an empty palette.
        /// </summary>
        public Palette()
        {
            Colors = new List<PaletteColor>();
            Name = "Untitled";
            Comments = new List<string>();
            Columns = 0;
        }

        /// <summary>
        /// Creates a new palette from an existing one.
        /// </summary>
        /// <param name="p">The palette to use.</param>
        public Palette(Palette p)
        {
            Name = p.Name;
            Columns = p.Columns;
            Colors = new List<PaletteColor>();
            Comments = new List<string>();
            // simply copying the list doesn't make it deep but a fill
            // deep copy would make the colors different; not desirable
            foreach (var pc in p.Colors)
                Colors.Add(pc);
            foreach (var c in p.Comments)
                Comments.Add(c);
        }

        /// <summary>
        /// Creates a new color palette from a GIMP palette file.
        /// </summary>
        /// <param name="file">The file itself.</param>
        public Palette(string file) :
            this(file.Split(Environment.NewLine.ToCharArray()))
        { }

        /// <summary>
        /// Creates a new color palette from a GIMP palette file.
        /// </summary>
        /// <param name="file">The file itself, as lines.</param>
        public Palette(string[] file)
        {
            if (file[0] != magic)
                throw new Exception("Palette is not in a valid format.");

            Colors = new List<PaletteColor>();
            Comments = new List<string>();

            foreach (string l in file)
            {
                if (l == magic || l == "#") continue;
                if (l.StartsWith("# "))
                {
                    // TODO: handle comments without space
                    Comments.Add(l.Remove(0, 2));
                }
                else if (l.StartsWith("Columns: "))
                {
                    int i = 0; // a default
                    int.TryParse(l.Remove(0, "Columns: ".Length), out i);
                    Columns = i;
                }
                else if (l.StartsWith("Name: "))
                {
                    Name = l.Remove(0, "Name: ".Length);
                }
                else
                {
                    // i guess we'll try to coax some colours out of it
                    Colors.Add(new PaletteColor(l));
                }
            }
        }

        /// <summary>
        /// Creates a new color palette from scratch.
        /// </summary>
        /// <param name="name">The name of the palette.</param>
        /// <param name="columns">The amount of columns.</param>
        /// <param name="colors">The list of colors.</param>
        public Palette(string name, int columns, IEnumerable<RgbColor> colors)
        {
            Name = name;
            Columns = columns;
            Colors = colors
                        .Select(c => new PaletteColor(c, "Untitled"))
                        .ToList();
        }

        /// <summary>
        /// Creates a new color palette from scratch.
        /// </summary>
        /// <param name="name">The name of the palette.</param>
        /// <param name="columns">The amount of columns.</param>
        /// <param name="comments">The list of comments.</param>
        /// <param name="colors">The list of colors.</param><commit_msg>use regexp to handle comments and columns in palettes<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Text.RegularExpressions;

namespace Colours
{
    /// <summary>
    /// Represents a color palette, using the GIMP's format as the backend.
    /// </summary>
    [DataContract]
    public class Palette
    {
        const string magic = "GIMP Palette";

        const string nameRegex = "Name: ?(.*)";
        const string columnsRegex = @"Columns: ?(\d*)";
        const string commentRegex = @"#\s*(.*)";

        /// <summary>
        /// Gets or sets the name of the palette.
        /// </summary>
        [DataMember]
        public string Name { get; set; }
        /// <summary>
        /// Gets or sets how many columns will the palette have if it is
        /// being displayed in a grid. If the amount of columns are 0, then
        /// the grid's logic will choose how many columns it wants.
        /// </summary>
        [DataMember]
        public int Columns { get; set; }
        /// <summary>
        /// Gets or sets a list of colors.
        /// </summary>
        [DataMember]
        public List<PaletteColor> Colors { get; set; }
        /// <summary>
        /// Gets or sets comments in a palette.
        /// </summary>
        [DataMember]
        public List<string> Comments { get; set; }

        /// <summary>
        /// Creates an empty palette.
        /// </summary>
        public Palette()
        {
            Colors = new List<PaletteColor>();
            Name = "Untitled";
            Comments = new List<string>();
            Columns = 0;
        }

        /// <summary>
        /// Creates a new palette from an existing one.
        /// </summary>
        /// <param name="p">The palette to use.</param>
        public Palette(Palette p)
        {
            Name = p.Name;
            Columns = p.Columns;
            Colors = new List<PaletteColor>();
            Comments = new List<string>();
            // simply copying the list doesn't make it deep but a fill
            // deep copy would make the colors different; not desirable
            foreach (var pc in p.Colors)
                Colors.Add(pc);
            foreach (var c in p.Comments)
                Comments.Add(c);
        }

        /// <summary>
        /// Creates a new color palette from a GIMP palette file.
        /// </summary>
        /// <param name="file">The file itself.</param>
        public Palette(string file) :
            this(file.Split(Environment.NewLine.ToCharArray()))
        { }

        /// <summary>
        /// Creates a new color palette from a GIMP palette file.
        /// </summary>
        /// <param name="file">The file itself, as lines.</param>
        public Palette(string[] file)
        {
            if (file[0] != magic)
                throw new Exception("Palette is not in a valid format.");

            Colors = new List<PaletteColor>();
            Comments = new List<string>();

            foreach (string l in file)
            {
                if (l == magic || l == "#") continue;
                if (Regex.IsMatch(l, commentRegex))
                {
                    Comments.Add(Regex.Match(l, commentRegex).Groups[1].Value);
                }
                else if (Regex.IsMatch(l, columnsRegex))
                {
                    int i = 0; // a default
                    int.TryParse(Regex.Match(l,
                        columnsRegex).Groups[1].Value, out i);
                    Columns = i;
                }
                else if (Regex.IsMatch(l, nameRegex))
                {
                    Name = Regex.Match(l, nameRegex).Groups[1].Value;
                }
                else
                {
                    // i guess we'll try to coax some colours out of it
                    Colors.Add(new PaletteColor(l));
                }
            }
        }

        /// <summary>
        /// Creates a new color palette from scratch.
        /// </summary>
        /// <param name="name">The name of the palette.</param>
        /// <param name="columns">The amount of columns.</param>
        /// <param name="colors">The list of colors.</param>
        public Palette(string name, int columns, IEnumerable<RgbColor> colors)
        {
            Name = name;
            Columns = columns;
            Colors = colors
                        .Select(c => new PaletteColor(c, "Untitled"))
                        .ToList();
        }

        /// <summary>
        /// Creates a new color palette from scratch.
        /// </summary>
        /// <param name="name">The name of the palette.</param>
        /// <param name="columns">The amount of columns.</param>
        /// <param name="comments">The list of comments.</param>
        /// <param name="colors">The list of colors.</param>
{'fraction_non_alphanumeric': 0.12472510210493246, 'fraction_numerical': 0.0015708451146716933, 'mean_word_length': 1.3609394313967862, 'pattern_counts': {'":': 0, '<': 105, '<?xml version=': 0, '>': 107, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1659461', 'n_tokens_mistral': 2520, 'n_tokens_neox': 2394, 'n_words': 796}