// Spludlow Software // Copyright © Samuel P. Ludlow 2020 All Rights Reserved // Distributed under the terms of the GNU General Public License version 3 // Distributed WITHOUT ANY WARRANTY; without implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE // https://www.spludlow.co.uk/LICENCE.TXT // The Spludlow logo is a registered trademark of Samuel P. Ludlow and may not be used without permission // v1.14 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data; using System.Drawing; namespace Spludlow.Drawing { /// /// Provides some pre-defined CMYK colours for PDF printing /// /// You can add your own to the table if you like /// /// For the reference sheet run: Spludlow.Drawing.CMYKColours.PaletteSheet(@"D:\CMYK Colour Reference.pdf"); /// public class CMYKColours { public static DataTable Table = null; public static Dictionary Colours = null; static CMYKColours() { string filename = Spludlow.Config.ProgramData + @"\Data\CMYKColors.txt"; Table = Spludlow.Data.TextTable.ReadFile(filename); Colours = new Dictionary(); foreach (DataRow row in Table.Rows) { int c = (int)row["C"]; int m = (int)row["M"]; int y = (int)row["Y"]; int k = (int)row["K"]; string name = (string)row["Name"]; Color colour = Color.FromArgb(c, m, y, k); Colours.Add(name, colour); } } public static void PaletteSheet(string pdfFilename) { int columns = 8; Color black = Colours["Black"]; // Color.FromArgb(0, 0, 0, 100); using (PDF pdf = new PDF(pdfFilename, "A4")) { pdf.CMYK = true; pdf.Text("Spludlow CMYK Colour Reference", "Arial, 18, Bold, Underline", pdf.Width / 2, 10.0F, black, 0, StringAlignment.Center, 0, Color.Empty); float xOff = 10.0F; float yOff = 20.0F; float size = (pdf.Width - xOff * 2) / columns; float textHeight = size * 0.35F; for (int index = 0; index < Table.Rows.Count; ++index) { DataRow row = Table.Rows[index]; float xPos = xOff + ((index % columns) * size); float yPos = yOff + ((index / columns) * size); int c = (int)row["C"]; int m = (int)row["M"]; int y = (int)row["Y"]; int k = (int)row["K"]; double h = (double)row["H"]; double s = (double)row["S"]; double v = (double)row["V"]; string name = (string)row["Name"]; string text = name + Environment.NewLine + c + ", " + m + ", " + y + ", " + k + Environment.NewLine + (string)row["RGB"] + Environment.NewLine + h + ", " + s + ", " + v; Color colour = Colours[name]; // Color.FromArgb(c, m, y, k); pdf.Rectangle(xPos, yPos, size, size - textHeight, 0, Color.Empty, colour); pdf.TextBox(text, "Arial, 5, Bold", xPos, (yPos + size) - textHeight, size, textHeight, black, StringAlignment.Center, 0, Color.Empty); } } } public static void FixTable() // Used for tidying up the colours table from the original spread sheet, not for you { string filename = Spludlow.Config.ProgramData + @"\Data\CMYKColors.txt"; DataTable table = Spludlow.Data.TextTable.ReadFile(filename); foreach (DataRow row in table.Rows) { string name = (string)row["Name"]; int index; index = name.IndexOf("#"); if (index != -1) { row["Name"] = name.Substring(0, index - 1).Replace(" ", ""); row["NameRGB"] = name.Substring(index).Trim(); } string rgb = (string)row["RGB"]; rgb = rgb.ToUpper(); row["RGB"] = rgb; Color colour = Color.FromArgb(Int32.Parse(rgb.Substring(1), System.Globalization.NumberStyles.HexNumber)); double[] hsv = Spludlow.Drawing.Colour.ToHSV(colour); row["H"] = hsv[0]; row["S"] = hsv[1]; row["V"] = hsv[2]; } DataTable tempTable = table.Clone(); for (int pass = 0; pass < 3; ++pass) { DataView view = new DataView(table); view.Sort = "H"; switch (pass) { case 0: view.RowFilter = "Row = -1"; break; case 1: view.RowFilter = "Row > 0"; break; case 2: view.RowFilter = "Row = 0"; view.Sort = "K"; break; } foreach (DataRowView rowView in view) tempTable.ImportRow(rowView.Row); } table = tempTable; Spludlow.Data.TextTable.Write(filename, table); table = Spludlow.Data.TextTable.ReadFile(filename); Spludlow.Log.Report("CMYK Colours", table); } } }