How to calculate color based on a range of values ​​in C #?

var colors = new Color[] { 
    Color.Blue, 
    Color.Green, 
    Color.Yellow, 
    Color.Orange, 
    Color.Red 
};

var min = 0;
var max = 400;

I am trying to get the color between these values ​​based on a different number. So, for example, if I wanted a color for a value of 350, it would be 50% orange and 50% red.

EDIT - rewritten for clarity

The only way I can do this is to create a gradient image in Photoshop, then calculate the offset and capture the RGB pixel value. However, this seems extremely hacky, and I would like to do it with some kind of calculation.

Any ideas?

+3
source share
5 answers

R, G B ( A, ). Windows Form. - 0 400 :

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    byte interpolate(byte a, byte b, double p)
    {
        return (byte)(a * (1 - p) + b * p);
    }

    private void trackBar1_Scroll(object sender, EventArgs e)
    {
        int v = trackBar1.Value;
        BackColor = getColor(v);
    }

    private Color getColor(int v)
    {
        SortedDictionary<int, Color> d = new SortedDictionary<int, Color>();
        d.Add(0, Color.Blue);
        d.Add(100, Color.Green);
        d.Add(200, Color.Yellow);
        d.Add(300, Color.Orange);
        d.Add(400, Color.Red);

        KeyValuePair<int, Color> kvp_previous = new KeyValuePair<int,Color>(-1, Color.Black);
        foreach (KeyValuePair<int, Color> kvp in d)
        {
            if (kvp.Key > v)
            {
                double p = (v - kvp_previous.Key) / (double)(kvp.Key - kvp_previous.Key);
                Color a = kvp_previous.Value;
                Color b = kvp.Value;
                Color c = Color.FromArgb(
                    interpolate(a.R, b.R, p),
                    interpolate(a.G, b.G, p),
                    interpolate(a.B, b.B, p));
                return c;
            }
            kvp_previous = kvp;
        }

        return Color.Black;
    }
}

HSL, nobugz.

. . , , .. . , , , .

+11

, , :

if (kvp.Key > v)
{
    double p = (v - kvp_previous.Key) / (double)(kvp.Key - kvp_previous.Key);
    Color a = kvp_previous.Value;
    Color b = kvp.Value;
    Color c = Color.FromArgb(
        interpolate(a.R, b.R, p),
        interpolate(a.G, b.G, p),
        interpolate(a.B, b.B, p));
    return c;
}
else if (kvp.Key == v)
{
    return kvp.Value;
}

, , . ( , , , )

+4

/// <summary>
/// Interpolate colors 0.0 - 1.0        
/// </summary>        
public static Color Interpolate(double percent, params Color[] colors)
{
    int left = (int)Math.Floor(percent * (colors.Length - 1));
    int right = (int)Math.Ceiling(percent * (colors.Length - 1));
    Color colorLeft = colors[left];
    Color colorRight = colors[right];

    double step = 1.0 / (colors.Length - 1);
    double percentRight = (percent - (left * step)) / step;
    double percentLeft = 1.0 - percentRight;

    Color outputColor = new Color();

    outputColor.R = (byte)(colorLeft.R * percentLeft + colorRight.R * percentRight);
    outputColor.G = (byte)(colorLeft.G * percentLeft + colorRight.G * percentRight);
    outputColor.B = (byte)(colorLeft.B * percentLeft + colorRight.B * percentRight);
    outputColor.A = (byte)(colorLeft.A * percentLeft + colorRight.A * percentRight);

    return outputColor;
}
+2

- ... . , , , , . , .

using System.Convert;

public static Color oddColorFunction(int value)
{
    Color colors = new Color[] { Color.Blue, Color.Green, Color.Yellow, Color.Orange, Color.Red };
    int min = 0;
    int max = 400;

    decimal range = max / colors.Length;

    Color leftColor = ToInt32(Decimal.Floor(value / range));
    Color rightColor = ToInt32(Decimal.Ceiling(value / range));

    return mixColors(colors[leftColor], colors[rightColor], ToInt32(Decimal.Round(value % range * 100)));
}

public static mixColors(Color colorA, Color colorB, int percentage)
{
    //combine colors here based on percentage
    //I'm to lazy to code this :P
}
+1

Color has properties of R, G and B . You can take your input value, divide it by 100 to get the bottom color (crop it by 3). Add one to this to get the top color. Then take R, G, and B from the lower and upper colors, create a weighted average of each of the values ​​based on% 100, and create a new color with these values.

0
source

Source: https://habr.com/ru/post/1733802/


All Articles