OWLNext    7.0
Borland's Object Windows Library for the modern age
Loading...
Searching...
No Matches
gdiplus.h
Go to the documentation of this file.
1//
2/// \file gdiplus.h
3/// Utilities for GDI+ interoperability
4//
5// Part of OWLNext - the next generation Object Windows Library
6// Copyright (c) 2014 Vidar Hasfjord
7//
8// For more information, including license details, see
9// http://owlnext.sourceforge.net
10//
11
12#ifndef OWL_GDIPLUS_H
13#define OWL_GDIPLUS_H
14
15#include <owl/geometry.h>
16#include <owl/color.h>
17
18//
19// GDI+ assumes Windows' macros `min` and `max` are defined, but these are
20// disabled by OWLNext. We pull in std::min and max instead here.
21//
22#include <algorithm>
23using std::min;
24using std::max;
25
26#include <gdiplus.h>
27#include <cmath>
28
29#pragma comment(lib, "gdiplus")
30
31namespace owl {
32
33//
34/// ConvertToGdiplus template;
35/// Converts any of TSize, TPoint and TRect to its counter-part in GDI+;
36/// Gdiplus::Size/SizeF, Point/PointF and Rect/RectF.
37/// @{
38//
39template <class TDest, class T>
41
42template <>
43inline Gdiplus::Size ConvertToGdiplus<Gdiplus::Size>(const TSize& v)
44{
45 return Gdiplus::Size(v.cx, v.cy);
46}
47
48template <>
49inline Gdiplus::SizeF ConvertToGdiplus<Gdiplus::SizeF>(const TSize& v)
50{
51 return Gdiplus::SizeF(static_cast<Gdiplus::REAL>(v.cx), static_cast<Gdiplus::REAL>(v.cy));
52}
53
54template <>
55inline Gdiplus::Point ConvertToGdiplus<Gdiplus::Point>(const TPoint& v)
56{
57 return Gdiplus::Point(v.x, v.y);
58}
59
60template <>
61inline Gdiplus::PointF ConvertToGdiplus<Gdiplus::PointF>(const TPoint& v)
62{
63 return Gdiplus::PointF(static_cast<Gdiplus::REAL>(v.x), static_cast<Gdiplus::REAL>(v.y));
64}
65
66template <>
67inline Gdiplus::Rect ConvertToGdiplus<Gdiplus::Rect>(const TRect& v)
68{
69 return Gdiplus::Rect(v.left, v.top, v.Width(), v.Height());
70}
71
72template <>
73inline Gdiplus::RectF ConvertToGdiplus<Gdiplus::RectF>(const TRect& v)
74{
75 return Gdiplus::RectF(
76 static_cast<Gdiplus::REAL>(v.left),
77 static_cast<Gdiplus::REAL>(v.top),
78 static_cast<Gdiplus::REAL>(v.Width()),
79 static_cast<Gdiplus::REAL>(v.Height())
80 );
81}
82
83/// @}
84
85//
86/// ConvertToGdiplus overload;
87/// Converts TColor to Gdiplus::Color with the given alpha channel. If the
88/// latter is not specified, the alpha channel is fully saturated (opaque).
89//
90inline Gdiplus::Color ConvertToGdiplus(const TColor& v, BYTE alphaChannel = 255)
91{
92 return Gdiplus::Color(alphaChannel, v.Red(), v.Green(), v.Blue());
93}
94
95//
96/// ConvertFromGdiplus overloads;
97/// Converts any of Gdiplus::Size/SizeF, Point/PointF, Rect/RectF and Color to
98/// its counter-part in OWLNext; TSize, TPoint, TRect and TColor.
99/// @{
100//
101inline TSize ConvertFromGdiplus(const Gdiplus::Size& v)
102{
103 return TSize(v.Width, v.Height);
104}
105
106inline TSize ConvertFromGdiplus(const Gdiplus::SizeF& v)
107{
108 return TSize(std::lround(v.Width), std::lround(v.Height));
109}
110
111inline TPoint ConvertFromGdiplus(const Gdiplus::Point& v)
112{
113 return TPoint(v.X, v.Y);
114}
115
116inline TPoint ConvertFromGdiplus(const Gdiplus::PointF& v)
117{
118 return TPoint(std::lround(v.X), std::lround(v.Y));
119}
120
121inline TRect ConvertFromGdiplus(const Gdiplus::Rect& v)
122{
123 return TRect(TPoint(v.X, v.Y), TSize(v.Width, v.Height));
124}
125
126inline TRect ConvertFromGdiplus(const Gdiplus::RectF& v)
127{
128 return TRect(
129 TPoint(std::lround(v.X), std::lround(v.Y)),
130 TSize(std::lround(v.Width), std::lround(v.Height)));
131}
132
133//
134/// Converts Gdiplus::Color to TColor. Note that the alpha channel of the color
135/// value is discarded, since TColor does not support alpha blending.
136//
137inline TColor ConvertFromGdiplus(const Gdiplus::Color& v)
138{
139 return TColor(v.GetRed(), v.GetGreen(), v.GetBlue());
140}
141
142/// @}
143
144//
145/// gdiplus_cast template;
146/// Simulates a cast operator that can convert to and from any of OWLNext types
147/// TSize, TPoint, TRect and TColor, and its counter-part in GDI+;
148/// Gdiplus::Size/SizeF, Point/PointF, Rect/RectF and Color. This template is
149/// an alternative to the more specific conversion functions ConvertToGdiplus
150/// and ConvertFromGdiplus.
151/// @{
152//
153template <class TDest, class T>
155
156template <>
157inline Gdiplus::Size gdiplus_cast<Gdiplus::Size>(const TSize& v)
158{
160}
161
162template <>
163inline Gdiplus::SizeF gdiplus_cast<Gdiplus::SizeF>(const TSize& v)
164{
166}
167
168template <>
169inline Gdiplus::Point gdiplus_cast<Gdiplus::Point>(const TPoint& v)
170{
172}
173
174template <>
175inline Gdiplus::PointF gdiplus_cast<Gdiplus::PointF>(const TPoint& v)
176{
178}
179
180template <>
181inline Gdiplus::Rect gdiplus_cast<Gdiplus::Rect>(const TRect& v)
182{
184}
185
186template <>
187inline Gdiplus::RectF gdiplus_cast<Gdiplus::RectF>(const TRect& v)
188{
190}
191
192template <>
193inline Gdiplus::Color gdiplus_cast<Gdiplus::Color>(const TColor& v)
194{
195 return ConvertToGdiplus(v, 255);
196}
197
198template <>
199inline TSize gdiplus_cast<TSize>(const Gdiplus::Size& v)
200{
201 return ConvertFromGdiplus(v);
202}
203
204template <>
205inline TSize gdiplus_cast<TSize>(const Gdiplus::SizeF& v)
206{
207 return ConvertFromGdiplus(v);
208}
209
210template <>
211inline TPoint gdiplus_cast<TPoint>(const Gdiplus::Point& v)
212{
213 return ConvertFromGdiplus(v);
214}
215
216template <>
217inline TPoint gdiplus_cast<TPoint>(const Gdiplus::PointF& v)
218{
219 return ConvertFromGdiplus(v);
220}
221
222template <>
223inline TRect gdiplus_cast<TRect>(const Gdiplus::Rect& v)
224{
225 return ConvertFromGdiplus(v);
226}
227
228template <>
229inline TRect gdiplus_cast<TRect>(const Gdiplus::RectF& v)
230{
231 return ConvertFromGdiplus(v);
232}
233
234template <>
235inline TColor gdiplus_cast<TColor>(const Gdiplus::Color& v)
236{
237 return ConvertFromGdiplus(v);
238}
239
240/// @}
241
242} // OWL namespace
243
244#endif
Class wrapper for management of color values.
Definition color.h:245
TPoint is a support class, derived from tagPOINT.
Definition geometry.h:87
TRect is a mathematical class derived from tagRect.
Definition geometry.h:308
The tagSIZE struct is defined as.
Definition geometry.h:234
Definition of windowing system color classes.
Utilities for GDI+ interoperability.
Classes for window system geometry.
Object Windows Library (OWLNext Core)
Definition animctrl.h:22
Gdiplus::SizeF ConvertToGdiplus< Gdiplus::SizeF >(const TSize &v)
Definition gdiplus.h:49
Gdiplus::SizeF gdiplus_cast< Gdiplus::SizeF >(const TSize &v)
Definition gdiplus.h:163
Gdiplus::Color gdiplus_cast< Gdiplus::Color >(const TColor &v)
Definition gdiplus.h:193
Gdiplus::Rect gdiplus_cast< Gdiplus::Rect >(const TRect &v)
Definition gdiplus.h:181
Gdiplus::RectF ConvertToGdiplus< Gdiplus::RectF >(const TRect &v)
Definition gdiplus.h:73
TDest ConvertToGdiplus(const T &)
ConvertToGdiplus template; Converts any of TSize, TPoint and TRect to its counter-part in GDI+; Gdipl...
TSize gdiplus_cast< TSize >(const Gdiplus::Size &v)
Definition gdiplus.h:199
TColor gdiplus_cast< TColor >(const Gdiplus::Color &v)
Definition gdiplus.h:235
Gdiplus::PointF ConvertToGdiplus< Gdiplus::PointF >(const TPoint &v)
Definition gdiplus.h:61
Gdiplus::Size gdiplus_cast< Gdiplus::Size >(const TSize &v)
Definition gdiplus.h:157
Gdiplus::Point gdiplus_cast< Gdiplus::Point >(const TPoint &v)
Definition gdiplus.h:169
TSize ConvertFromGdiplus(const Gdiplus::Size &v)
ConvertFromGdiplus overloads; Converts any of Gdiplus::Size/SizeF, Point/PointF, Rect/RectF and Color...
Definition gdiplus.h:101
Gdiplus::Rect ConvertToGdiplus< Gdiplus::Rect >(const TRect &v)
Definition gdiplus.h:67
Gdiplus::RectF gdiplus_cast< Gdiplus::RectF >(const TRect &v)
Definition gdiplus.h:187
Gdiplus::Size ConvertToGdiplus< Gdiplus::Size >(const TSize &v)
Definition gdiplus.h:43
Gdiplus::Point ConvertToGdiplus< Gdiplus::Point >(const TPoint &v)
Definition gdiplus.h:55
TDest gdiplus_cast(const T &)
gdiplus_cast template; Simulates a cast operator that can convert to and from any of OWLNext types TS...
Gdiplus::PointF gdiplus_cast< Gdiplus::PointF >(const TPoint &v)
Definition gdiplus.h:175
TPoint gdiplus_cast< TPoint >(const Gdiplus::Point &v)
Definition gdiplus.h:211
TRect gdiplus_cast< TRect >(const Gdiplus::Rect &v)
Definition gdiplus.h:223