Freigeben über


Drehen von Farben

Die Drehung in einem vierdimensionalen Farbraum ist schwer zu visualisieren. Sie ist jedoch leichter vorstellbar, wenn vereinbart wird, dass ein Farbanteil stets einen festen Wert beibehält. Angenommen, der Alphaanteil behält einen festen Wert von 1 (vollständig deckend) bei. In diesem Fall lässt sich ein dreidimensionaler Farbraum mit Rot-, Grün- und Blauachsen, wie in der folgenden Abbildung, einfach visualisieren.

Eine Farbe kann man sich im 3D-Raum wie einen Punkt vorstellen. Der Punkt (1, 0, 0) stellt im Raum beispielsweise die Farbe Rot und der Punkt (0, 1, 0) die Farbe Grün dar.

Die folgende Abbildung veranschaulicht, welche Auswirkungen die Drehung der Farbe (1, 0, 0) in einem Winkel von 60 Grad in der Rot-Grün-Ebene hat. Die Drehung in einer Ebene parallel zur Rot-Grün-Ebene können Sie sich wie eine Drehung um die Blauachse vorstellen.

In der folgenden Abbildung wird veranschaulicht, wie Sie eine Farbmatrix initialisieren, um Drehungen um jede der drei Koordinatenachsen (Rot, Grün, Blau) auszuführen.

Im folgenden Beispiel wird eine 60-Grad-Drehung um die Blauachse auf ein komplett einfarbiges Bild (1, 0, 0.6) angewendet. In einer Ebene, die parallel zur Rot-Grün-Ebene verläuft, wird der Drehwinkel aufgehoben.

Dim image = New Bitmap("RotationInput.bmp")
Dim imageAttributes As New ImageAttributes()
Dim width As Integer = image.Width
Dim height As Integer = image.Height
Dim degrees As Single = 60F
Dim r As Double = degrees * System.Math.PI / 180 ' degrees to radians
Dim colorMatrixElements As Single()() = { _
   New Single() {CSng(System.Math.Cos(r)), _
                 CSng(System.Math.Sin(r)), 0, 0, 0}, _
   New Single() {CSng(- System.Math.Sin(r)), _
                 CSng(- System.Math.Cos(r)), 0, 0, 0}, _
   New Single() {0, 0, 2, 0, 0}, _
   New Single() {0, 0, 0, 1, 0}, _
   New Single() {0, 0, 0, 0, 1}}
      
Dim colorMatrix As New ColorMatrix(colorMatrixElements)
      
imageAttributes.SetColorMatrix( _
   colorMatrix, _
   ColorMatrixFlag.Default, _
   ColorAdjustType.Bitmap)
      
e.Graphics.DrawImage(image, 10, 10, width, height)
      
' Pass in the destination rectangle (2nd argument), the upper-left corner 
' (3rd and 4th arguments), width (5th argument),  and height (6th 
' argument) of the source rectangle.
e.Graphics.DrawImage( _
   image, _
   New Rectangle(150, 10, width, height), _
   0, 0, _
   width, _
   height, _
   GraphicsUnit.Pixel, _
   imageAttributes)
[C#]
Image image = new Bitmap("RotationInput.bmp");
ImageAttributes imageAttributes = new ImageAttributes();
int width = image.Width;
int height = image.Height;
float degrees = 60f;
double r = degrees*System.Math.PI/180; // degrees to radians

float[][] colorMatrixElements = { 
   new float[] {(float)System.Math.Cos(r),  (float)System.Math.Sin(r),  0,  0, 0},
   new float[] {(float)-System.Math.Sin(r),  (float)-System.Math.Cos(r),  0,  0, 0},
   new float[] {0,  0,  2,  0, 0},
   new float[] {0,  0,  0,  1, 0},
   new float[] {0, 0, 0, 0, 1}};

ColorMatrix colorMatrix = new ColorMatrix(colorMatrixElements);

imageAttributes.SetColorMatrix(
   colorMatrix, 
   ColorMatrixFlag.Default,
   ColorAdjustType.Bitmap);

e.Graphics.DrawImage(image, 10, 10, width, height);

e.Graphics.DrawImage(
   image, 
   new Rectangle(150, 10, width, height),  // destination rectangle 
   0, 0,        // upper-left corner of source rectangle 
   width,       // width of source rectangle
   height,      // height of source rectangle
   GraphicsUnit.Pixel,
   imageAttributes);

In der folgenden Abbildung ist das ursprüngliche Bild auf der linken und das Bild, dessen Farben gedreht wurden, auf der rechten Seite zu sehen.

In der folgenden Abbildung wird die im vorangehenden Code ausgeführte Farbdrehung veranschaulicht.