Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C# How to get the color value of the background color of a button for display in RGB, HSL, and HEX formats

jv1597

Coder
I'm trying to get the background color of a button and convert it to a string to display in rgb, hsl, and hex formats. I've made some progress in getting the hex value out of the button background color, but I've yet to find any info on how to get the background color value converted to a string for display in rgb and hsl formats.

Here's what I have so far:

Code:
public void colorCodes(object sender, RoutedEventArgs e)
{
    Button _b = sender as Button;
    SolidColorBrush _s = _b.Background as SolidColorBrush;

    var color = _s.Color;
    txtblk1.Text = color.ToString();
    txtblk2.Text = color.ToString();
    txtblk3.Text = color.ToString();
}

I have the 2nd and 3rd TextBlocks displaying the same hex value for now. Anyone have any ideas?
 
Solution
I'm trying to get the background color of a button and convert it to a string to display in rgb, hsl, and hex formats. I've made some progress in getting the hex value out of the button background color, but I've yet to find any info on how to get the background color value converted to a string for display in rgb and hsl formats.

Here's what I have so far:

Code:
public void colorCodes(object sender, RoutedEventArgs e)
{
    Button _b = sender as Button;
    SolidColorBrush _s = _b.Background as SolidColorBrush;

    var color = _s.Color;
    txtblk1.Text = color.ToString();
    txtblk2.Text = color.ToString();
    txtblk3.Text = color.ToString();
}

I have the 2nd and 3rd TextBlocks displaying the same hex value for now...
I'm trying to get the background color of a button and convert it to a string to display in rgb, hsl, and hex formats. I've made some progress in getting the hex value out of the button background color, but I've yet to find any info on how to get the background color value converted to a string for display in rgb and hsl formats.

Here's what I have so far:

Code:
public void colorCodes(object sender, RoutedEventArgs e)
{
    Button _b = sender as Button;
    SolidColorBrush _s = _b.Background as SolidColorBrush;

    var color = _s.Color;
    txtblk1.Text = color.ToString();
    txtblk2.Text = color.ToString();
    txtblk3.Text = color.ToString();
}

I have the 2nd and 3rd TextBlocks displaying the same hex value for now. Anyone have any ideas?
Hey there,
So this may be helpful. At least to convert the hex value to rgb
Code:
public string HexToRgb(string backgroundColorHex)
{
     Color color = ColorTranslator.FromHtml(backgroundColorHex);
     int r = Convert.ToInt16(color.R);
     int g = Convert.ToInt16(color.G);
     int b = Convert.ToInt16(color.B);
     return $"rgb({r}, {g}, {b})";
}
 
Solution
Hey there,
So this may be helpful. At least to convert the hex value to rgb
Code:
public string HexToRgb(string backgroundColorHex)
{
     Color color = ColorTranslator.FromHtml(backgroundColorHex);
     int r = Convert.ToInt16(color.R);
     int g = Convert.ToInt16(color.G);
     int b = Convert.ToInt16(color.B);
     return $"rgb({r}, {g}, {b})";
}
I believe this here will solve the HSL issue
Code:
HSL hsl = ColorConverter.HexToHsl(new HEX("#00FF00"));
 
Antero360,
I'm using WinUI3. Thanks for the info! I'll try your first suggestion shortly; on the HexToHsl solution, I'm using a button object passed in by a sender object to apply to any button object in general variable as with 'Button _b = sender as Button;'... so I'm wondering if the '_b.Background' object is specified in the parentheses in the 'new HEX("...")' code. How might I get that done using a button object variable rather than one specific hex code at a time?

I'll post the code shortly to give you a better picture of what I'm doing...
 
