7

如何检查两个 System.Drawing.Color 结构是否以 16 位颜色深度表示相同的颜色(或通常基于 Screen.PrimaryScreen.BitsPerPixel 的值)?

假设我将 Form.TransparencyKey 设置为 Value1(颜色类型),我想检查当用户为表单(Value2)选择新的背景颜色时,我没有将整个表单设置为透明。

在 32 位色深屏幕上,我只是比较这两个值:

如果(值 1 == 值 2)

但是,这在 16 位颜色深度屏幕上不起作用,因为 Value2 的更多颜色值将代表与 Value1 相同的实际 16 位颜色,因为我发现很难。

4

3 回答 3

1

16 位颜色有两种像素格式,555 和 565。在比较它们之前,您必须使用 0xf8(5 位)和 0xfc(6 位)屏蔽 R、G 和 B 值。请记住,运行设计器的机器并不代表运行程序的机器。

于 2010-02-02T15:50:40.447 回答
1

试试下面的代码:

void MyTestMethod() {
    TransparencyKey = Color.FromArgb(128, 128, 64);
    BackColor = Color.FromArgb(128, 128, 71);

    byte tR = ConvertR(TransparencyKey.R);
    byte tG = ConvertG(TransparencyKey.G);
    byte tB = ConvertB(TransparencyKey.B);

    byte bR = ConvertR(BackColor.R);
    byte bG = ConvertG(BackColor.G);
    byte bB = ConvertB(BackColor.B);

    if (tR == bR &&
        tG == bG &&
        tB == bB) {
        MessageBox.Show("Equal: " + tR + "," + tG + "," + tB + "\r\n" +
            bR + "," + bG + "," + bB);
    }
    else {
        MessageBox.Show("NOT Equal: " + tR + "," + tG + "," + tB + "\r\n" +
            bR + "," + bG + "," + bB);
    }
}

byte ConvertR(byte colorByte) {
    return (byte)(((double)colorByte / 256.0) * 32.0);
}

byte ConvertG(byte colorByte) {
    return (byte)(((double)colorByte / 256.0) * 64.0);
}

byte ConvertB(byte colorByte) {
    return (byte)(((double)colorByte / 256.0) * 32.0);
}

只需摆弄 TransparancyKey 和 BackColor 看看它是否适合您。对我来说确实如此。是的,我知道它是臃肿而丑陋的代码,当然它只是作为示例。

于 2010-02-02T15:55:26.850 回答
0

由于 ColorTranslator.ToWin32 在引擎盖下使用,这有效吗?

if( ColorTranslator.ToWin32(Value1) == ColorTranslator.ToWin32(Value2) )
于 2010-02-02T15:00:06.567 回答