(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) ); } } } |