방법: 색 회전
업데이트: 2007년 11월
4차원 색 공간에서 회전을 시각적으로 표시하기는 어렵습니다. 색 구성 요소 중 하나가 고정되어 있다고 가정하면 좀 더 쉽게 회전을 시각적으로 나타낼 수 있습니다. 알파 구성 요소를 1(완전 불투명)로 고정한다고 가정합니다. 그러면 아래 그림과 같이 빨강, 녹색 및 파랑 구성 요소에 대한 축을 가진 3차원 색 공간을 시각적으로 나타낼 수 있습니다.
색을 3차원 공간에 있는 점으로 생각할 수 있습니다. 예를 들어, 3차원 공간에서 점 (1, 0, 0)은 빨강을 나타내며 점 (0, 1, 0)은 녹색을 나타냅니다.
아래 그림에서는 색 (1, 0, 0)을 빨강-녹색 평면에서 60도 회전하면 어떻게 되는지를 보여 줍니다. 빨강-녹색 평면과 평행한 평면에서 회전하는 것은 파랑 구성 요소의 축을 중심으로 회전하는 것과 같습니다.
아래 그림에서는 세 좌표축(빨강, 녹색, 파랑) 각각을 중심으로 회전하기 위해 색 매트릭스를 초기화하는 방법을 보여 줍니다.
예제
아래 예제에서는 모두 같은 색(1, 0, 0.6)으로 되어 있는 이미지에 파랑 요소의 축을 중심으로 60도 회전하는 변환을 적용합니다. 회전은 빨강-녹색 평면과 평행한 평면에서 이루어집니다.
아래 그림에서 왼쪽은 원래 이미지이고 오른쪽은 색 회전을 적용한 이미지입니다.
다음 그림에서는 아래 코드에서 수행되는 색 회전을 시각적으로 보여 줍니다.
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
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);
}
코드 컴파일
앞의 예제는 Windows Forms에서 사용해야 하며 Paint 이벤트 처리기의 매개 변수인 PaintEventArgse를 필요로 합니다. RotationInput.bmp를 시스템에서 사용할 수 있는 이미지 파일 이름 및 경로로 바꾸십시오.