简体   繁体   English

使用ToArgb()后跟FromArgb()不会产生原始颜色

[英]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? 有什么建议?

[Edit] [编辑]

I'm using NUnit and the output is 我正在使用NUnit,输出是

failed: 失败:

Expected: Color [Blue] 预计:颜色[蓝色]

But was: Color [A=255, R=0, G=0, B=255] 但是:颜色[A = 255,R = 0,G = 0,B = 255]

[Edit] [编辑]

This works! 这有效!

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

From the MSDN documentation on Color.operator == : Color.operator ==MSDN文档Color.operator ==

This method compares more than the ARGB values of the Color structures. 此方法比Color结构的ARGB值更多。 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. 如果只想比较两个Color结构的ARGB值,请使用ToArgb方法进行比较。

I'm guessing the state flags are different. 我猜测州旗是不同的。

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);" 它们不会相同,因为Color.Blue不等于你的颜色对象,它等于内部存储的东西,“new Color(KnownColor.Blue);” to be exact. 确切地说。

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);
    }

I would have expected this with Assert.AreSame because of the boxing with the value types, but AreEqual should not have this problem. 我本来期望这与Assert.AreSame,因为拳击与值类型,但AreEqual不应该有这个问题。

Could you add which language (I'm assuming C#) your using and which testing framework? 你可以添加哪种语言(我假设是C#)你的使用和测试框架?

What does Assert.AreEqual(true, Color.Blue == fred); Assert.AreEqual(true, Color.Blue == fred);是什么Assert.AreEqual(true, Color.Blue == fred); result in? 造成?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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