public SortedDictionary <int, PaletteItem> LoadClusters() { SortedDictionary <int, PaletteItem> clusters = null; using (SqlConnection conn = this.GetConnection()) { SqlCommand cmd = new SqlCommand(@"SELECT c.PALETTE_ID, c.RGB FROM PALETTE_COLOR c WHERE c.PALETTE_ID < 0 ORDER BY c.PALETTE_ID, c.[ORDER]", conn); SqlDataReader r = cmd.ExecuteReader(); int id = 1; Color[] colors = new Color[32]; int p = 0; while (r.Read()) { int x = r.GetInt32(0); if (x != id) { if (id < 0) { if (clusters == null) { clusters = new SortedDictionary <int, PaletteItem>(); } clusters.Add(id, new PaletteItem(id, null, id, colors, p)); } id = x; p = 0; } colors[p++] = PaletteManager.ColorFromInt(r.GetInt32(1)); } if (id < 0) { if (clusters == null) { clusters = new SortedDictionary <int, PaletteItem>(); } clusters.Add(id, new PaletteItem(id, null, id, colors, p)); } r.Close(); r = null; } return(clusters); }
public int[] Find(int color, int count) { List <FindColorInfo> list = new List <FindColorInfo>(); using (SqlConnection conn = this.GetConnection()) { LabColor lab; SqlCommand cmd = new SqlCommand(PaletteManager.ColorSearchSql(color, ColorSearchWidth.Narrow, out lab), conn); SqlDataReader r = cmd.ExecuteReader(); while (r.Read()) { list.Add(new FindColorInfo((int)r[0], lab.Distance(new LabColor((float)(double)r[1], (float)(double)r[2], (float)(double)r[3])))); } r.Close(); r = null; cmd = null; } if (list.Count == 0) { return(null); } list.Sort(this.comparer); int l = count; if (l > list.Count) { l = count; } int[] found = new int[l]; for (int i = 0; i < l; i++) { found[i] = list[i].Rgb; } list = null; return(found); }
public PaletteItem Load(int paletteId) { PaletteItem pi = new PaletteItem(); pi.Id = -1; using (SqlConnection conn = this.GetConnection()) { string sql = @"SELECT p.ID, p.[DESCRIPTION], c.RGB, p.CLUSTER FROM PALETTE_COLOR c, PALETTE p WHERE (p.ID = c.PALETTE_ID) AND (p.ID = " + paletteId.ToString() + @") ORDER BY p.ID, c.[ORDER]"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader r = cmd.ExecuteReader(); Color[] colors = new Color[32]; int p = 0; while (r.Read()) { int x = r.GetInt32(0); if (x != pi.Id) { pi.Id = x; p = 0; pi.Title = r.GetString(1); if (r.IsDBNull(3)) { pi.Cluster = 0; } else { pi.Cluster = r.GetInt32(3); } } colors[p++] = PaletteManager.ColorFromInt(r.GetInt32(2)); } r.Close(); if (pi.Id > 0) { pi = new PaletteItem(pi.Id, pi.Title, pi.Cluster, colors, p); } } return(pi); }
public Color[] FindColor(Color color, ColorSearchWidth width) { SortedDictionary <int, int> l = new SortedDictionary <int, int>(); using (SqlConnection conn = this.GetConnection()) { this.FindAddColors(conn, l, color.ToArgb() & 0xffffff, width, 0x1); } if (l.Count == 0) { return(null); } Color[] r = new Color[l.Count]; int i = 0; foreach (KeyValuePair <int, int> p in l) { r[i++] = PaletteManager.ColorFromInt(p.Key); } l = null; return(r); }
private void FindAddColors(SqlConnection conn, SortedDictionary <int, int> list, int color, ColorSearchWidth width, int mask) { LabColor lab; string sql = PaletteManager.ColorSearchSql(color, width, out lab); SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader r = cmd.ExecuteReader(); while (r.Read()) { int c = r.GetInt32(0); if (!list.ContainsKey(c)) { list.Add(c, mask); } else { list[c] |= mask; } } r.Close(); r = null; cmd = null; }
public int CreatePalette(int colors, int colorCountLimit, int maxDimension, string title, RawImage image, IClustering clustering, int thumbnailSize) { RawImage img = image.ResizeTo(maxDimension); int[] vector = img.Vector(colorCountLimit); clustering.SetPoints(RawImage.ToColor3Array(vector, this, 0)); Color3[] p = RawImage.ToColor3Array(vector, this, colors); for (int j = 0; j < p.Length; j++) { clustering.AddCluster(p[j]); } clustering.Start(); clustering.Run(); Color[] palette = PaletteManager.LabSort(RawImage.FromColor3Array(clustering.Clusters(colors), this), Color.Black); int id = this.AddPalette(title); for (int i = 0; i < palette.Length; i++) { this.AddColor(palette[i]); this.AddPaletteColor(id, palette[i].ToArgb() & 0xffffff, i + 1, (i + 1) >= palette.Length); } if (thumbnailSize > 0) { img = img.ResizeTo(thumbnailSize); this.AddPaletteThumbnail(id, img.Jpeg(80), 2048); img.Clear(); img = null; } return(id); }
private static string ColorSearchSql(int color, ColorSearchWidth width, out LabColor lab) { lab = ColorTransform.RgbToLab(PaletteManager.ColorFromInt(color)); StringBuilder sb = new StringBuilder(); int w = (int)width; sb.Append(@"SELECT c.RGB, c.L, c.A, c.B FROM COLOR c, COLOR_INDEX_L l, COLOR_INDEX_A a, COLOR_INDEX_B b WHERE (l.RGB = c.RGB) AND (a.RGB = c.RGB) AND (b.RGB = l.RGB)"); sb.Append("AND (l.[HASH] "); int h = lab.HashL; if (w > 0) { sb.Append("IN ("); int c = 0; for (int i = -w; i <= w; i++) { if (c++ > 0) { sb.Append(','); } sb.Append((h + i).ToString()); } sb.Append(')'); } else { sb.Append("= "); sb.Append(h.ToString()); } sb.Append(')'); sb.Append(" AND (a.[HASH] "); h = lab.HashA; if (w > 0) { sb.Append("IN ("); int c = 0; for (int i = -w; i <= w; i++) { if (c++ > 0) { sb.Append(','); } sb.Append((h + i).ToString()); } sb.Append(')'); } else { sb.Append("= "); sb.Append(h.ToString()); } sb.Append(')'); sb.Append(" AND (b.[HASH] "); h = lab.HashB; if (w > 0) { sb.Append("IN ("); int c = 0; for (int i = -w; i <= w; i++) { if (c++ > 0) { sb.Append(','); } sb.Append((h + i).ToString()); } sb.Append(')'); } else { sb.Append("= "); sb.Append(h.ToString()); } sb.Append(')'); return(sb.ToString()); }
private void AddForColors(SqlConnection conn, SortedDictionary <int, PaletteItem> list, string colorList, SortedDictionary <int, int> colorIndex, int mask, int maxCount, ref int count) { string sql = @"SELECT p.ID, p.[DESCRIPTION], c.RGB, p.CLUSTER FROM PALETTE_COLOR c, PALETTE p WHERE (p.ID = c.PALETTE_ID) AND (p.ID IN (SELECT DISTINCT pc.PALETTE_ID FROM PALETTE_COLOR pc WHERE (pc.RGB IN (" + colorList + @")))) ORDER BY p.ID, c.[ORDER]"; SqlCommand cmd = new SqlCommand(sql, conn); SqlDataReader r = cmd.ExecuteReader(); int id = -1; string title = null; int cluster = -1; Color[] colors = new Color[32]; int p = 0; bool add = false; while (r.Read()) { int x = r.GetInt32(0); if (x != id) { if (title != null) { if (add) { PaletteItem pal = new PaletteItem(id, title, cluster, colors, p); if (PaletteManager.MatchMask(pal.Colors, colorIndex, mask)) { list.Add(id, pal); ++count; if (count >= maxCount) { break; } } } } id = x; if (list.ContainsKey(id)) { add = false; p = 0; title = null; } else { add = true; p = 0; title = r.GetString(1); if (r.IsDBNull(3)) { cluster = 0; } else { cluster = r.GetInt32(3); } } } if (add) { colors[p++] = PaletteManager.ColorFromInt(r.GetInt32(2)); } } if (title != null) { if (add && (count < maxCount)) { PaletteItem pal = new PaletteItem(id, title, cluster, colors, p); if (PaletteManager.MatchMask(pal.Colors, colorIndex, mask)) { list.Add(id, pal); ++count; } } } r.Close(); r = null; }