映射值的无限范围内10种颜色?范围内、种颜色

由网友(感情演员)分享简介:我有10种颜色的清单如下:I have the following list of 10 colors:public static readonly IList lineColors = new ReadOnlyCollection(new List { new Solid...

我有10种颜色的清单如下:

I have the following list of 10 colors:

public static readonly IList<Brush> lineColors = new ReadOnlyCollection<Brush>
        (new List<Brush> { 
            new SolidColorBrush(Color.FromRgb(35, 31, 32)),
            new SolidColorBrush(Color.FromRgb(64, 64, 66)),
            new SolidColorBrush(Color.FromRgb(89, 89, 91)),
            new SolidColorBrush(Color.FromRgb(110, 111, 113)),
            new SolidColorBrush(Color.FromRgb(129, 130, 132)),
            new SolidColorBrush(Color.FromRgb(148, 149, 153)),
            new SolidColorBrush(Color.FromRgb(168, 169, 173)),
            new SolidColorBrush(Color.FromRgb(189, 190, 193)),
            new SolidColorBrush(Color.FromRgb(210, 211, 213)),
            new SolidColorBrush(Color.FromRgb(231, 231, 232))
        });

现在我也有一个范围从1到n。我想同样映射这些数值,对该10种颜色,使得最小值是第一颜色和最高值被映射到最后的颜色。所有其他颜色应该包括在该数值范围相等的距离。

Now I also have a range from 1 to n. I'd like to map these value equally to this 10 colors so that the smallest value is the first color and the highest value is mapped to the last color. All the other colors should cover an equal distance in the value range.

如何做到这一点?

我认为,这将可能做到这一点:

I think this will maybe do it:

int position = Math.floor( value / ((max - min) / lineColors.Count));
lineColors.ElementAt(position);

但我不知道这是否适用于所有的可能性,如果没有一个简单的解决方案。

But I'm not sure if this is valid for all possibilities and if there isn't a simpler solution.

推荐答案

映射值可以是EX pressed用线性函数:F(X)= AX + B

Mapping values can be expressed with a linear function: f(x) = ax + b

无论你的范围内提供该功能,从它可以被计算要点。 F(1)= 0 F(N)= 9 |该指数最高的十个颜色列表。

Both your ranges provide points on that function from which it can be calculated. f(1) = 0 f(n) = 9 | The highest index in a list of ten colours.

0 = A * 1 + B 9 =一* N + B

0 = a * 1 + b 9 = a * n + b

9 = A *(N - 1) A = 9 /(N - 1) => B = -9 /(N - 1)

9 = a * (n - 1) a = 9 / (n - 1) => b = -9 / (n - 1)

=> F(X)= 9 /(N - 1)*(X - 1)

=> f(x) = 9 / ( n - 1) * (x - 1)

这里的问题是,如果你一轮马上少值将被映射到0和9,因为四舍五入的范围只有0.5(从0到0.5,从8.5到9),所以你可以伸展范围和转移回由0.5到占该

The problem here is that if you round that right away less values will be mapped to 0 and 9 because the rounding range is only 0.5 (from 0 to 0.5 and from 8.5 to 9), so you could stretch the range and shift it back by 0.5 to account for that.

private int MapValue(int value, int n)
{
    int output = (int)Math.Round((10.0 / (n - 1) * (value - 1)) - 0.5, 0);
    if (output == -1) return 0;
    else return output;
}
阅读全文

相关推荐

最新文章