简体   繁体   中英

Cannot compare two SFML Colors

I have to compare two colors in SFML.net. In C++ it's possible because there is defined == operator. In SFML.net Visual Studio won't let me compile the code. How to resolve that? Error: 1>E:\\DB\\Dropbox\\Repozytoria\\ARDSQL GUI\\Sources\\StatusBar.cs(91,17,91,70): error CS0019: Operator '==' cannot be applied to operands of type 'SFML.Graphics.Color' and 'SFML.Graphics.Color'

My code:

if (base.barRectangle.FillColor == Color.Green)
{
     ///Do something...
}

Try comparing the individual components:

if (base.barRectangle.FillColor.r == Color.Green.r &&
    base.barRectangle.FillColor.g == Color.Green.g &&
    base.barRectangle.FillColor.b == Color.Green.b){
     ///Do something...
}

Or you could try writing a color comparison function of your own:

bool isEqualSFColors(SFML.Graphics.Color c1, SFML.Graphics.Color c2){
    if (c1.r == c2.r &&
        c1.g == c2.g &&
        c1.b == c2.b){
        return true;
    }
    return false;
}

Note that I did not include alpha into the comparison (yourColor.a is how you would get it).

SFML is also open source, so you are free to add the operator overloading you desire: http://msdn.microsoft.com/en-us/library/aa288467(v=vs.71).aspx

It's also possible that you are using an older version/binary that you found. I haven't used SFML.net, but I'm sure if you grab a newer copy there may already be this feature built-in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM