Search This Blog

Title of the confirm dialog

Description of what is about to happen

Showing posts with label interpolation. Show all posts
Showing posts with label interpolation. Show all posts

Bitmap Color Replacement

Replace specific bitmap pixels color with specific intensity(0~100%) and/or limited alpha  into new defined color. First get bitmap pixels by width and height by an iteration then do process of pixel color replacement.


List of functions

○ Replace Color.
○ Replace Non Transparent Color.
○ Replace Non Transparent ColorA.
○ Set Replace All Color Bitmap (2 overloads).
○ Set Replace Color Bitmap(2 overloads).

Class Lerp is needed and can be viewed here


Source Code


  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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
using System;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;

namespace TechScales
{
    internal static class BitmapExtensions
    {
        /// <summary>
        /// Replace color with limited alpha and specific intensity
        /// </summary>
        /// <param name="source"></param>
        /// <param name="to"></param>
        /// <param name="minAlphaReplace"></param>
        /// <param name="intensity"></param>
        /// <returns></returns>
        internal static Bitmap SetReplaceAllColorBitmap(
            this Bitmap source, Color to, byte minAlphaReplace = 0, float intensity = .25f)
        {
            if (source == null) { return null; }
            if (intensity < 0.1f && intensity > 1f) { intensity = 1f; }
            Bitmap bm = new Bitmap(source);
            for (int w = 0; w < source.Width - 1; w++)
            {
                for (int h = 0; h < source.Height - 1; h++)
                {
                    Color c = bm.GetPixel(w, h);
                    if (c.A > minAlphaReplace)
                    {
                        c = Color.FromArgb(c.A, intensity == 1f ? to : c.Lerp(to, intensity));
                        bm.SetPixel(w, h, c);
                    }
                }
            }
            return bm;
        }
        /// <summary>
        /// Replace color with specific intensity
        /// </summary>
        /// <param name="source"></param>
        /// <param name="to"></param>
        /// <param name="minAlphaReplace"></param>
        /// <param name="intensity"></param>
        /// <returns></returns>
        internal static Bitmap SetReplaceAllColorBitmap(
            this Bitmap source, Color to, float intensity)
        {
            return SetReplaceAllColorBitmap(source, to, 0, intensity);
        }
        /// <summary>
        /// Replace all color into new color with specific intensity
        /// </summary>
        /// <param name="source">gambar</param>
        /// <param name="col">warna baru</param>
        /// <param name="intensity"></param>
        /// <returns></returns>
        internal static Bitmap SetReplaceColorBitmap(
            this Bitmap source, Color col, float intensity = 1f)
        {
            if (source == null) { return null; }
            if (intensity < 0.1f && intensity > 1f) { intensity = 1f; }
            Bitmap bm = new Bitmap(source);
            for (int w = 0; w < source.Width - 1; w++)
            {
                for (int h = 0; h < source.Height - 1; h++)
                {
                    Color c = bm.GetPixel(w, h);
                    if (c.A > 0)
                    {
                        c = Color.FromArgb(c.A, intensity == 1f ? col : c.Lerp(col, intensity)); 
                        bm.SetPixel(w, h, c);
                    }
                }
            }
            return bm;
        }
        /// <summary>
        /// Replace specific color into new color with specific intensity
        /// </summary>
        /// <param name="source">gambar</param>
        /// <param name="from">warna diganti</param>
        /// <param name="to">watrna pengganti</param>
        /// <param name="intensity">intensitas</param>
        /// <returns></returns>
        internal static Bitmap SetReplaceColorBitmap(
            this Bitmap source, Color @from, Color to, float intensity = 1f)
        {
            if (source == null) { return null; }
            if (from.Equals(to)) { return source;                  }
            if (intensity < 0.1f && intensity > 1f) { intensity = 1f; }
            Bitmap bm = new Bitmap(source);
            for (int w = 0; w < source.Width - 1; w++)
            {
                for (int h = 0; h < source.Height - 1; h++)
                {
                    Color c = bm.GetPixel(w, h);
                    if (c.R == from.R && c.G == from.B && c.B == from.B)
                    {
                        c = Color.FromArgb(c.A, intensity == 1f ? to : c.Lerp(to, intensity));
                        bm.SetPixel(w, h, c);
                    }
                }
            }
            return bm;
        }
        private static unsafe Bitmap ReplaceNonTransparentColorA(
            Bitmap source,
            Color replacement,
            byte alphaLimitMinim = 5)
        {
            const int PIXEL_SIZE = 4;                   // 32 bits per pixel
            var target = new Bitmap(source.Width, source.Height, PixelFormat.Format32bppArgb);
            BitmapData sourceData = null, targetData = null;

            try
            {
                sourceData = source.LockBits(
                  new Rectangle(0, 0, source.Width, source.Height),
                  ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

                targetData = target.LockBits(
                  new Rectangle(0, 0, target.Width, target.Height),
                  ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb);

                for (int y = 0; y < source.Height; ++y)
                {
                    byte* sourceRow = (byte*)sourceData.Scan0 + (y * sourceData.Stride);
                    byte* targetRow = (byte*)targetData.Scan0 + (y * targetData.Stride);

                    for (int x = 0; x < source.Width; ++x)
                    {
                        byte b = sourceRow[x * PIXEL_SIZE + 0];
                        byte g = sourceRow[x * PIXEL_SIZE + 1];
                        byte r = sourceRow[x * PIXEL_SIZE + 2];
                        byte a = sourceRow[x * PIXEL_SIZE + 3];

                        r = replacement.R;
                        g = replacement.G;
                        b = replacement.B;

                        targetRow[x * PIXEL_SIZE + 0] = b;
                        targetRow[x * PIXEL_SIZE + 1] = g;
                        targetRow[x * PIXEL_SIZE + 2] = r;
                        targetRow[x * PIXEL_SIZE + 3] = a;
                    }
                }
            }
            finally
            {
                if (sourceData != null) { source.UnlockBits(sourceData); }
                if (targetData != null) { target.UnlockBits(targetData); }
            }
            return target;
        }
        internal static Bitmap ReplaceNonTransparentColor(this 
            Bitmap source,
            Color replacement,
            byte alphaLimitMinim = 5)
        {
            return ReplaceNonTransparentColorA(source, replacement, alphaLimitMinim);
        }
        internal static Bitmap ReplaceColor(
            this Bitmap source, Color @from, Color to, float intensity = 1f)
        {
            return SetReplaceColorBitmap(source, from, to, intensity);
        }
    }
}

Test

Result
1.   2.   3.   4.   5.   6.
Code
1. Bitmap bm = original bitmap
2. bm.ReplaceColor(Color.Black, Color.DodgerBlue, 0.85f);
3. bm.ReplaceNonTransparentColor(Color.Green, 5);
4. bm.SetReplaceAllColorBitmap(Color.DodgerBlue, 0, 0.25f);
5. bm.SetReplaceAllColorBitmap(Color.Yellow, 0.65f);
6. bm.SetReplaceAllColorBitmap(Color.Black, Color.Red, 0.85f);

TOP

RGB Color Linear Interpolation

Sometimes some cases, color (C0) with its RGB values, need to be changed to a new color (Cn), based from another color (C1) with a certain amount of percentage (0 to 100%) from C1, linearly.

(C0 » C1 (n%) = Cn)

The process of converting color, from one color that influenced by another color into a new color, we name it 'Color Linear Interpolation (Lerp)'


Source Code

 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 
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;

namespace TechScales
{ 
    static class ColorExtension
    {
        internal static float Lerp(this float start, float end, float amount)
        {
            float difference = end - start;
            float adjusted = difference * amount;
            return (start + adjusted);
        }
        /// <summary>
        /// linear interpolation
        /// </summary>
        /// <param name="colour"></param>
        /// <param name="to" >Color To</param>
        /// <param name="amount">0 ~ 1.0</param>
        /// <returns></returns>
        /// <remarks></remarks> 
        internal static Color Lerp(this Color colour, Color to, float amount)
        {
            // start color
            float r0 = (float)colour.R,
                  g0 = (float)colour.G,
                  b0 = (float)colour.B;
            // end color
            float r1 = (float)to.R,
                  g1 = (float)to.G,
                  b1 = (float)to.B;
            // lerp the color
            byte r = (byte)(r0.Lerp(r1, amount)),
                 g = (byte)(g0.Lerp(g1, amount)),
                 b = (byte)(b0.Lerp(b1, amount));
            // return the new color
            return Color.FromArgb(r, g, b);
        }
        internal static T DeepClone<T>(this T a)
        {
            using (var stream = new System.IO.MemoryStream())
            {
                var formatter = new System.Runtime.Serialization
                                .Formatters.Binary.BinaryFormatter();
                formatter.Serialize(stream, a);
                stream.Position = 0;
                return (T)formatter.Deserialize(stream);
            }
        }
        /// <summary>
        /// simple inversion of color
        /// </summary>
        /// <param name="from"></param>
        /// <returns></returns>
        private static Color InvertA(this Color from)
        {
            return Color.FromArgb(from.A, 255 - from.R, 255 - from.G, 255 - from.B);
        }
        /// <summary>
        /// simple inversion of color, to black or white
        /// </summary>
        /// <param name="from"></param>
        /// <param name="toBlackOrWhiteColor"></param>
        /// <returns></returns>
        internal static Color Invert(this Color from, bool toBlackOrWhiteColor = true)
        {
            if (!toBlackOrWhiteColor) { return from.InvertA(); }
            return (GetBrightness(from) > 130 ? Color.Black : Color.White);
        }
        private static int GetBrightness(Color c)
        {
            return 
            (int)Math.Sqrt(c.R * c.R * .299 + c.G * c.G * .587 + c.B * c.B * .114);
        }
        /*
         * another way
         */ 
        static Color LerpRGB(Color a, Color b, float t)
        {
            return Color.FromArgb
            (
                a.A + (int)((b.A - a.A) * t),
                a.R + (int)((b.R - a.R) * t),
                a.G + (int)((b.G - a.G) * t),
                a.B + (int)((b.B - a.B) * t)
            );
        }
    }
}

Test

Immediate Window
?System.Drawing.Color.Blue.Lerp(System.Drawing.Color.Red, 0.25f);
 "{Name=ff3f00bf, ARGB=(255, 63, 0, 191)}"
    A: 255
    B: 191
    G: 0
    IsEmpty: false
    IsKnownColor: false
    IsNamedColor: false
    IsSystemColor: false
    Name: "ff3f00bf"
    R: 63
 
?System.Drawing.Color.Blue.Lerp(System.Drawing.Color.Yellow, 0.25f);
 "{Name=ff3f3fbf, ARGB=(255, 63, 63, 191)}"
    A: 255
    B: 191
    G: 63
    IsEmpty: false
    IsKnownColor: false
    IsNamedColor: false
    IsSystemColor: false
    Name: "ff3f3fbf"
    R: 63
 

var newColor = Color.Blue.Lerp(Color.Cyan, 0.85f); "{Name=ff00d8ff, ARGB=(255, 0, 216, 255)}" A: 255 B: 255 G: 216 IsEmpty: false IsKnownColor: false IsNamedColor: false IsSystemColor: false Name: "ff00d8ff" R: 0 lerp blue cyan .085f

TOP