Click here to Skip to main content
15,884,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi i'm working on c# winform application and i store the color in databse "
Color [A=255, R=128, G=0, B=255]
"in this form now i want to convert this to back in color .any one tell me how can i do this

What I have tried:

I'm getting color in string form from sql server 2014
string bakcolo=row["Color"].ToString();
            // and try this method to convert string in color but its not working
               Color mycolor = ColorTranslator.FromHtml(bakcolo);
              Colorbutton1.BackColor=Color.FromName(bakcolo);
Posted
Updated 28-Mar-21 4:44am

 
Share this answer
 
Comments
Maciej Los 28-Dec-18 8:03am    
5ed!
Mehdi Gholam 28-Dec-18 8:14am    
Cheers!
Maciej Los 28-Dec-18 8:47am    
:thumbsup:
Why are you storing a Color value in a DB as a string? It fits in an unsigned integer as a "native" value ... and then it's just a simple matter to get a Color back again:
Color c = Color.AliceBlue;
int i = c.ToArgb();
c = Color.FromArgb(i);

To play with strings is both inefficient, and cumbersome:
string s = "Color [A=255, R=128, G=0, B=255]";
Match m = Regex.Match(s, @"A=(?<Alpha>\d+),\s*R=(?<Red>\d+),\s*G=(?<Green>\d+),\s*B=(?<Blue>\d+)");
if (m.Success)
    {
    int alpha = int.Parse(m.Groups["Alpha"].Value);
    int red = int.Parse(m.Groups["Red"].Value);
    int green = int.Parse(m.Groups["Green"].Value);
    int blue = int.Parse(m.Groups["Blue"].Value);
    Color c = Color.FromArgb(alpha, red, green, blue);
    ...
    }
 
Share this answer
 
Comments
Fahid Zahoor 27-Dec-18 21:17pm    
Color c = Color.AliceBlue;
int i = c.ToArgb();
c = Color.FromArgb(i);
its good method now I'm using it but one problem i face
I'm using colordialouge for selection of color now please tell me how can i put selected color instead this name 'AliceBlue'.
Or 1).you have any idea how can i convert selected color to its name.
2) you have any idea how can i convert button background color to color name and store this color name in any variable of string data type.
OriginalGriff 28-Dec-18 1:42am    
You are kidding, aren't you?
"Color.AliceBlue" was just an example to show it worked - when you have your Color value in a variable it is irrelevant what the name is. And the ColorDialog supplies you with a Color value whenteh user has picked it!

And don't store "color names" - there are 4,294,967,295 different color combinations in an ARGB color value and not all of those have names... Store the integer value and it can be converted easily and efficiently.
Fahid Zahoor 28-Dec-18 6:51am    
ok so plz tell me how can i convert selected color to integer value and then i store this integer value in database
OriginalGriff 28-Dec-18 7:01am    
Oh ... but converting Color to int so difficult! And I didn't give you the code for that two days ago, did I? :doh:


You know, I think I did ... it's one whole line of code ... which calls just one method ...

You are going to have to start thinking about what you are doing - you aren't giving anyone a particularly good impression you know.
Create a Method for convert this (Color[A=....) into int Array
C#
public static int[] ConvertColo(string coll) 
        {
            string[] split = coll.Split(',');
            for (int i = 0; i < split.Length; i++)
            {
                if (i == 0)
                {
                    split[i] = split[i].Remove(0, 9);
                }
                else if (i == 3)
                {
                    split[i] = split[i].Remove(0, 3);
                    split[i] = split[i].Remove(split[i].Length - 1, 1);
                }
                else
                    split[i] = split[i].Remove(0, 3);
            }
            int[] ints = Array.ConvertAll(split, s => int.Parse(s));
            return ints;
        }

Now pass the string in this method and receive output in int array and set output color to your control
C#
int[] ints = ColorClass.ConvertColo(item.Color);
               btn.BackColor = Color.FromArgb(ints[0], ints[1], ints[2], ints[3]);
 
Share this answer
 
Comments
CHill60 29-Mar-21 4:56am    
You haven't really added anything new to this thread and this uncommented method is very cumbersome and not clear at all. Magic numbers 3 and 9 - what do they do?
And worse than that when I run your code with col1 = "[A=255, R=15, G=0, B=255]"; I get a Runtime exception "Index and count must refer to a location within the string."
Stick to answering newer posts where the OP still needs help,
Make sure you bring something new to the thread
and make sure your code actually works
Naeem Shah 29-Mar-21 6:36am    
please look at question in question string is "Color [A=255, R=128, G=0, B=255]"
and you testing on this string "[A=255, R=15, G=0, B=255]" definetly it will give u exception
Thanks
Naeem Shah 29-Mar-21 6:39am    
when u choose ARGB Color from ColorDialog it will return colorName like this
Color [A=255, R=128, G=0, B=255] same issue i faced that's why i created my own method to convert argb int number into array
CHill60 29-Mar-21 8:03am    
My other points still stand. Especially the bits about lack of comments or explanation and the use of magic numbers. If you don't like the use of regex from Solution 2 then the following is less verbose, easier to understand and doesn't use magic numbers
public static int[] ConvertColor(string coll) 
{
// Convert color string in "Color [A=9, R=9, G=9, B=9]" format to int array 

	//Split on commas to get A R G B
	string [] split = coll.Split(',');
	int [] result = new int[split.Length];
	for (int i = 0; i < split.Length; i ++)
	{
		//Split on = to get the actual values in 2nd element
		string [] split2 = split[i].Split('=');
		//remove the trailing bracket ]
		result[i] = int.Parse(split2[1].Replace("]", string.Empty));
	}
	return result;
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900