如何:旋转颜色

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

显示使用红色、绿色和蓝色轴旋转的插图。

颜色可以视为三维空间中的点。 例如,空间中的点 (1, 0, 0) 表示红色,空间中的点 (0, 1, 0) 表示绿色。

下图显示了在 Red-Green 平面中通过 60 度的角度旋转颜色(1,0,0)的含义。 平面中与 Red-Green 平面平行的旋转可以视为绕蓝色轴旋转。

展示围绕蓝色轴旋转的插图。

下图展示了如何初始化颜色矩阵,以便绕三个坐标轴(红色、绿色、蓝色)中的每一个执行旋转:

初始化颜色矩阵以执行大约三个轴的旋转。

示例:

以下示例使用一种颜色 (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 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 用您系统上有效的映像文件名和路径替换RotationInput.bmp

另请参阅