Example #1
0
        public int AddColor(Color color)
        {
            int      rgb = (color.ToArgb() & 0xffffff);
            LabColor lab = ColorTransform.RgbToLab(color);

            using (SqlConnection conn = this.GetConnection())
            {
                SqlCommand cmd = new SqlCommand("UP_ADD_COLOR", conn);
                cmd.CommandType = System.Data.CommandType.StoredProcedure;

                cmd.Parameters.Add("@rgb", System.Data.SqlDbType.Int).Value    = rgb;
                cmd.Parameters.Add("@l", System.Data.SqlDbType.Float).Value    = Math.Round(lab.L, 6);
                cmd.Parameters.Add("@a", System.Data.SqlDbType.Float).Value    = Math.Round(lab.A, 6);
                cmd.Parameters.Add("@b", System.Data.SqlDbType.Float).Value    = Math.Round(lab.B, 6);
                cmd.Parameters.Add("@l_hash", System.Data.SqlDbType.Int).Value = lab.HashL;
                cmd.Parameters.Add("@a_hash", System.Data.SqlDbType.Int).Value = lab.HashA;
                cmd.Parameters.Add("@b_hash", System.Data.SqlDbType.Int).Value = lab.HashB;

                cmd.ExecuteNonQuery();

                cmd = null;
            }

            return(rgb);
        }
Example #2
0
        public static Color HlsToRgb(HlsColor hls)
        {
            double h = (double)hls.H / 100f;
            double l = (double)hls.L / 1000f;
            double s = (double)hls.S / 1000f;

            double p2;

            if (l <= 0.5f)
            {
                p2 = l * (1f + s);
            }
            else
            {
                p2 = l + s - l * s;
            }
            double p1 = 2f * l - p2;

            if (s == 0f)
            {
                int c = (int)(l * 255f);
                return(Color.FromArgb(c, c, c));
            }
            return(Color.FromArgb(
                       (int)(ColorTransform.QqhToRgb(p1, p2, h + 120f) * 255f),
                       (int)(ColorTransform.QqhToRgb(p1, p2, h) * 255f),
                       (int)(ColorTransform.QqhToRgb(p1, p2, h - 120f) * 255f)
                       ));
        }
Example #3
0
        public static Color[] LabDistanceSort(Color[] colors, Color color)
        {
            if (colors == null)
            {
                return(colors);
            }
            if (colors.Length == 0)
            {
                return(colors);
            }
            SortedDictionary <ColorIndex, Color> l = new SortedDictionary <ColorIndex, Color>();
            int i;

            for (i = 0; i < colors.Length; i++)
            {
                l.Add(new ColorIndex(ColorTransform.LabDistance(color, colors[i]), i), colors[i]);
            }
            Color[] r = new Color[colors.Length];
            i = 0;
            foreach (KeyValuePair <ColorIndex, Color> p in l)
            {
                r[i++] = p.Value;
            }
            return(r);
        }
Example #4
0
        public RawImage Resample(int factor)
        {
            switch (factor)
            {
            case 2:
            case 3:
            case 4:
                break;

            default:
                throw new ArgumentException("Invalid resample factor");
            }

            int w = this.Width / factor;
            int h = this.Height / factor;

            int[] dest = new int[w * h];

            int p = 0;

            int[]  pp = new int[4];
            double l;
            double a;
            double b;

            for (int i = 0; i < h; i++)
            {
                int x = (i * factor) * this.Width;
                for (int k = 0; k < factor; k++)
                {
                    pp[k] = x;
                    x    += this.Width;
                }
                for (int j = 0; j < w; j++)
                {
                    int count = 0;
                    l = a = b = 0f;
                    for (int k = 0; k < factor; k++)
                    {
                        for (int m = 0; m < factor; m++)
                        {
                            LabColor lab = ColorTransform.RgbToLab(Color.FromArgb(this.Pixels[pp[k]]));
                            l += lab.L;
                            a += lab.A;
                            b += lab.B;
                            pp[k]++;
                            ++count;
                        }
                    }

                    dest[p++] = ColorTransform.LabToRgb(new LabColor(
                                                            l / (double)count,
                                                            a / (double)count,
                                                            b / (double)count)).ToArgb() & 0xffffff;
                }
            }
            return(new RawImage(w, h, dest));
        }
Example #5
0
        private static ClusteringPoint ToPoint(Color[] colors)
        {
            ClusteringPoint cp = new ClusteringPoint(24);
            int             i  = 0;

            for (int j = 0; j < colors.Length; j++)
            {
                LabColor lc = ColorTransform.RgbToLab(colors[j]);
                cp[i++] = lc.L;
                cp[i++] = lc.A;
                cp[i++] = lc.B;
                if (i >= cp.Size)
                {
                    break;
                }
            }
            return(cp);
        }
