Код: Выделить всё
public static void AdjustImageAttributes(Image image, float brightness, float saturation, float transparency)
{
//adapted from https://stackoverflow.com/a/14384449/9852011
// Luminance vector for linear RGB
const float rwgt = 0.3086f;
const float gwgt = 0.6094f;
const float bwgt = 0.0820f;
// Create a new color matrix
ColorMatrix colorMatrix = new ColorMatrix();
// Adjust saturation
float baseSat = 1.0f - saturation;
colorMatrix[0, 0] = baseSat * rwgt + saturation;
colorMatrix[0, 1] = baseSat * rwgt;
colorMatrix[0, 2] = baseSat * rwgt;
colorMatrix[1, 0] = baseSat * gwgt;
colorMatrix[1, 1] = baseSat * gwgt + saturation;
colorMatrix[1, 2] = baseSat * gwgt;
colorMatrix[2, 0] = baseSat * bwgt;
colorMatrix[2, 1] = baseSat * bwgt;
colorMatrix[2, 2] = baseSat * bwgt + saturation;
// Adjust brightness
float adjustedBrightness = brightness - 1f;
colorMatrix[4, 0] = adjustedBrightness;
colorMatrix[4, 1] = adjustedBrightness;
colorMatrix[4, 2] = adjustedBrightness;
colorMatrix[3, 3] = transparency;
// Create image attributes
ImageAttributes imageAttributes = new ImageAttributes();
imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
// Draw the image with the new color matrix
using (Graphics g = Graphics.FromImage(image))
{
g.DrawImage(image, new Rectangle(0, 0, image.Width, image.Height),
0, 0, image.Width, image.Height,
GraphicsUnit.Pixel, imageAttributes);
}
}
, что возвращает Colormatrix с желаемым вращением Hue. /p>
ColorMatrix GetHueShiftColorMax(Single hueShiftDegrees)
{
/* Return the matrix
A00 A01 A02 0 0
A10 A11 A12 0 0
A20 A21 A22 0 0
0 0 0 1 0
0 0 0 0 1
*/
Single theta = hueShiftDegrees/360 * 2*pi; //Degrees --> Radians
Single c = cos(theta);
Single s = sin(theta);
Single A00 = 0.213 + 0.787*c - 0.213*s;
Single A01 = 0.213 - 0.213*c + 0.413*s;
Single A02 = 0.213 - 0.213*c - 0.787*s;
Single A10 = 0.715 - 0.715*c - 0.715*s;
Single A11 = 0.715 + 0.285*c + 0.140*s;
Single A12 = 0.715 - 0.715*c + 0.715*s;
Single A20 = 0.072 - 0.072*c + 0.928*s;
Single A21 = 0.072 - 0.072*c - 0.283*s;
Single A22 = 0.072 + 0.928*c + 0.072*s;
ColorMatrix cm = new ColorMatrix(
( A00, A01, A02, 0, 0 ),
( A10, A11, A12, 0, 0 ),
( A20, A21, A22, 0, 0 ),
( 0, 0, 0, 0, 0 ),
( 0, 0, 0, 0, 1 )
)
return cm;
}
< /code>
Так что теперь у меня в основном две цветные матрицы. Что я мог бы сделать, так это перерисовать изображение с первой функцией, а затем создать другую, аналогичную функцию только для оттенка и перерисовать его снова. Однако это кажется мне ненужным. Я чувствую, что должен быть способ объединить 2 матрицы в 1 и сделать все с одним перерисованием. Математика в этих функциях, к сожалению, над моей головой, поэтому я понятия не имею, с чего начать.
Подробнее здесь: https://stackoverflow.com/questions/793 ... of-a-bitma