Procedimiento para rotar colores
La rotación en un espacio de colores de cuatro dimensiones es difícil de visualizar. Podemos facilitar la visualización de la rotación si mantenemos fijo uno de los componentes de color. Supongamos que mantenemos fijo el componente alfa en 1 (totalmente opaco). Después, podemos visualizar un espacio de colores tridimensional con los ejes rojo, verde y azul, como se muestra en la ilustración siguiente.
Un color se puede considerar un punto en el espacio tridimensional. Por ejemplo, el punto (1, 0, 0) en el espacio representa el color rojo, mientras que el punto (0, 1, 0) en el espacio representa el color verde.
En la ilustración siguiente se muestra lo que significa girar el color (1, 0, 0) con un ángulo de 60 grados en el plano rojo-verde. La rotación en un plano paralelo al plano rojo-verde se puede considerar una rotación sobre el eje azul.
En la ilustración siguiente se muestra cómo inicializar una matriz de colores para realizar rotaciones sobre cada uno de los tres ejes de coordenadas (rojo, verde, azul):
Ejemplo
En el ejemplo siguiente se toma una imagen que es todo un color (1, 0, 0.6) y se aplica una rotación de 60 grados sobre el eje azul. El ángulo de la rotación se arrastra en un plano paralelo al plano rojo-verde.
En la ilustración siguiente se muestra la imagen original a la izquierda y la imagen con el color girado a la derecha:
En la ilustración siguiente se muestra una visualización de la rotación de colores realizada en el código siguiente:
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
Compilar el código
El ejemplo anterior está diseñado para su uso con Windows Forms y requiere PaintEventArgse
, que es un parámetro del controlador de eventos Paint. Reemplace RotationInput.bmp
por un nombre de archivo de imagen y una ruta de acceso válidos en el sistema.
Consulte también
.NET Desktop feedback
Comentarios
https://aka.ms/ContentUserFeedback.
Próximamente: A lo largo de 2024 iremos eliminando gradualmente GitHub Issues como mecanismo de comentarios sobre el contenido y lo sustituiremos por un nuevo sistema de comentarios. Para más información, vea:Enviar y ver comentarios de