次の方法で共有


色の回転

4 次元カラー空間における回転は、視覚的に認識するのが困難です。いずれかの色要素を固定したと想定することによって、回転を視覚的に認識しやすくすることができます。たとえば、アルファ要素を完全な不透明を示す 1 に固定したとします。これにより、次の図に示すように、赤、緑、および青の軸を持つ 3 次元のカラー空間を視覚化できます。

1 つの色は、3 次元空間内の 1 つの点として考えることができます。たとえば、空間内の点 (1, 0, 0) は赤の色を表し、空間内の点 (0, 1, 0) は緑の色を表します。

次の図は、赤軸と緑軸が構成する平面上で色 (1, 0, 0) を 60°回転させるとは、どういうことかを示しています。赤軸と緑軸が構成する平面と平行な平面における回転は、青軸を中心とした回転と考えることができます。

3 つの座標軸 (赤、緑、青) それぞれを中心として回転を実行するようにカラー行列を初期化する方法を次の図に示します。

次の図は、全体が 1 つの色 (1, 0, 0.6) であるイメージに、青軸を中心として 60°の回転を適用する例を示しています。回転の角度は、赤軸と緑軸が構成する平面と平行な平面上で内で測定されます。

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);

次の図は、左側に元のイメージ、右側に色の回転後のイメージを示しています。

上のコードで実行した色の回転を視覚化した図を次に示します。