VS2015图标指南-颜色反转

5
我下载了一组VS2015图标并阅读MSDN指南
在“使用图像中的颜色”下,它指出:“为了使图标在Visual Studio暗主题中显示正确的对比度比率,需要通过编程应用反转。”
我试图在我的应用程序中模仿这种行为,但是当我将颜色反转应用于图像时,它不会像在VS的暗主题中看起来那样。

enter image description here

有人知道 VS 是如何反转颜色的,这样我就可以模仿它了吗?

编辑: 这是我正在使用的反转代码 - 问题似乎出现在具有透明度/ alpha 的边缘处:

        public static void InvertColors(Bitmap bitmapImage)
    {
        var bitmapRead = bitmapImage.LockBits(new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppPArgb);
        var bitmapLength = bitmapRead.Stride * bitmapRead.Height;
        var bitmapBGRA = new byte[bitmapLength];
        Marshal.Copy(bitmapRead.Scan0, bitmapBGRA, 0, bitmapLength);
        bitmapImage.UnlockBits(bitmapRead);

        for (int i = 0; i < bitmapLength; i += 4)
        {
            bitmapBGRA[i] = (byte)(255 - bitmapBGRA[i]);
            bitmapBGRA[i + 1] = (byte)(255 - bitmapBGRA[i + 1]);
            bitmapBGRA[i + 2] = (byte)(255 - bitmapBGRA[i + 2]);
        }

        var bitmapWrite = bitmapImage.LockBits(new Rectangle(0, 0, bitmapImage.Width, bitmapImage.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppPArgb);
        Marshal.Copy(bitmapBGRA, 0, bitmapWrite.Scan0, bitmapLength);
        bitmapImage.UnlockBits(bitmapWrite);
    }

你能展示一下你已经尝试过的代码吗?Visual Studio的源代码是专有的,所以你不太可能找到任何人可以向你展示它的源代码工作方式。 - nevada_scout
2个回答

4

谢谢 - 我正在进一步调查他们如何生成反转图像,因为我不能将这些库与我的解决方案打包和分发。 - TJF

4

根据IVsUIShell5.ThemeDIBits方法的文档,颜色是通过其亮度进行调整的:

图像的亮度被转换,以使常量“光晕”亮度与背景混合。 这有助于在视觉上消除光晕。 “光晕”亮度是一个不可变常量,并不是从输入图像计算出来的。

因此,您需要将像素转换为HSL颜色空间,调整颜色并转换回来。我在某个地方偶然发现了这个方法:

private double TransformLuminosity(double luminosity)
{
    double haloLuminosity = HaloLuminosity; //Color.FromArgb(255, 246, 246, 246)
    double themeBackgroundLuminosity = ThemeBackgroundColor.L;

    if (themeBackgroundLuminosity < LuminosityInversionThreshold) //LuminosityInversionThreshold = 0.5
    {
        haloLuminosity = 1.0 - haloLuminosity;
        luminosity = 1.0 - luminosity;
    }

    if (luminosity < haloLuminosity)
    {
        return themeBackgroundLuminosity * luminosity / haloLuminosity;
    }

    return (1.0 - themeBackgroundLuminosity) * (luminosity - 1.0) / (1.0 - haloLuminosity) + 1.0;
}

我根据大多数图标周围的灰色颜色 Color.FromArgb(255, 246, 246, 246) 来确定光环亮度。它并不能完全达到相同的效果,但足以令人满意,并且目前符合我的目的。以下是一些示例:

Without transformation With transformation


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接