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

前端 未结 4 1247
清酒与你
清酒与你 2021-01-13 04:35

This does not work

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


        
相关标签:
4条回答
  • 2021-01-13 04:54

    Alternatively, this also works, and I think it's 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 讨论(0)
  • 2021-01-13 05:04

    They won't equal the same, as Color.Blue doesn't equal your colour object, it equals something stored internally, a "new Color(KnownColor.Blue);" to be exact.

    0 讨论(0)
  • 2021-01-13 05:08

    I would have expected this with Assert.AreSame because of the boxing with the value types, but AreEqual should not have this problem.

    Could you add which language (I'm assuming C#) your using and which testing framework?

    What does Assert.AreEqual(true, Color.Blue == fred); result in?

    0 讨论(0)
  • 2021-01-13 05:12

    From the MSDN documentation on Color.operator ==:

    This method compares more than the ARGB values of the Color structures. It also does a comparison of some state flags. If you want to compare just the ARGB values of two Color structures, compare them using the ToArgb method.

    I'm guessing the state flags are different.

    0 讨论(0)
提交回复
热议问题