1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
|
/// <summary>
/// Class to replace the colors of a black and white Bitmap with specified colors.
/// </summary>
public class BitmapGradientColorReplacer
{
/// <summary>
/// Lower gradient <see cref="Color"/> to replace.
/// </summary>
private Color LowerColor { get; set; }
/// <summary>
/// Upper gradient <see cref="Color"/> to replace.
/// </summary>
private Color UpperColor { get; set; }
/// <summary>
/// Destination gradient <see cref="Color"/> to for the lower gradient color.
/// </summary>
public Color ReplaceLowerColor { get; set; }
/// <summary>
/// Destination gradient <see cref="Color"/> to for the upper gradient color.
/// </summary>
public Color ReplaceUpperColor { get; set; }
public BitmapGradientColorReplacer()
{
// Später könnte man auch mit anderen Farben als Grundwerte arbeiten.
// Allerdings muss dafür noch die GetPercentalColorValue angepasst werden.
LowerColor = Color.FromArgb(0, 0, 0);
UpperColor = Color.FromArgb(255, 255, 255);
}
/// <summary>
/// Replaces all pixel colors in the <see cref="Bitmap"/> object with the specified gradient colors.
/// </summary>
/// <param name="bitmap">The <see cref="Bitmap"/> object for replace the colors.</param>
/// <exception cref="NullReferenceException"></exception>
/// <exception cref="ArgumentException"></exception>
/// <exception cref="OverflowException"></exception>
/// <exception cref="Exception"></exception>
public void Replace(Bitmap bitmap)
{
if (ReplaceLowerColor == null)
{
throw new NullReferenceException("ReplaceLowerColor must not be null. Please set the destination gradient color for the lower color.");
}
if (ReplaceUpperColor == null)
{
throw new NullReferenceException("ReplaceUpperColor must not be null. Please set the destination gradient color for the upper color.");
}
if (LowerColor == null)
{
throw new NullReferenceException("LowerColor must not be null. Please set the lower gradient color to replace.");
}
if (UpperColor == null)
{
throw new NullReferenceException("UpperColor must not be null. Please set the upper gradient color to replace.");
}
if (bitmap == null)
{
// Man kann auch eine Exception werfen, oder einfach die Methode übergehen.
// throw new ArgumentNullException("bitmap");
return;
}
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = 0; y < bitmap.Height; y++)
{
Color pixelColor = bitmap.GetPixel(x, y);
bitmap.SetPixel(x, y, GetPercentalColorValue(pixelColor));
}
}
}
private Color GetPercentalColorValue(Color color)
{
// Von dem übergebenen Farbwert wird für jeden RGB Farbanteil der prozentuale Anteil
// anhand der unteren gradient und oberen Gradientfarbe berechnet.
// Dieser Prozentsatz für jeden einzelnen Anteil wird benötigt, um den Farbwert nach
// der Zwischenfarbe den zu ersetzenden Gradientfarben zu übertragen.
double rPercental = GetPercentage(color.R - UpperColor.R, LowerColor.R - UpperColor.R);
double gPercental = GetPercentage(color.G - UpperColor.G, LowerColor.G - UpperColor.G);
double bPercental = GetPercentage(color.B - UpperColor.B, LowerColor.B - UpperColor.B);
// Es werden für jeden RGB Farbanteil anhand der entsprechenden Prozentsätze von
// den Zielgradientfarben die neuen Prozentwerte für den Farbwert berechnet.
int newRedValue = Math.Abs(Convert.ToInt32(ReplaceUpperColor.R + GetPercentageValue((ReplaceLowerColor.R - ReplaceUpperColor.R), rPercental)));
int newGreenValue = Math.Abs(Convert.ToInt32(ReplaceUpperColor.G + GetPercentageValue((ReplaceLowerColor.G - ReplaceUpperColor.G), rPercental)));
int newBlueValue = Math.Abs(Convert.ToInt32(ReplaceUpperColor.B + GetPercentageValue((ReplaceLowerColor.B - ReplaceUpperColor.B), rPercental)));
return Color.FromArgb(color.A, newRedValue, newGreenValue, newBlueValue);
}
private double GetPercentage(double percentageValue, double baseValue)
{
return percentageValue * 100 / baseValue;
}
private double GetPercentageValue(double baseValue, double percentage)
{
return baseValue * Math.Abs(percentage) / 100;
}
|