Example #6
0
        private static Color[] LabSort(Color[] colors, Color origin)
        {
            List <LabColor> l = new List <LabColor>(colors.Length);

            for (int i = 0; i < colors.Length; i++)
            {
                l.Add(ColorTransform.RgbToLab(colors[i]));
            }

            l.Sort(new PaletteColorComparer(origin));

            Color[] c = new Color[l.Count];
            for (int i = 0; i < l.Count; i++)
            {
                c[i] = ColorTransform.LabToRgb(l[i]);
            }
            return(c);
        }
Example #7
0
        public static Color DarkColor(Color color, int percent)
        {
            HlsColor hls = ColorTransform.RgbToHls(color);

            float l = (float)hls.L / 1000f;

            l = l - ((l * (float)percent) / 100f);
            if (l < 0f)
            {
                l = 0f;
            }
            if (l > 1f)
            {
                l = 1f;
            }
            hls.L = (short)(l * 1000f);

            return(ColorTransform.HlsToRgb(hls));
        }
Example #8
0
        public void UpdatePaletteClusters(ClusteringPoint[] clusters, ClusteringPoint[] points)
        {
            if (clusters == null)
            {
                return;
            }

            using (SqlConnection conn = this.GetConnection())
            {
                SqlCommand cmd = new SqlCommand(@"DELETE FROM PALETTE_COLOR WHERE PALETTE_ID < 0", conn);
                cmd.ExecuteNonQuery();
                cmd = null;

                cmd = new SqlCommand(@"UPDATE PALETTE SET CLUSTER = NULL", conn);
                cmd.ExecuteNonQuery();
                cmd = null;

                for (int i = 0; i < clusters.Length; i++)
                {
                    ClusteringPoint p     = clusters[i];
                    int             j     = 0;
                    int             order = 0;
                    while (j < p.Size)
                    {
                        int rgb = ColorTransform.LabToRgb(new LabColor(p[j++], p[j++], p[j++])).ToArgb() & 0xffffff;
                        ++order;
                        cmd = new SqlCommand(@"INSERT INTO [PALETTE_COLOR] ([PALETTE_ID],[ORDER],[RGB]) VALUES
(-" + (i + 1).ToString() + "," + order.ToString() + "," + rgb.ToString() + ")", conn);
                        cmd.ExecuteNonQuery();
                        cmd = null;
                    }
                }

                for (int i = 0; i < points.Length; i++)
                {
                    int id      = points[i].Id;
                    int cluster = -(points[i].Index + 1);
                    cmd = new SqlCommand(@"UPDATE PALETTE SET CLUSTER = " + cluster.ToString() + " WHERE ID = " + id.ToString(), conn);
                    cmd.ExecuteNonQuery();
                    cmd = null;
                }
            }
        }
Example #9
0
        public static HsvColor RgbToHsv(Color rgb)
        {
            double r = (double)rgb.R / 255f;
            double g = (double)rgb.G / 255f;
            double b = (double)rgb.B / 255f;

            double min    = ColorTransform.FMin(ColorTransform.FMin(r, g), b);
            double max    = ColorTransform.FMax(ColorTransform.FMax(r, g), b);
            double chroma = max - min;

            double h = 0f;
            double s = 0f;

            //If Chroma is 0, then S is 0 by definition, and H is undefined but 0 by convention.
            if (chroma != 0f)
            {
                if (r == max)
                {
                    h = (g - b) / chroma;

                    if (h < 0.0)
                    {
                        h += 6.0;
                    }
                }
                else if (g == max)
                {
                    h = ((b - r) / chroma) + 2.0f;
                }
                else
                {
                    h = ((r - g) / chroma) + 4.0f;
                }

                h *= 60.0f;
                s  = chroma / max;
            }

            return(new HsvColor(h, s, max));
        }
Example #10
0
        public static Color FindLabNearest(Color color, Color[] colors)
        {
            if (colors == null)
            {
                return(color);
            }
            if (colors.Length == 0)
            {
                return(color);
            }
            Color  c    = colors[0];
            double diff = ColorTransform.LabDistance(color, c);

            for (int i = 1; i < colors.Length; i++)
            {
                double d = ColorTransform.LabDistance(color, colors[i]);
                if (d < diff)
                {
                    diff = d;
                    c    = colors[i];
                }
            }
            return(c);
        }
        private const double ref_Z = 108.883f; //ref_Z = 108.883

        public static LabColor RgbToLab(Color rgb)
        {
            return(ColorTransform.XyzToLab(ColorTransform.RgbToXyz(rgb)));
        }
Example #12
0
        public RawImage ResizeTo(int maxDimension)
        {
            if ((this.Width <= maxDimension) && (this.Height <= maxDimension))
            {
                return(this);
            }

            double scaleX = (double)this.Width / (double)maxDimension;
            double scaleY = (double)this.Height / (double)maxDimension;

            scaleX = scaleX > scaleY ? scaleX : scaleY;

            int w = (int)((double)this.Width / scaleX);
            int h = (int)((double)this.Height / scaleX);

            scaleX = (double)this.Width / (double)(w + 1);
            int[] mapX = new int[w + 1];
            for (int i = 0; i <= w; i++)
            {
                mapX[i] = (int)(scaleX * (double)i);
            }
            mapX[w] = this.Width;

            scaleY = (double)this.Height / (double)(h + 1);
            int[] mapY = new int[h + 1];
            for (int i = 0; i <= h; i++)
            {
                mapY[i] = (int)(scaleY * (double)i);
            }
            mapY[h] = this.Height;

            int    p = 0;
            double l;
            double a;
            double b;

            int[] dest = new int[w * h];

            for (int i = 0; i < h; i++)
            {
                int y0 = mapY[i];
                int y1 = mapY[i + 1];
                for (int j = 0; j < w; j++)
                {
                    int x0    = mapX[j];
                    int x1    = mapX[j + 1];
                    int count = 0;
                    l = a = b = 0f;

                    for (int ii = y0; ii < y1; ii++)
                    {
                        for (int jj = x0; jj < x1; jj++)
                        {
                            int      c   = this.Pixels[(ii * this.Width) + jj];
                            LabColor lab = ColorTransform.RgbToLab(Color.FromArgb(c));
                            l += lab.L;
                            a += lab.A;
                            b += lab.B;
                            ++count;
                        }
                    }

                    if (count > 0)
                    {
                        dest[p] = ColorTransform.LabToRgb(new LabColor(
                                                              l / (double)count,
                                                              a / (double)count,
                                                              b / (double)count)).ToArgb() & 0xffffff;
                    }
                    ++p;
                }
            }

            return(new RawImage(w, h, dest));
        }
Example #13
0
        public static List <Color> CreateGradient(Color start, Color end, int steps, ColorGradientFlags flags, double saturation)
        {
            HsvColor c0 = ColorTransform.RgbToHsv(start);
            HsvColor c1 = ColorTransform.RgbToHsv(end);

            int value = 0;

            if ((flags & ColorGradientFlags.MinValue) != 0)
            {
                value = (c0.V < c1.V) ? c0.V : c1.V;
            }
            else if ((flags & ColorGradientFlags.MaxValue) != 0)
            {
                value = (c0.V < c1.V) ? c1.V : c0.V;
            }
            else
            {
                value = (c0.V + c1.V) >> 1;
            }

            int h = c0.H;
            int a = c1.H - h;

            if (a < 0)
            {
                a = a + 36000;
            }
            int step = 0;

            if ((flags & ColorGradientFlags.CounterClockwise) == 0)
            {
                step = (int)((double)(a) / (double)(steps - 1));
            }
            else
            {
                step = -(int)((double)(36000 - a) / (double)(steps - 1));
            }

            int s     = c0.S;
            int sstep = 0;

            if ((flags & ColorGradientFlags.ReplaceSaturation) != 0)
            {
                s = (int)(saturation * 1000f);
            }
            else if ((flags & ColorGradientFlags.AlignSaturation) == 0)
            {
                sstep = (int)((double)(c1.S - s) / (double)steps);
            }

            List <Color> l = new List <Color>();

            for (int i = 0; i < steps; i++)
            {
                l.Add(ColorTransform.HsvToRgb(new HsvColor(h, s, value)));
                s += sstep;
                h += step;
                if (h < 0)
                {
                    h = h + 36000;
                }
                else if (h > 36000)
                {
                    h = h - 36000;
                }
            }

            return(l);
        }
Example #14
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());
        }
Example #15
0
 public Color FromColor3(Color3 color)
 {
     return(ColorTransform.LabToRgb(new LabColor(color.A, color.B, color.C)));
 }
Example #16
0
 public Color3 ToColor3(Color color)
 {
     return(new Color3(ColorTransform.RgbToLab(color)));
 }
Example #17
0
 public PaletteColorComparer(Color rgb)
 {
     this.origin = ColorTransform.RgbToLab(rgb);
 }
Example #18
0
 public static double ColorDistance(Color x, Color y)
 {
     return(ColorTransform.RgbToLab(x).Distance(ColorTransform.RgbToLab(y)));
 }
 public static Color LabToRgb(LabColor lab)
 {
     return(ColorTransform.XyzToRgb(ColorTransform.LabToXyz(lab)));
 }