Generate Color Gradient in C#

后端 未结 6 843
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-08 19:50

My question here is similar to the question here, except that I am working with C#.

I have two colors, and I have a predefine steps. How to retrieve a list of C

6条回答
  •  北海茫月
    2020-12-08 20:32

    Maybe this function can help:

    public IEnumerable GetGradients(Color start, Color end, int steps)
    {
        Color stepper = Color.FromArgb((byte)((end.A - start.A) / (steps - 1)),
                                       (byte)((end.R - start.R) / (steps - 1)),
                                       (byte)((end.G - start.G) / (steps - 1)),
                                       (byte)((end.B - start.B) / (steps - 1)));
    
        for (int i = 0; i < steps; i++)
        {
            yield return Color.FromArgb(start.A + (stepper.A * i),
                                        start.R + (stepper.R * i),
                                        start.G + (stepper.G * i),
                                        start.B + (stepper.B * i));
        }
    }
    

提交回复
热议问题