Send comments on this topic. | Back to Introduction - All Topics | Help Version 16.5.9.25
Using Color Values in LEADTOOLS

The standard Windows values for COLORREF represent either red, green, and blue color values, or an index into the image palette. A COLORREF value with the format 0x00BBGGRR represents the blue, green, and red color values for the specified pixel, where 0xBB is the blue value, 0xGG is the green value and 0xRR is the red value. If 0x01000000 is set in the COLORREF value (0x010000ZZ), the lower 8 bits (0xZZ) represent an index into the image palette which holds the color value. These COLORREF values can be used with any Windows function and macro that takes a COLORREF parameter. Please note that just because an image has a palette, that does not mean the returned COLORREF value is automatically an index.

LEADTOOLS for .NET uses a RasterColor structure to specifiy a color, this structures can be converted to a Windows COLORREF value with the RasterColor.ToArgb method. You can also create a RasterColor object from a COLORREF by using the RasterColor.FromArgb method.

In the Document/Medical Imaging editions, the COLORREF value may represent a 16 bit grayscale value if the RasterImage is a 12 or 16-bit grayscale image. So that the value is not confused with an RGB value, the mask (0x04000000) is set. In this case (0x0400YYYY), the lower 16 bits (0xYYYY) of the COLORREF value represent the 16-bit grayscale value. This is not a standard Windows value. Therefore, LEADTOOLS methods will recognize a COLORREF having this format, but Windows functions will not. To use a COLORREF value of this type in a non-LEADTOOLS function, you must do the following:


int GetColorRef(RasterImage image, int x, int y)
{
   RasterColor color = image.GetPixelColor(x, y);
   // convert to Windows COLORREF
   int clrref = color.ToArgb();
   const int COLORREF_GRAY16 = 0x04000000;
   if((clrref & COLORREF_GRAY16) == COLORREF_GRAY16)
   {
      // nonstandard COLORREF value, convert it to a windows COLORREF
      // get an 8 bit gray value corresponding to the 16 bit gray value
      byte gray = (byte)((clrref & 0xFFFF) >> 8);
      // use the equivalant of the standard Windows RGB macro to get a windows COLORREF
      clrref = RGB(gray, gray, gray);
   }

   return clrref;
}

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