-2

Sorry if question to stupid, but I have close to none C# experience. For the project I work on I want to check if Border color is blue.

Position pos = new Position(x, y); // my custom class
Border b = (Border)FindName("b_"+ pos.X +"_"+ pos.Y); // get berder by name
// One of my fail attempts
bool isBlue = b.GetValue(SolidColorBrush.ColorProperty).ToString().Equals(Colors.Blue.ToString());

Hope it is posible.

// I set color to Border this way
Border b = new Border();
b.Background = new SolidColorBrush(Colors.WhiteSmoke);

Hour of Google didn't gave me any simple enough result I could use. Closest I found was compare post. But my knowledge is not good enough to adapt it. Any help right direction are welcome.

my-
  • 604
  • 9
  • 17
  • What **exactly** do you mean by "color is blue"? Do you mean "consists of only the color channel for blue"? Do you mean "look blue(ish)"? Do you mean "more blue than red"? – Lasse V. Karlsen Dec 15 '17 at 18:46
  • I mean if `Border.Background = new SolidColorBrush(Colors.Blue);` then value of `isBlue` should be `true`. if color `Colors.WhiteSmoke` then value of `isBlue` should be `false`. – my- Dec 15 '17 at 19:32
  • I know only this way to set color for the Border. – my- Dec 15 '17 at 19:35

1 Answers1

4

What you can do is compare the ARGB values, an example would be this extension:

public static bool IsEqualTo(this Color color1, Color color2)
{
    return color1.A == color2.A && color1.R == color2.R && color1.G == color2.G && color1.B == color2.B;
}

And use it like this:

bool isBlue = b.GetValue(SolidColorBrush.ColorProperty).IsEqualTo(Colors.Blue);
Haytam
  • 4,643
  • 2
  • 20
  • 43
  • For my Java brain that method parameters looks strange.. Does **this** in parameters means I can call this method on this type object? and don't pass it to method?? Asking because so some reason Visual Studio don't like your suggested method call.. – my- Dec 15 '17 at 18:42
  • 1
    Is it an *extension method*, google it. Specifically, it means you're allowed to use it by writing `c1.IsEqualTo(c2)` instead of (in addition to) `ColorExtensions.IsEqualTo(c1, c2)`. – Lasse V. Karlsen Dec 15 '17 at 18:43
  • @MichaelDorgan I would aspect it should. At least it would be logical. But I could not find any. – my- Dec 15 '17 at 18:44
  • 2
    The `Color` type does indeed implement a `==` operator but you shouldn't use it as it doesn't compare the RGB values directly, it also does things such as comparing the "name" or whatnot of the color, which means that no matter how you construct a color through `Color.FromArgb` it will never be equal to `Color.White`. Better to roll your own. – Lasse V. Karlsen Dec 15 '17 at 18:45
  • That's what I was wondering. Thank you @LasseVågsætherKarlsen – Michael Dorgan Dec 15 '17 at 18:49
  • @LasseVågsætherKarlsen thanks for pointing to _ extension methods_ . To my current knowledge Java don't have extension methods.. Google say it kind of does, from Java8 default methods in interface. But its nothing like c# way.. Or its same way. Do I need add that method (isEqualTo) to some interface? I just trying find out why VS don't like it. – my- Dec 15 '17 at 18:59
  • or is because of I have it inside MainPage.xaml.cs ? – my- Dec 15 '17 at 19:00
  • According to [this post](https://stackoverflow.com/a/6096330/5322506): Following points need to be considered when creating an extension method: The class which defines an extension method must be non-generic, static and non-nested Every extension method must be a static method The first parameter of the extension method should use the this keyword. – my- Dec 15 '17 at 20:13
  • You can just use a simple method (so you will have to remove `this`). – Haytam Dec 15 '17 at 20:18
  • @Haitam I already did it, but I geting 'System.Exception' . Additional information: Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED)) – my- Dec 15 '17 at 20:44
  • UWA is depression. I guess I should rethink my logic and try to avoid color checking. – my- Dec 15 '17 at 20:47
  • @Haitam I don't mind show it. its on github, but I doubt you have enough time for it.. but in case you do its [here](https://github.com/My-/BattleShipUWA) and its that [part](https://github.com/My-/BattleShipUWA/commit/3048ca981ece49aad1da839864d93aaf0a13dad4) .I commented it. Apologies everyone who might look. Code formatting is not standard . – my- Dec 15 '17 at 20:57