In my windows desktop application development, both GDI+ and Direct2D are being used for drawing rectangles, lines, ellipses, etc. How do I convert from COLORREF (used in GDI+ for the brush) to D2D1::ColorF (used in Direct2D for the brush) and vice-versa?
Code Sample:
In GDI+, in order to fill a rectangle with a specific colour,
PAINTSTRUCT ps;
HDC hdc;
hdc = BeginPaint(hWnd, &ps);
HBRUSH brush;
brush = ::CreateSolidBrush(*pColor); //COLORREF* pColor;
::FillRect(hdc, &ps.rcPaint, brush);
::DeleteObject((HGDIOBJ) brush);
EndPaint(hWnd, &ps);
In Direct2D, drawing an ellipse with a specific colour,
pRenderTarget->BeginDraw(); //type ID2D1SolidColorBrush - alpha value (4th parameter) is omitted brush->SetColor(D2D1::ColorF(0.5f,0.5f,0.5f)); const D2D1_ELLIPSE circle1 = D2D1::Ellipse( D2D1::Point2F(pt1, pt2, 3.0f, 1.0f); pRenderTarget->DrawEllipse(circle1, brush); pRenderTarget->EndDraw();
Both are built from three color components: R, G, B.
ColorF(FLOAT, FLOAT, FLOAT, FLOAT)(
FLOAT r,
FLOAT g,
FLOAT b,
FLOAT a = 1.0
);
To create a COLORREF color value, use the RGB macro. To extract the individual values for the red, green, and blue components of a color value, use the GetRValue, GetGValue, and GetBValue macros, respectively.
COLORREF RGB(
BYTE byRed,
BYTE byGreen,
BYTE byBlue
);
Specifically, you can use GetRValue
, GetGValue
, and GetBValue
macros to obtain individual components from COLORREF
and then use them in the ColorF
constructor.