Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Colors WPF

I have some ellipsis in a canvas. I want that when I click above a ellipse it should change the color of the stroke to green, and if I click again it back to the original color that its red.

I had used this three colors.

SolidColorBrush red = new SolidColorBrush(Colors.Red);
SolidColorBrush green = new SolidColorBrush(Colors.Green);
SolidColorBrush transp = new SolidColorBrush(Colors.Transparent);

When I create the ellipse I already set the colors as red.

Ellipse obj = new Ellipse()
{
    Name = "",
    Width = width,
    Height = height,
    Fill = transp,
    Stroke = red,
};

Then if I click in some ellipse I ask the stroke color to change color.

if (obj.Stroke == red) obj.Stroke = green;
else if (obj.Stroke == green) obj.Stroke = red;
else obj.Stroke = gray;

But the problem is that always get in else condition. Even if the colors is the same in the if condition it returns me false. And always when clicked my ellipse turns gray.

Why this is happening? How can I fix?

EDIT: this <code>if</code> returns me false

like image 504
Butzke Avatar asked Mar 19 '26 20:03

Butzke


1 Answers

Do not create your own SolidColorBrush instances, but use the predefined ones from the Brushes class:

Ellipse obj = new Ellipse()
{
    Name = "",
    Width = width,
    Height = height,
    Fill = Brushes.Transparent,
    Stroke = Brushes.Red,
};

...

if (obj.Stroke == Brushes.Red)
{
    obj.Stroke = Brushes.Green;
}
else if (obj.Stroke == Brushes.Green)
{
    obj.Stroke = Brushes.Red;
}
else
{
    obj.Stroke = Brushes.Gray;
}
like image 168
Clemens Avatar answered Mar 22 '26 10:03

Clemens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!