Antero360,
Your RGB solution worked great! Here is how I implemented it:
Code:
 public void stroke(object sender, RoutedEventArgs e)
 {   
     Button _b = sender as Button;
     SolidColorBrush _s = _b.Background as SolidColorBrush;

     var HexColor = _s.Color;
    
     int r = Convert.ToInt16(HexColor.R);
     int g = Convert.ToInt16(HexColor.G);
     int b = Convert.ToInt16(HexColor.B);
     var RgbColor = $"{r}, {g}, {b}";

     txtblk2.Text = RgbColor.ToString();
     txtblk2.FontStyle = FontStyle.Normal;
     txtblk2.Foreground = new SolidColorBrush(Colors.LightGray);
     txtblk4.Text = HexColor.ToString();
     txtblk4.FontStyle = FontStyle.Normal;
     txtblk4.Foreground = new SolidColorBrush(Colors.LightGray);

     foreach (Line line in g1.Children.OfType<Line>())
     {
         line.Stroke = _s;
     }
 }


Here is an image of the color picker I'm working on:
ColorPicker_ScreenShot.jpg
 
Antero360,
Your RGB solution worked great! Here is how I implemented it:
Code:
 public void stroke(object sender, RoutedEventArgs e)
 {  
     Button _b = sender as Button;
     SolidColorBrush _s = _b.Background as SolidColorBrush;

     var HexColor = _s.Color;
   
     int r = Convert.ToInt16(HexColor.R);
     int g = Convert.ToInt16(HexColor.G);
     int b = Convert.ToInt16(HexColor.B);
     var RgbColor = $"{r}, {g}, {b}";

     txtblk2.Text = RgbColor.ToString();
     txtblk2.FontStyle = FontStyle.Normal;
     txtblk2.Foreground = new SolidColorBrush(Colors.LightGray);
     txtblk4.Text = HexColor.ToString();
     txtblk4.FontStyle = FontStyle.Normal;
     txtblk4.Foreground = new SolidColorBrush(Colors.LightGray);

     foreach (Line line in g1.Children.OfType<Line>())
     {
         line.Stroke = _s;
     }
 }


Here is an image of the color picker I'm working on:
View attachment 2472
Niiiice! Glad to see it working. How about the other portion?
 
Also, as a side note: you might want to rename some of your variables to something that can easily identify the functionality. Will save you some headaches later on 🤣Coming from personal experiences
 
Wow, that really clears it up. So I'm guessing I need to pull hue, saturation, and brightness from the color variable and place them in a string, such as with 'StringBuilder str = "Hue" + " Sat" + "Lit";' or even with a plain string variable?

Great I'll try it out...
 
Wow, that really clears it up. So I'm guessing I need to pull hue, saturation, and brightness from the color variable and place them in a string, such as with 'StringBuilder str = "Hue" + " Sat" + "Lit";' or even with a plain string variable?

Great I'll try it out...
you could just do the same string interpolation (the correct wording lol ) I showed you in the RGB example
 
Antero360,
I tried those out just now... they aren't recognized in WinUI3. I get the common namespace error saying that there is no definition for 'GetHue()' in SolidColorBrush when trying it on the '_s' object. I also tried the 'GetBrightness()' and 'GetSaturation()' members to no avail. That code might only work on WPF apps...
 
Antero360,
I tried those out just now... they aren't recognized in WinUI3. I get the common namespace error saying that there is no definition for 'GetHue()' in SolidColorBrush when trying it on the '_s' object. I also tried the 'GetBrightness()' and 'GetSaturation()' members to no avail. That code might only work on WPF apps...
What's the error message that you are getting?
 
Antero,
I tried installing the toolkit you referenced, but it wasn't compatible with the version of Windows I'm using the version of Windows I'm using seems to be a later one than that of the version the toolkit is compatible with...

I'll be looking around for some more info in the meantime. I'll post anything I find as I come across it...
 
Antero,
I haven't found any workable solutions as of yet, but I'm ok with the RGB and HEX value content I have on the picker. It works. I think we can close this issue for now. I have other issues I need to address to get the module streamlined for efficiency's sake. Thanks for your help!
 

New Threads

Buy us a coffee!

Back
Top Bottom