Rgb Linear Interpolate : Color « 2D Graphics « C# / C Sharp






Rgb Linear Interpolate

  
using System;
using System.Collections.Generic;
using System.Windows.Media;
using System.Reflection;
using System.Globalization;


namespace NASA.BeAMartian.Utils
{
    public class ColorUtils
    {
        public static List<Color> RgbLinearInterpolate(Color start, Color end, int colorCount)
        {
            List<Color> ret = new List<Color>();

            // linear interpolation lerp (r,a,b) = (1-r)*a + r*b = (1-r)*(ax,ay,az) + r*(bx,by,bz)
            for (int n = 0; n < colorCount; n++)
            {
                double r = (double)n / (double)(colorCount - 1);
                double nr = 1.0 - r;
                double A = (nr * start.A) + (r * end.A);
                double R = (nr * start.R) + (r * end.R);
                double G = (nr * start.G) + (r * end.G);
                double B = (nr * start.B) + (r * end.B);

                ret.Add(Color.FromArgb((byte)A, (byte)R, (byte)G, (byte)B));
            }

            return ret;
        }
        public static List<Color> RgbLinearInterpolate(Color start, Color middle, Color end, int colorCount)
        {
            if (colorCount % 2 == 0)
                throw new ArgumentException("colorCount should be and odd number. Currently it is: " + colorCount);

            List<Color> ret = new List<Color>();

            if (colorCount == 0)
                return ret;

            int size = (colorCount + 1) / 2;

            List<Color> res = ColorUtils.RgbLinearInterpolate(start, middle, size);
            if (res.Count > 0)
                res.RemoveAt(res.Count - 1);

            ret.AddRange(res);
            ret.AddRange(ColorUtils.RgbLinearInterpolate(middle, end, size));

            return ret;
        }
        public static Color HsvToRgb(double h, double s, double v)
        {
            int hi = (int)Math.Floor(h / 60.0) % 6;
            double f = (h / 60.0) - Math.Floor(h / 60.0);

            double p = v * (1.0 - s);
            double q = v * (1.0 - (f * s));
            double t = v * (1.0 - ((1.0 - f) * s));

            Color ret;

            switch (hi)
            {
                case 0:
                    ret = ColorUtils.GetRgb(v, t, p);
                    break;
                case 1:
                    ret = ColorUtils.GetRgb(q, v, p);
                    break;
                case 2:
                    ret = ColorUtils.GetRgb(p, v, t);
                    break;
                case 3:
                    ret = ColorUtils.GetRgb(p, q, v);
                    break;
                case 4:
                    ret = ColorUtils.GetRgb(t, p, v);
                    break;
                case 5:
                    ret = ColorUtils.GetRgb(v, p, q);
                    break;
                default:
                    ret = Color.FromArgb(0xFF, 0x00, 0x00, 0x00);
                    break;
            }
            return ret;
        }
        public static Color GetRgb(double r, double g, double b)
        {
            return Color.FromArgb(255, (byte)(r * 255.0), (byte)(g * 255.0), (byte)(b * 255.0));
        }
    }
}

   
    
  








Related examples in the same category

1.Transparent colorTransparent color
2.List all known color in a systemList all known color in a system
3.Draw each of 100 cells with randomly chosen colorsDraw each of 100 cells with randomly chosen colors
4.Filled with the semi transparent and transparent colorFilled with the semi transparent and transparent color
5.All the colors that are supported in C# according
6.Color ChangerColor Changer
7.Known ColorsKnown Colors
8.Five yellow squares with different alpha values(Transparensy)
9.Create two color instances with different alpha components
10.Color.Chocolate
11.Use Color.FromArgb to create Color
12.Get all known color
13.Color representation in r,g,b with values [0.0 - 1.0].
14.Color representation in h,s,v with h = [0 - 360], s,v = [0.0 - 1.0].
15.Return a randomly-generated color
16.Color to RGB value
17.Get color outof String
18.Parse Color
19.Color To Rgb
20.Rgb To Color
21.Returns an HTML #XXXXXX format for a color.
22.Convert color name to Color object
23.Hex Color Util
24.Helper method to get a color based on it's string value
25.Converts a color string to a hex value string
26.Color to Hue,Lightness,Saturation value
27.Get Color From String
28.Convert String value To Color
29.Color to String
30.Masks an image and returns a two color bitmap.
31.Convert Hex To Color
32.Create Color From HLS value
33.Parse Color with Color.FromArgb
34.Create Color Object from RGB value
35.Get Rainbow Colors
36.Convert RGB to HSL
37.HSL to RGB conversion.
38.Hsv To Rgb
39.Get Luminance
40.To Grey scale
41.Heat Map background colour calculations