Using ToArgb () followed by FromArgb () does not result in the original color

This does not work

        int blueInt = Color.Blue.ToArgb();
        Color fred = Color.FromArgb(blueInt);
        Assert.AreEqual(Color.Blue,fred);

Any suggestions?

[change]

I am using NUnit, and the output is

failed:

Expected: Color [Blue]

But it was: Color [A = 255, R = 0, G = 0, B = 255]

[change]

It works!

        int blueInt = Color.Blue.ToArgb();
        Color fred = Color.FromArgb(blueInt);
        Assert.AreEqual(Color.Blue.ToArgb(),fred.ToArgb());
+5
source share
4 answers

From the MSDN documentation onColor.operator == :

This method compares more than ARGB Values โ€‹โ€‹of color structures. He also makes comparisons of some state flags. If you just want to compare the ARGB values โ€‹โ€‹of the two colors of the structures, compare them using the ToArgb Method.

I assume the status flags are different.

+10
source

, Color.Blue , , , " (KnownColor.Blue)"; .

+1

I would expect this with Assert.AreSame due to the box with value types, but AreEqual should not have this problem.

Could you add which language (I assume C #), do you use and what testing structure?

What does it mean Assert.AreEqual(true, Color.Blue == fred);?

0
source

Alternatively this also works and I think it is more intuitive

    [Test]
    public void ColorTransform()
    {
        var argbInt = Color.LightCyan.ToArgb();
        Color backColor = Color.FromArgb(argbInt);
        Assert.AreEqual(Color.LightCyan.A, backColor.A);
        Assert.AreEqual(Color.LightCyan.B, backColor.B);
        Assert.AreEqual(Color.LightCyan.G, backColor.G);
        Assert.AreEqual(Color.LightCyan.R, backColor.R);
    }
0
source

All Articles