예제 #1
0
        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);
        }
예제 #2
0
        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);
        }
예제 #3
0
        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);
        }
예제 #4
0
        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());
        }
예제 #5
0
        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;
        }