次の方法で共有


方法: 色を回転させる

4 次元の色空間での回転は、視覚化が困難です。 色コンポーネントの 1 つを固定したままにすることで、回転を簡単に視覚化できます。 アルファ コンポーネントを 1 (完全に不透明) で固定したままにすることに同意するとします。 次の図に示すように、赤、緑、青の軸で 3 次元の色空間を視覚化できます。

赤、緑、青の軸での回転を示す図。

色は、3D 空間のポイントと考えることができます。 たとえば、空間内の点 (1, 0, 0) は赤の色を表し、空間内の点 (0, 1, 0) は緑色を表します。

次の図は、Red-Green 平面で 60 度の角度で色 (1、0、0) を回転させる意味を示しています。 Red-Green 平面と平行な平面での回転は、青色の軸を中心とした回転と考えることができます。

青い軸の回転を示す図。

次の図は、カラー マトリックスを初期化して、3 つの座標軸 (赤、緑、青) のそれぞれの回転を実行する方法を示しています。

3 軸を中心に回転するようにカラー マトリックスを初期化します。

次の例では、すべて 1 色 (1、0、0.6) の画像を取得し、青色の軸を中心に 60 度回転を適用します。 回転の角度は、赤緑色の平面と平行な平面で描かれます。

次の図は、左側の元の画像と右の色で回転した画像を示しています。

元の画像と色で回転した画像を示す図。

次の図は、次のコードで実行される色の回転の視覚化を示しています。

色の回転の視覚化を示す図。

private void RotateColors(PaintEventArgs e)
{
    Bitmap 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);
}
Private Sub RotateColors(ByVal e As PaintEventArgs)
    Dim image As Bitmap = 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 = 60.0F
    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)
End Sub

コードのコンパイル

前の例は Windows フォームで使用できるように設計されており、PaintEventArgs イベント ハンドラーのパラメーターである ePaintが必要です。 RotationInput.bmpを、システムで有効なイメージ ファイル名とパスに置き換えます。

こちらも参照ください