Feedback

C# - Schwarzweiß Bilder mit einem Farbverlauf füllen

Veröffentlicht von am 5/20/2010
(0 Bewertungen)
Mit dieser Klasse kann man vorzüglich schwarzweiß Bitmaps mit einem Farbverlauf von 2 Farben füllen.
Man kann z.B. schwarz zu rot umwandeln und weiß zu grün, wenn einzelne Farbwerte im Bitmap Grautöne haben, werden sie zu einer Mischung aus rot und grün umgerechnet.

Besonders nützlich ist die Klasse dann, wenn man eine Anwendungsoberfläche mit einem Farbschema bestehend aus 2 Farben hat und man diese Farben beliebig austauschen und somit unterschiedliche Skins ermöglichen möchte.

Beispiel zum Einsetzen der Klasse:

Bitmap bitmap = Bitmap.FromFile("Bild.bmp");

BitmapGradientColorReplacer bgcr = new BitmapGradientColorReplacer();

bgcr.ReplaceLowerColor = Color.FromArgb(255, 0, 0);
bgcr.ReplaceUpperColor = Color.FromArgb(0, 255, 0);

bgcr.Replace(bitmap);


Hier ein Screenshot aus dem Beispiel:
http://www.imgbox.de/users/public/images/D6gtecHwO9.JPG
/// <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;
        }
Abgelegt unter Farbverlauf, Bild, Farben, Skins, Farbschema.

Kommentare zum Snippet

 

Logge dich ein, um hier zu kommentieren!