开发者

Convert int to Color in Windows Forms

I have a value开发者_开发问答 in row that equals -16777056, but how do I cast it to Color?

Something like that:

Color = (Color) Convert.ToInt32(((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"]) })


Converting between colors and numbers is called color-graphemic synesthesia, and a person with this condition can be able to identify the color (or even shape or "feeling") of a number. One tricky bit here, though, is that this has never been identified in a computer (only in people). Further, this condition is generally due to genetics in people, but has been reported after psychedelic drug use. And while I would never suggest the use of illegal drugs, I guess slipping your CPU some LSD may be worth a shot.

One more difficulty is that synesthetes don't have commonality between them - that is to say that the number 6 isn't the same color to me as it is to you. So these may render differently on your computer than on mine. (Sort of like a "wow, man, how can I know that things that the color "blue" for me doesn't look like what I think "red" is for you?" But again, we're back to illegal drugs.)

That's how I'd try to convert a number into a color. That is, unless your number actually represents something useful, like an ARGB color value. In which case you can use:

Color.FromArgb(numericValue);


Try this instead:

var argb = Convert.ToInt32(
    ((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"]);
Color = Color.FromArgb(argb);


Well, do you know how the color was turned into that integer in the first place? My guess is that it's an ARGB value made up of concatenated bytes.

If so, this is the simplest way:

var myColor = Color.FromArgb(myColorInt);

which would be integrated into your actual line of code something like:

var myColor = Color.FromArgb(Convert.ToInt32(((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"]) }));


Color myColor = Color.FromArgb(Convert.ToInt32(((DataRowView)  
    this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"])});

Edit: after reading one of your comments above, you might be able to simplify everything a bit. Try assigning the selected color like this:

((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"] = 
    colorDialog.Color;

And then read it back out like this:

Color myColor = 
    ((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"];

This way you're saving it as a Color and retrieving it as a Color, and skipping the intermediate conversion steps.

This may or may not work with some other modifications, depending on whether your data source will store a Color instead of an int as it's currently defined.


You can't use this

Color color = System.Drawing.Color.FromArgb(0xFFAABBCC);

Because the input argument is int, and 0xFFAABBCC is outside the range of int

You have use this

Color color = System.Drawing.Color.FromArgb(0xFF, System.Drawing.Color.FromArgb(0xAABBCC));

or create this function in your project.

Color color = GetColor(0xFFAABBCC);

public static Color GetColor(uint color) {
    return Color.FromArgb((byte) ((color >> 24) & 0xFF), (byte) ((color >> 16) & 0xFF), (byte) ((color >> 8) & 0xFF), (byte) (color & 0xFF));
}


Use this function:

System.Drawing.Color LongToColor(long colorNumber)
{
    int a = Int32.Parse(((colorNumber & 0xFF000000L) >> 24).ToString());
    int r = Int32.Parse(((colorNumber & 0x00FF0000L) >> 16).ToString());
    int g = Int32.Parse(((colorNumber & 0x0000FF00L) >> 8).ToString());
    int b = Int32.Parse((colorNumber & 0x000000FFL).ToString());
    return System.Drawing.Color.FromArgb(a, r, g, b);
}

Example

var redColor = LongToColor(0x00FF0000);
var greenColor = LongToColor(0x0000FF00);
var blueColor = LongToColor(0x000000FF);
var other = LongToColor(0xBBABCDEF);
Console.WriteLine(redColor);
//Color [A=0, R=255, G=0, B=0]
Console.WriteLine(greenColor);
//Color [A=0, R=0, G=255, B=0]
Console.WriteLine(blueColor);
//Color [A=0, R=0, G=0, B=255]
Console.WriteLine(other);
//Color [A=187, R=171, G=205, B=239]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