/// <summary> /// Make an interpolation from c1 to c2 /// </summary> /// <param name="c1">The left color (interp == 0.0). Set to (59, 76, 192) for cold color</param> /// <param name="c2">The right color (interp == 1.0). Set to (180, 4, 38) for warm color</param> /// <param name="interp">The interpolation value between 0.0 and 1.0</param> public void FromColorInterpolation(Color c1, Color c2, float interp) { MSHColor m1 = new MSHColor(c1); MSHColor m2 = new MSHColor(c2); FromColorInterpolation(m1, m2, interp); }
/// <summary> /// Copy constructor /// </summary> /// <param name="copy">The color to copy</param> public MSHColor(MSHColor copy) { M = copy.M; S = copy.S; H = copy.H; A = copy.A; }
/// <summary> /// Adjust the Hue /// </summary> /// <param name="color">The saturated color</param> /// <param name="m">The unsaturated M component</param> /// <returns></returns> public static float AdjustHue(MSHColor color, float m) { if (color.M >= m) { return(color.H); } float hSpin = (float)(color.S * Math.Sqrt(m * m - color.M * color.M) / (color.M * Math.Sin(color.S))); if (hSpin > -Math.PI / 3.0) { return(color.H + hSpin); } return(color.H - hSpin); }
/// <summary> /// Make an interpolation from m1 to m2 /// </summary> /// <param name="m1">The left color (interp == 0.0). Set to RGB = (59, 76, 192) for cold color</param> /// <param name="m2">The right color (interp == 1.0). Set to RGB = (180, 4, 38) for warm color</param> /// <param name="interp">The interpolation value between 0.0 and 1.0</param> public void FromColorInterpolation(MSHColor m1, MSHColor m2, float interp) { float radDiff = Math.Abs(m1.H - m2.H); if (m1.S > 0.05 && m2.S > 0.05 && radDiff > Math.PI / 3.0f) { float midM = m1.M; if (midM < m2.M) { midM = m2.M; } if (midM < 98) { midM = 98; } if (interp < 0.5f) { m2.M = midM; m2.S = 0; m2.H = 0; interp *= 2.0f; } else { m1.M = midM; m1.S = 0; m1.H = 0; interp = 2.0f * interp - 1.0f; } } if (m1.S < 0.05 && m2.S > 0.05) { m1.H = AdjustHue(m2, m1.M); } else if (m1.S > 0.05 && m2.S < 0.05) { m2.H = AdjustHue(m1, m2.M); } MSHColor mid = m1 * (1.0f - interp) + m2 * (interp); M = mid.M; S = mid.S; H = mid.H; }
/// <summary> /// Make an interpolation from c1 to c2 /// </summary> /// <param name="c1">The left color (interp == 0.0). Set to (59, 76, 192) for cold color</param> /// <param name="c2">The right color (interp == 1.0). Set to (180, 4, 38) for warm color</param> /// <param name="interp">The interpolation value between 0.0 and 1.0</param> public void FromColorInterpolation(Color c1, Color c2, float interp) { MSHColor m1 = new MSHColor(c1); MSHColor m2 = new MSHColor(c2); float radDiff = Math.Abs(m1.H - m2.H); if (m1.S > 0.05 && m2.S > 0.05 && radDiff > Math.PI / 3.0f) { float midM = (float)(Math.Max(Math.Max(m1.M, m2.M), 98)); if (interp < 0.5f) { m2.M = midM; m2.S = 0; m2.H = 0; interp *= 2.0f; } else { m1.M = midM; m1.S = 0; m1.H = 0; interp = 2.0f * interp - 1.0f; } } if (m1.S < 0.05 && m2.S > 0.05) { m1.H = AdjustHue(m2, m1.M); } else if (m1.S > 0.05 && m2.S < 0.05) { m2.H = AdjustHue(m1, m2.M); } MSHColor mid = m1 * (1.0f - interp) + m2 * (interp); M = mid.M; S = mid.S; H = mid.H; }
/// <summary> /// Make a linear interpolation between two LAB Color /// </summary> /// <param name="c1">the first color (t=0)</param> /// <param name="c2">the second color (t=1)</param> /// <param name="t">the progression factor (between 0 and 1)</param> /// <returns>The interpolated color</returns> public static MSHColor Lerp(MSHColor c1, MSHColor c2, float t) { return(new MSHColor(c1 * (1.0f - t) + c2 * t)); }