Hows makes window color calculated from ARGB

Can someone tell me how from ARGB, the corresponding windows are computed? I know that there is ColorTranslator.ToWin32 () that does the calculations, but how is this done?

Also, what is the difference between OLE color and windows color (win32)?

JD.

+3
source share
2 answers

The color object in .NET includes an alpha channel (i.e., the transparency level), and Win32 colors are pure RGB. So, to convert between the two, you want something like the following:

    static public int ConvertColourToWindowsRGB(Color dotNetColour)
    {
        int winRGB = 0;

        // windows rgb values have byte order 0x00BBGGRR
        winRGB |= (int)dotNetColour.R;
        winRGB |= (int)dotNetColour.G << 8;
        winRGB |= (int)dotNetColour.B << 16;

        return winRGB;
    }

    static public Color ConvertWindowsRGBToColour(int windowsRGBColour)
    {
        int r = 0, g = 0, b = 0;

        // windows rgb values have byte order 0x00BBGGRR
        r = (windowsRGBColour & 0x000000FF);
        g = (windowsRGBColour & 0x0000FF00) >> 8;
        b = (windowsRGBColour & 0x00FF0000) >> 16;

        Color dotNetColour = Color.FromArgb(r, g, b);

        return dotNetColour;
    }
+2
source

Using an IL disassembler (e.g. Reflector), you can get the code for ColorTranslator.ToWin32 (). Note that the ToWin32 color does not use an alpha value.

public static int ToWin32(Color c)
{
    return ((c.R | (c.G << 8)) | (c.B << 0x10));
}

ColorTranslator.ToOle(...) , , ToWin32:

public static int ToOle(Color c)
{
    if (c.IsKnownColor)
    {
        switch (c.ToKnownColor())
        {
            case KnownColor.ActiveBorder:
                return -2147483638;

            case KnownColor.ActiveCaption:
                return -2147483646;

            case KnownColor.ActiveCaptionText:
                return -2147483639;

            case KnownColor.AppWorkspace:
                return -2147483636;

            case KnownColor.Control:
                return -2147483633;

            case KnownColor.ControlDark:
                return -2147483632;

            case KnownColor.ControlDarkDark:
                return -2147483627;

            case KnownColor.ControlLight:
                return -2147483626;

            case KnownColor.ControlLightLight:
                return -2147483628;

            case KnownColor.ControlText:
                return -2147483630;

            case KnownColor.Desktop:
                return -2147483647;

            case KnownColor.GrayText:
                return -2147483631;

            case KnownColor.Highlight:
                return -2147483635;

            case KnownColor.HighlightText:
                return -2147483634;

            case KnownColor.HotTrack:
                return -2147483635;

            case KnownColor.InactiveBorder:
                return -2147483637;

            case KnownColor.InactiveCaption:
                return -2147483645;

            case KnownColor.InactiveCaptionText:
                return -2147483629;

            case KnownColor.Info:
                return -2147483624;

            case KnownColor.InfoText:
                return -2147483625;

            case KnownColor.Menu:
                return -2147483644;

            case KnownColor.MenuText:
                return -2147483641;

            case KnownColor.ScrollBar:
                return -2147483648;

            case KnownColor.Window:
                return -2147483643;

            case KnownColor.WindowFrame:
                return -2147483642;

            case KnownColor.WindowText:
                return -2147483640;

            case KnownColor.ButtonFace:
                return -2147483633;

            case KnownColor.ButtonHighlight:
                return -2147483628;

            case KnownColor.ButtonShadow:
                return -2147483632;

            case KnownColor.GradientActiveCaption:
                return -2147483621;

            case KnownColor.GradientInactiveCaption:
                return -2147483620;

            case KnownColor.MenuBar:
                return -2147483618;

            case KnownColor.MenuHighlight:
                return -2147483619;
        }
    }
    return ToWin32(c);
}

, Color = SystemColors.ControlText, ColorTranslator.ToOle() -2147483630, ARGB, ColorTranslator.ToOle ToWin32 ().

+3

Source: https://habr.com/ru/post/1718256/


All Articles