Penguen
Penguen

Reputation: 17298

How do I find a color that is between two other colors?

I am assigning each color a numeric value. For example:

Color.red: 12 
Color.Blue: 6

I need to find a color between two colors (for example, red and blue). But how? I have tried this,

(Color.red+color.blue)/2=> (12 + 6)/2 = 9

9 corresponds to Color.yellow

Upvotes: 0

Views: 3152

Answers (2)

gunwin
gunwin

Reputation: 4832

This problem requires you to split the alpha, red, green, and blue componenets of each colour, find the average of each, and create a new color:

    Color first = Color.Red;
    Color second = Color.Blue;


    byte r = (byte)((first.R + second.R) / (byte)2);
    byte g = (byte)((first.G + second.G) / (byte)2);
    byte b = (byte)((first.B + second.B) / (byte)2);
    byte a = (byte)((first.A + second.A) / (byte)2);

    Color mix = Color.FromArgb(a, r, g, b);

Upvotes: 0

ChrisF
ChrisF

Reputation: 137158

You'll need to use the RGB values of the colour and interpolate between those. Using a single value isn't going to give you the discrimination you need.

The answer that yx quotes Drawing a line with a gradient color looks like a good place to start

Upvotes: 5

Related Questions