Пример #1
0
        private static ColorRgb888 GetClosestColor(ColorRgb888 col, int depth)
        {
            int invDepth = 8 - depth;

            return(new ColorRgb888
            {
                R = (byte)((col.R >> invDepth) << invDepth),
                G = (byte)((col.G >> invDepth) << invDepth),
                B = (byte)((col.B >> invDepth) << invDepth),
            });
        }
Пример #2
0
        private static Dictionary <ColorRgb888, Bucket <ColorRgb888> > GenBuckets(byte[] bgrData, int colorDepth)
        {
            var graph = new Dictionary <ColorRgb888, Bucket <ColorRgb888> >();

            for (var i = 0; i < bgrData.Length; i += 3)
            {
                var c = new ColorRgb888
                {
                    B = bgrData[i + 0],
                    G = bgrData[i + 1],
                    R = bgrData[i + 2],
                };
                var d = GetClosestColor(c, colorDepth);
                if (!graph.ContainsKey(d))
                {
                    graph[d] = new Bucket <ColorRgb888>();
                }
                graph[d].Elements.Add(c);
            }
            return(graph);
        }
Пример #3
0
        private static void ToHsl(this ColorRgb888 rgb, out double h, out double s, out double l)
        {
            const double EPSILON = 1.0 / 255;

            var    r      = EPSILON * rgb.R;
            var    g      = EPSILON * rgb.G;
            var    b      = EPSILON * rgb.B;
            var    max    = Math.Max(Math.Max(r, g), b);
            var    min    = Math.Min(Math.Min(r, g), b);
            var    chroma = max - min;
            double h1;

            if (Math.Abs(chroma) < EPSILON)
            {
                h1 = 0;
            }
            else if (Math.Abs(max - r) < EPSILON)
            {
                h1 = ((g - b) / chroma) % 6;
            }
            else if (Math.Abs(max - g) < EPSILON)
            {
                h1 = 2 + (b - r) / chroma;
            }
            else //if (max == b)
            {
                h1 = 4 + (r - g) / chroma;
            }

            var lightness  = 0.5 * (max - min);
            var saturation = Math.Abs(chroma) < EPSILON
                                 ? 0
                                 : chroma / (1 - Math.Abs(2 * lightness - 1));

            h = 60 * h1;
            s = saturation;
            l = lightness;
        }
Пример #4
0
 public bool Equals(ColorRgb888 other)
 {
     return R == other.R && G == other.G && B == other.B;
 }
Пример #5
0
 private static ColorRgb888 GetClosestColor(ColorRgb888 col, int depth)
 {
     int invDepth = 8 - depth;
     return new ColorRgb888
     {
         R = (byte) ((col.R >> invDepth) << invDepth),
         G = (byte) ((col.G >> invDepth) << invDepth),
         B = (byte) ((col.B >> invDepth) << invDepth),
     };
 }
Пример #6
0
 private static Dictionary<ColorRgb888, Bucket<ColorRgb888>> GenBuckets(byte[] bgrData, int colorDepth)
 {
     var graph = new Dictionary<ColorRgb888, Bucket<ColorRgb888>>();
     for (var i = 0; i < bgrData.Length; i += 3)
     {
         var c = new ColorRgb888
         {
             B = bgrData[i + 0],
             G = bgrData[i + 1],
             R = bgrData[i + 2],
         };
         var d = GetClosestColor(c, colorDepth);
         if (!graph.ContainsKey(d))
             graph[d] = new Bucket<ColorRgb888>();
         graph[d].Elements.Add(c);
     }
     return graph;
 }
Пример #7
0
 public bool Equals(ColorRgb888 other)
 {
     return(R == other.R && G == other.G && B == other.B);
 }