转换excel颜色BGR到RGB



我发现一篇文章说Interior.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);
}

我做错了什么?

这个方法返回颜色

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颜色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;
}

我正在使用这个函数(我正在"切割"颜色的阿尔法部分)

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

最新更新