public static void CopyPixels(this BitmapSource source, ColorRGBA[,] pixels, int stride, int offset)
 {
     var height = source.PixelHeight;
             var width = source.PixelWidth;
             var pixelBytes = new byte[height * width * 4];
             source.CopyPixels(pixelBytes, stride, 0);
             int y0 = offset / width;
             int x0 = offset - width * y0;
             for (int y = 0; y < height; y++)
                 for (int x = 0; x < width; x++)
                     pixels[x + x0, y + y0] = new ColorRGBA
                     {
                         Blue = pixelBytes[(y * width + x) * 4 + 0],
                         Green = pixelBytes[(y * width + x) * 4 + 1],
                         Red = pixelBytes[(y * width + x) * 4 + 2],
                         Alpha = pixelBytes[(y * width + x) * 4 + 3],
                     };
 }
        public static ColorRGBA[,] GetPixels(BitmapSource source)
        {
            int height = source.PixelHeight;
            int width = source.PixelWidth;
            ColorRGBA[,] result = new ColorRGBA[width, height];

            int nStride = (source.PixelWidth * source.Format.BitsPerPixel + 7) / 8;
            byte[] pixelByteArray = new byte[source.PixelHeight * nStride];
            source.CopyPixels(pixelByteArray, nStride, 0);
            ColorRGBA[] pixelColors = GetPixels(pixelByteArray, source.Format).ToArray();
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int index = y*width + x;
                    result[x, y] = pixelColors[index];
                }
            }
            //BitmapUtils.CopyPixels(source, result, width * 4, 0);
            return result;
        }
        // http://stackoverflow.com/a/147426/25216
        private static ColorRGBA FromHSLA(double H, double S, double L, double A)
        {
            double v;
            double r, g, b;
            if (A > 1.0)
                A = 1.0;

            r = L;   // default to gray
            g = L;
            b = L;
            v = (L <= 0.5) ? (L * (1.0 + S)) : (L + S - L * S);
            if (v > 0)
            {
                double m;
                double sv;
                int sextant;
                double fract, vsf, mid1, mid2;

                m = L + L - v;
                sv = (v - m) / v;
                H *= 6.0;
                sextant = (int)H;
                fract = H - sextant;
                vsf = v * sv * fract;
                mid1 = m + vsf;
                mid2 = v - vsf;
                switch (sextant)
                {
                    case 0:
                        r = v;
                        g = mid1;
                        b = m;
                        break;
                    case 1:
                        r = mid2;
                        g = v;
                        b = m;
                        break;
                    case 2:
                        r = m;
                        g = v;
                        b = mid1;
                        break;
                    case 3:
                        r = m;
                        g = mid2;
                        b = v;
                        break;
                    case 4:
                        r = mid1;
                        g = m;
                        b = v;
                        break;
                    case 5:
                        r = v;
                        g = m;
                        b = mid2;
                        break;
                }
            }
            ColorRGBA rgb = new ColorRGBA();
            rgb.Red = Convert.ToByte(r * 255.0f);
            rgb.Green = Convert.ToByte(g * 255.0f);
            rgb.Blue = Convert.ToByte(b * 255.0f);
            rgb.Alpha = Convert.ToByte(A * 255.0f);
            return rgb;
        }
        public static void PutPixels(WriteableBitmap bitmap, ColorRGBA[,] pixels)
        {
            int width = pixels.GetLength(0);
            int height = pixels.GetLength(1);

            byte[] bytes = new byte[pixels.Length*4];
            for(int y = 0; y < height; ++y)
            {
                for(int x = 0; x < width; x+=1)
                {
                    var index = (y * width + x)*4;
                    bytes[index] = pixels[x, y].Blue;
                    bytes[index+1] = pixels[x,y].Green;
                    bytes[index+2] = pixels[x,y].Red;
                    bytes[index + 3] = pixels[x, y].Alpha;
                }
            }

            Int32Rect rect = new Int32Rect(0, 0, bitmap.PixelWidth, bitmap.PixelHeight);
            bitmap.WritePixels(rect, bytes, width * 4, 0, 0);
        }
 public static ColorHSL FromRGBA(ColorRGBA colorRGBA)
 {
     System.Drawing.Color color = System.Drawing.Color.FromArgb(colorRGBA.Alpha, colorRGBA.Red, colorRGBA.Green, colorRGBA.Red);
     return FromSystemDrawingColor(color);
 }