Extension of the GDI+ struct ColorMatrix.
 /// <summary>
 /// X and Y are the indices of the value to receive the sin(phi) value
 /// </summary>
 /// <param name="phi">phi is in degrees</param>
 /// <param name="x">x value</param>
 /// <param name="y">y value</param>
 /// <param name="order">Matrix order</param>
 private void RotateColor(float phi, int x, int y, MatrixOrder order)
 {
     phi *= RAD;
     var qm = new QColorMatrix();
     qm._matrix[x, x] = qm._matrix[y, y] = (float)Math.Cos(phi);
     var s = (float)Math.Sin(phi);
     qm._matrix[y, x] = s;
     qm._matrix[x, y] = -s;
     this.Multiply(qm, order);
 }
 private void ShearColor(int x, int y1, float d1, int y2, float d2, MatrixOrder order)
 {
     var qm = new QColorMatrix();
     qm._matrix[y1, x] = d1;
     qm._matrix[y2, x] = d2;
     this.Multiply(qm, order);
 }
 /// <summary>
 /// Constructor specifying matrix data
 /// </summary>
 /// <param name="matrixData">Matrix data to initialize with</param>
 public QColorMatrix(QColorMatrix matrixData)
 {
     Copy(matrixData);
 }
 private void Copy(QColorMatrix qm)
 {
     if (qm == null)
     {
         this.Reset();
         return;
     }
     Copy(qm._matrix);
 }
 /// <summary>
 /// Set the saturation of the matrix. Saturation of 0.0f yields black and white, 1.0f is neutral.
 /// </summary>
 /// <param name="saturation">Saturation value</param>
 /// <param name="order">Matrix order</param>
 public void SetSaturation(float saturation, MatrixOrder order)
 {
     float satCompl = 1.0f - saturation;
     float satComplR = LUM_R * satCompl;
     float satComplG = LUM_G * satCompl;
     float satComplB = LUM_B * satCompl;
     var tm = new[,]
     {
         {satComplR + saturation,	satComplR,	satComplR,	0.0f, 0.0f} ,
         {satComplG,	satComplG + saturation,	satComplG,	0.0f, 0.0f},
         {satComplB,	satComplB,	satComplB + saturation,	0.0f, 0.0f},
         {0.0f,	0.0f,	0.0f,	1.0f,	0.0f},
         {0.0f,	0.0f,	0.0f,	0.0f,	1.0f}
     };
     var qm = new QColorMatrix(tm);
     this.Multiply(qm, order);
 }
 /// <summary>
 /// Update this matrix with the product of itself and a translation vector.
 /// </summary>
 /// <param name="offsetRed">Red offset value</param>
 /// <param name="offsetGreen">Green offset value</param>
 /// <param name="offsetBlue">Blue offset value</param>
 /// <param name="offsetOpacity">Alpha offset value</param>
 /// <param name="order">Matrix order</param>
 public void Translate(float offsetRed, float offsetGreen, float offsetBlue,
     float offsetOpacity, MatrixOrder order)
 {
     var qm = new QColorMatrix();
     qm._matrix[4, 0] = offsetRed;
     qm._matrix[4, 1] = offsetGreen;
     qm._matrix[4, 2] = offsetBlue;
     qm._matrix[4, 3] = offsetOpacity;
     this.Multiply(qm, order);
 }
 /// <summary>
 /// Update this matrix with the product of itself and a scaling vector.
 /// </summary>
 /// <param name="scaleRed">Red scaling value</param>
 /// <param name="scaleGreen">Green scaling value</param>
 /// <param name="scaleBlue">Blue scaling value</param>
 /// <param name="scaleOpacity">Alpha scaling value</param>
 /// <param name="order">Matrix order</param>
 public void Scale(float scaleRed, float scaleGreen, float scaleBlue,
     float scaleOpacity, MatrixOrder order)
 {
     var qm = new QColorMatrix();
     qm._matrix[0, 0] = scaleRed;
     qm._matrix[1, 1] = scaleGreen;
     qm._matrix[2, 2] = scaleBlue;
     qm._matrix[3, 3] = scaleOpacity;
     this.Multiply(qm, order);
 }
        /// <summary>
        /// Multiply the given matrix using the specified matrix order.
        /// </summary>
        /// <param name="matrix">Matrix to multiply</param>
        /// <param name="order">Order to multiply with</param>
        public void Multiply(QColorMatrix matrix, MatrixOrder order)
        {
            if (matrix == null) throw new ArgumentException();
            float[,] a, b;
            if (order == MatrixOrder.MatrixOrderAppend)
            {
                a = matrix._matrix;
                b = this._matrix;
            }
            else
            {
                a = this._matrix;
                b = matrix._matrix;
            }

            var temp = new float[MATRIX_LENGTH, MATRIX_LENGTH];
            for (int y = 0; y < MATRIX_LENGTH; y++)
            {
                for (int x = 0; x < MATRIX_LENGTH; x++)
                {
                    float t = 0;
                    for (int i = 0; i < MATRIX_LENGTH; i++)
                    {
                        t += b[y, i] * a[i, x];
                    }
                    temp[y, x] = t;
                }
            }
            for (int y = 0; y < MATRIX_LENGTH; y++)
            {
                for (int x = 0; x < MATRIX_LENGTH; x++)
                {
                    this._matrix[y, x] = temp[y, x];
                }
            }
        }
 /// <summary>
 /// Multiplies the given matrix
 /// </summary>
 /// <param name="matrix">Matrix to multiply</param>
 public void Multiply(QColorMatrix matrix)
 {
     this.Multiply(matrix, MatrixOrder.MatrixOrderPrepend);
 }
Beispiel #10
0
 /// <summary>
 /// Rotates the hue of an image
 /// </summary>
 /// <param name="image">Image to change</param>
 /// <param name="hue">Degree of hue displacement (0..360)</param>
 /// <remarks>Values out of range will be automatically corrected</remarks>
 public static void RotateHue(Image image, int hue)
 {
     using (var newImage = new Bitmap(image))
     {
         using (var g = Graphics.FromImage(image))
         {
             var imageAttr = new ImageAttributes();
             var qm = new QColorMatrix();
             qm.RotateHue(hue % 360);
             imageAttr.SetColorMatrix(qm.ToColorMatrix());
             var destRect = new Rectangle(new Point(), image.Size);
             g.Clear(Color.Transparent);
             g.DrawImage(newImage, destRect, 0, 0, image.Width, image.Height,
                 GraphicsUnit.Pixel, imageAttr);
         }
     }
 }
Beispiel #11
0
 /// <summary>
 /// Changes the opacity of an image. 
 /// </summary>
 /// <param name="image">Image to change</param>
 /// <param name="opacity">Opacity change to apply (0..255)</param>
 /// <remarks>Values out of range will be automatically corrected</remarks>
 public static void ChangeOpacity(Image image, int opacity)
 {
     using (var newImage = new Bitmap(image))
     {
         using (var g = Graphics.FromImage(image))
         {
             var imageAttr = new ImageAttributes();
             var qm = new QColorMatrix();
             qm.ScaleOpacity(opacity.Clamp(0, 255) / 255.0f);
             imageAttr.SetColorMatrix(qm.ToColorMatrix());
             var destRect = new Rectangle(new Point(), image.Size);
             g.Clear(Color.Transparent);
             g.DrawImage(newImage, destRect, 0, 0, image.Width, image.Height,
                 GraphicsUnit.Pixel, imageAttr);
         }
     }
 }