将excel Color BGR转换为RGB

时间:2014-07-10 09:25:57

标签: c# excel

我发现articleInterior.Color返回的值在BGR中,我试图将其转换为RGB但我仍然有奇怪的颜色

我正在尝试检索excel单元格中的基色以使用ControlPaint.Light点亮它,如下所示:

int rgbColor = ExcelDoc.BGRToRGB((int)contentCanonical.Interior.Color);

Color canonicalColor = ColorTranslator.FromOle(rgbColor);
Color backgroundColor = ControlPaint.Light(canonicalColor, 75);

这是我的转换方法

public static int BGRToRGB(int bgr)
{
    byte[] hexBGR;
    byte[] hexRGB = new byte[4] {0,0,0,0};

    hexBGR = BitConverter.GetBytes(bgr);

    hexRGB[0] = hexBGR[0];
    hexRGB[1] = hexBGR[3];
    hexRGB[2] = hexBGR[2];
    hexRGB[3] = hexBGR[1];

    return BitConverter.ToInt32(hexRGB, 0);
}

我做错了什么?

2 个答案:

答案 0 :(得分:3)

method返回颜色

public static Color From0BGR(int bgrColor)
{
    // Get the color bytes
    var bytes = BitConverter.GetBytes(bgrColor);

    // Return the color from the byte array
    return Color.FromArgb(bytes[0], bytes[1], bytes[2]);
}

这将从rgb颜色返回bgr color int

public static int Get0BGR(Color rgbColor)
{
    // Return a zero-alpha 24-bit BGR color integer
    return (0 << 24) + (rgbColor.B << 16) + (rgbColor.G << 8) + rgbColor.R;
}

答案 1 :(得分:0)

我使用这个功能(我&#39; m&#34;切割&#34;颜色的alfa部分)

    /// <summary>
    /// Converts the color to an Argb color
    /// </summary>
    /// <param name="color">Color to convert</param>
    /// <returns>The Argb color</returns>
    public static int ToArgb(int color)
    {
        int Argb = (color >> 16) | (color & 0xFF) << 16 | (color & 0x00FF00);
        return Argb;
    }

    /// <summary>
    /// Converts the color to a MSO Color
    /// </summary>
    /// <param name="Argb">Color to convert</param>
    /// <returns>The MSO color</returns>
    public static int ToMsoColor(int Argb)
    {
        Argb &= 0xFFFFFF;
        int color = (Argb >> 16) | (Argb & 0xFF) << 16 | (Argb & 0x00FF00);
        return color;
    }