Search
Module:
Directory

   Desktop Functions:

   Smart Device Functions:


Show Recent Changes
Subscribe (RSS)
Misc. Pages
Comments
FAQ
Helpful Tools
Playground
Suggested Reading
Website TODO List
Download Visual Studio Add-In

COLORREF (Structures)
 
.
Summary
The COLORREF value is used to specify or retrieve an RGB color.

C# Definition:

[StructLayout(LayoutKind.Sequential)]
struct COLORREF {
   public byte R;
   public byte G;
   public byte B;
}

// Alternate
[StructLayout(LayoutKind.Sequential)]
public struct COLORREF
{
   public uint ColorDWORD;

   public COLORREF(System.Drawing.Color color)
   {
      ColorDWORD = (uint) color.R + (((uint) color.G) << 8) + (((uint) color.B) << 16);
   }

   public System.Drawing.Color GetColor()
   {
      return System.Drawing.Color.FromArgb((int) (0x000000FFU & ColorDWORD),
     (int) (0x0000FF00U & ColorDWORD) >> 8, (int) (0x00FF0000U & ColorDWORD) >> 16);
   }

   public void SetColor(System.Drawing.Color color)
   {
      ColorDWORD = (uint) color.R + (((uint) color.G) << 8) + (((uint) color.B) << 16);
   }
}

// Alternate
    [StructLayout(LayoutKind.Explicit, Size = 4)]
    public struct COLORREF {
    public COLORREF(byte r, byte g, byte b) {
        this.Value = 0;
        this.R = r;
        this.G = g;
        this.B = b;
    }

    public COLORREF(uint value) {
        this.R = 0;
        this.G = 0;
        this.B = 0;
        this.Value = value & 0x00FFFFFF;
    }

    [FieldOffset(0)]
    public byte R;
    [FieldOffset(1)]
    public byte G;
    [FieldOffset(2)]
    public byte B;

    [FieldOffset(0)]
    public uint Value;
    }

VB Definition:

Structure COLORREF
     Public R As Byte
     Public G As Byte
     Public B As Byte

     Public Overrides Function ToString() As String
     Return String.Format("({0},{1},{2})", R, G, B)
     End Function
End Structure

User-Defined Field Types:

None.

Notes:

Actually, there's no COLORREF structure in native Win32. It is typedef-ed to DWORD, which means that in the managed world its direct counterpart is System.Int32 (aka int in C#). So, when faced with interop involving COLORREF'S you'd better treat them as int's. Also have in mind that the color components are stored in reverse order, i.e. the Red component is in the lowest-byte. In short, the format is 0x00BBGGRR. You can use a code similar to the following to obtain a COLORREF:

private static int MakeCOLORREF(byte r, byte g, byte b)
{
    return  (int) (((uint)r) | ( ((uint)g) <<8 ) |  ( ((uint)b) << 16 ));
}

Notes2:

For converting COLORREF from and to .net's Color use 'ColorTranslator.ToWin32' and 'ColorTranslator.FromWin32' methods.

Documentation
COLORREF on MSDN

Please edit this page!

Do you have...

  • helpful tips?
  • corrections to the existing content?
  • alternate definitions?
  • additional languages you want to include?

Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing any supporting types needed.

 
Access PInvoke.net directly from VS:
Terms of Use
Edit This Page
Find References
Show Printable Version
Revisions