Convert int to Color in Windows Forms
Asked Answered
O

6

7

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"]) })
Overlarge answered 13/6, 2011 at 19:43 Comment(4)
What does this value mean? How is it supposed to be converted to a color?Lookthrough
Is it supposed to represent an RGB value?Ertha
I get it from ((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"] = colorDialog.Color.ToArgb();Overlarge
You can't just cast an int to a Color in C#.Nutbrown
C
10

Try this instead:

var argb = Convert.ToInt32(
    ((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"]);
Color = Color.FromArgb(argb);
Curmudgeon answered 13/6, 2011 at 19:47 Comment(3)
No its not working FromArgb() - will take 4 args.so how its possibleCulprit
FromArgb has four overloads, one of which takes a 32-bit integer. msdn.microsoft.com/en-us/library/2zys7833.aspxCurmudgeon
System.Drawing.Color.FromArgb(argb); Took me ages to work that out because System.Windows.Media ALSO has Color.FromArgb that only takes 4 parametersConsensus
O
37

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);
Ourself answered 13/6, 2011 at 21:20 Comment(1)
That is a hilarious build up.Phenetole
C
10

Try this instead:

var argb = Convert.ToInt32(
    ((DataRowView)this.dataGridView1.Rows[e.RowIndex].DataBoundItem)["Color"]);
Color = Color.FromArgb(argb);
Curmudgeon answered 13/6, 2011 at 19:47 Comment(3)
No its not working FromArgb() - will take 4 args.so how its possibleCulprit
FromArgb has four overloads, one of which takes a 32-bit integer. msdn.microsoft.com/en-us/library/2zys7833.aspxCurmudgeon
System.Drawing.Color.FromArgb(argb); Took me ages to work that out because System.Windows.Media ALSO has Color.FromArgb that only takes 4 parametersConsensus
R
6

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"]) }));
Rubi answered 13/6, 2011 at 19:48 Comment(0)
G
1
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.

Gnomic answered 13/6, 2011 at 19:47 Comment(0)
F
0

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));
}
Fricative answered 4/11, 2022 at 11:8 Comment(1)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Gibbsite
R
0

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]
Ramtil answered 8/12, 2022 at 23:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.