如何:旋转颜色

在四维颜色空间中进行的旋转难以可视化。 通过协定将其中一个颜色组件保持固定不变,我们可以更轻松地可视化旋转。 假设我们一致协定将 alpha 组件固定在 1 处(完全不透明)。 然后,我们可以用红轴、绿轴和蓝轴来可视化一个三维颜色空间,如下图所示。

Illustration that shows rotation with red, green, and blue axes.

可以将一种颜色视为 3D 空间中的一个点。 例如,空间中的点 (1, 0, 0) 表示红色,空间中的点 (0, 1, 0) 表示绿色。

下图显示了在红-绿平面中通过 60 度的角度来旋转颜色 (1, 0, 0) 的意义。 可以将在与红-绿平面平行的平面中进行的旋转视为围绕蓝轴进行的旋转。

Illustration that shows rotation about the blue axis.

下图显示了如何初始化颜色矩阵来围绕三个坐标轴(红色、绿色和蓝色)的每一个轴进行旋转:

Initialize a color matrix to perform rotations about three axes.

示例

以下示例采用一张全单色 (1, 0, 0.6) 的图像,并围绕蓝轴应用 60 度的旋转。 旋转的角度在与红-绿平面平行的平面中扫过。

下图左侧显示原始图像,右侧显示颜色旋转后的图像:

Illustration that shows original image and color-rotated image.

下图显示了以下代码中执行的颜色旋转的可视化效果:

Illustration that shows the visualization of the color rotation.

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 窗体,它需要 PaintEventArgse,后者是 Paint 事件处理程序的参数。 将 RotationInput.bmp 替换为系统中有效的图像文件名称和路径。

另请参阅