开发者

get a unique brush for numbers

I like to display some decimal numbers. As many as possible need to have a unique brush in WPF.

example

-1.00 (display in Green and Orange)
0.0开发者_如何学运维0 (display in Orange)
1.00 (display in Green)
2.00 (display in Red)

no specified pattern is needed.


RBG is to small for my case.

255 * 255 * 255 = 16 581 375


The color for a value have to be always the same.


public Brush GetValueColor(decimal value)
{
    //Generate Brush for value
    return myBrush;
}

I don't know what is wrong with my question but whatever. Just need a function to return a unique brush for every unique number. It doesn't mater how the numbers looks like.


You could always generate a LinearGradientBrush with GradientStops based on the bytes from value.ToString(). You can also reverse the LinearGradientBrush back to the decimal value if needed.

GetValueColor

public LinearGradientBrush GetValueColor(decimal value)
{
    LinearGradientBrush brush = new LinearGradientBrush();
    byte[] byteArray = Encoding.ASCII.GetBytes(value.ToString());
    for (int i = 0; i < byteArray.Length; i += 3)
    {
        byte red = byteArray[i];
        byte green = i < byteArray.Length - 1 ? byteArray[i + 1] : (byte)0;
        byte blue = i < byteArray.Length - 2 ? byteArray[i + 2] : (byte)0;
        brush.GradientStops.Add(
            new GradientStop(new Color {A = 255, R = red, G = green, B = blue },
                                            (double)(i+1) / byteArray.Length));
    }
    return brush;
}

Reverse: GetColorValue

public decimal GetColorValue(LinearGradientBrush brush)
{
    List<byte> byteList = new List<byte>();
    foreach (GradientStop gradientStop in brush.GradientStops)
    {
        byteList.Add(gradientStop.Color.R);
        byteList.Add(gradientStop.Color.G);
        byteList.Add(gradientStop.Color.B);
    }
    return Convert.ToDecimal(Encoding.ASCII.GetString(byteList.ToArray()));
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