EFM32 Happy Gecko Software Documentation  efm32hg-doc-5.1.2
glib_rectangle.c
Go to the documentation of this file.
1  /*************************************************************************/
16 /* Standard C header files */
17 #include <stdint.h>
18 
19 /* EM types */
20 #include "em_types.h"
21 
22 /* GLIB header files */
23 #include "glib.h"
24 
25 /**************************************************************************/
41 bool GLIB_rectContainsPoint(const GLIB_Rectangle_t *pRect, int32_t x, int32_t y)
42 {
43  if ((pRect == NULL) ||
44  (x < pRect->xMin) ||
45  (x > pRect->xMax) ||
46  (y < pRect->yMin) ||
47  (y > pRect->yMax)) {
48  return false;
49  }
50  return true;
51 }
52 
53 /**************************************************************************/
67 {
68  int32_t swap;
69  if (pRect->xMin > pRect->xMax) {
70  swap = pRect->xMin;
71  pRect->xMin = pRect->xMax;
72  pRect->xMax = swap;
73  }
74 
75  if (pRect->yMin > pRect->yMax) {
76  swap = pRect->yMin;
77  pRect->yMin = pRect->yMax;
78  pRect->yMax = swap;
79  }
80 }
81 
82 /**************************************************************************/
96 EMSTATUS GLIB_drawRect(GLIB_Context_t *pContext, const GLIB_Rectangle_t *pRect)
97 {
98  EMSTATUS status;
99  GLIB_Rectangle_t tmpRectangle = *pRect;
100 
101  GLIB_normalizeRect(&tmpRectangle);
102 
103  /* Clip rectangle if necessary */
104  if (tmpRectangle.xMin < pContext->clippingRegion.xMin) {
105  tmpRectangle.xMin = pContext->clippingRegion.xMin;
106  }
107  if (tmpRectangle.xMax > pContext->clippingRegion.xMax) {
108  tmpRectangle.xMax = pContext->clippingRegion.xMax;
109  }
110  if (tmpRectangle.yMin < pContext->clippingRegion.yMin) {
111  tmpRectangle.yMin = pContext->clippingRegion.yMin;
112  }
113  if (tmpRectangle.yMax > pContext->clippingRegion.yMax) {
114  tmpRectangle.yMax = pContext->clippingRegion.yMax;
115  }
116 
117  /* Draw a line across the top of the rectangle */
118  status = GLIB_drawLineH(pContext, tmpRectangle.xMin, tmpRectangle.yMin, tmpRectangle.xMax);
119  if (status != GLIB_OK) return status;
120 
121  /* Check if the rectangle is one pixel tall */
122  if (tmpRectangle.yMin == tmpRectangle.yMax) {
123  return GLIB_OK;
124  }
125 
126  /* Draw the right side of the rectangle */
127  status = GLIB_drawLineV(pContext, tmpRectangle.xMax, tmpRectangle.yMin + 1, tmpRectangle.yMax);
128  if (status != GLIB_OK) return status;
129 
130  /* Check if the rectangle is one pixel wide */
131  if (tmpRectangle.xMin == tmpRectangle.xMax) {
132  return GLIB_OK;
133  }
134 
135  /* Draw a line across the bottom of the rectangle */
136  status = GLIB_drawLineH(pContext, tmpRectangle.xMin, tmpRectangle.yMax, tmpRectangle.xMax - 1);
137  if (status != GLIB_OK) return status;
138 
139  /* Return if the rectangle is two pixels tall */
140  if ((tmpRectangle.yMin + 1) == tmpRectangle.yMax) {
141  return GLIB_OK;
142  }
143 
144  /* Draw the left side of the rectangle */
145  return GLIB_drawLineV(pContext, tmpRectangle.xMin, tmpRectangle.yMin + 1, tmpRectangle.yMax - 1);
146 }
147 
148 /**************************************************************************/
162 EMSTATUS GLIB_drawRectFilled(GLIB_Context_t *pContext, const GLIB_Rectangle_t *pRect)
163 {
164  EMSTATUS status;
165  uint8_t red;
166  uint8_t green;
167  uint8_t blue;
168  int32_t width;
169  int32_t height;
170  GLIB_Rectangle_t tmpRectangle = *pRect;
171 
172  GLIB_normalizeRect(&tmpRectangle);
173 
174  /* Clip rectangle if necessary */
175  if (tmpRectangle.xMin < pContext->clippingRegion.xMin) {
176  tmpRectangle.xMin = pContext->clippingRegion.xMin;
177  }
178  if (tmpRectangle.xMax > pContext->clippingRegion.xMax) {
179  tmpRectangle.xMax = pContext->clippingRegion.xMax;
180  }
181  if (tmpRectangle.yMin < pContext->clippingRegion.yMin) {
182  tmpRectangle.yMin = pContext->clippingRegion.yMin;
183  }
184  if (tmpRectangle.yMax > pContext->clippingRegion.yMax) {
185  tmpRectangle.yMax = pContext->clippingRegion.yMax;
186  }
187 
188  /* Draw filled rectangle */
189  GLIB_colorTranslate24bpp(pContext->foregroundColor, &red, &green, &blue);
190 
191  width = tmpRectangle.xMax - tmpRectangle.xMin + 1;
192  height = tmpRectangle.yMax - tmpRectangle.yMin + 1;
193 
194  status = DMD_setClippingArea(tmpRectangle.xMin, tmpRectangle.yMin, width, height);
195  if (status != DMD_OK) return status;
196 
197  status = DMD_writeColor(0, 0, red, green, blue, width * height);
198  if (status != DMD_OK) return status;
199 
200  /* Reset driver clipping area to GLIB clipping region */
201  return GLIB_applyClippingRegion(pContext);
202 }
int32_t yMax
Definition: glib.h:266
int32_t yMin
Definition: glib.h:262
EMSTATUS GLIB_drawLineV(GLIB_Context_t *pContext, int32_t x1, int32_t y1, int32_t y2)
Draws a vertical line from x1, y1 to x1, y2.
Definition: glib_line.c:113
EMSTATUS GLIB_drawRect(GLIB_Context_t *pContext, const GLIB_Rectangle_t *pRect)
Draws a rectangle outline defined by the passed in rectangle.
Rectangle structure.
Definition: glib.h:257
EMSTATUS GLIB_drawRectFilled(GLIB_Context_t *pContext, const GLIB_Rectangle_t *pRect)
Draws a filled rectangle defined by the passed in rectangle. The filled rectangle is drawn from (xMin...
uint32_t foregroundColor
Definition: glib.h:282
Silicon Labs Graphics Library.
void GLIB_colorTranslate24bpp(uint32_t color, uint8_t *red, uint8_t *green, uint8_t *blue)
Extracts the color components from the 32-bit color passed and puts them in the passed in 8-bits ints...
Definition: glib.c:309
#define GLIB_OK
Definition: glib.h:190
EMSTATUS GLIB_drawLineH(GLIB_Context_t *pContext, int32_t x1, int32_t y1, int32_t x2)
Draws a horizontal line from x1, y1 to x2, y2.
Definition: glib_line.c:49
EMSTATUS GLIB_applyClippingRegion(const GLIB_Context_t *pContext)
Apply the clipping region from the GLIB_Context_t in the DMD driver.
Definition: glib.c:278
void GLIB_normalizeRect(GLIB_Rectangle_t *pRect)
Normalize the rectangle that is passed in. Sets yMin to the minimum value of yMin and yMax...
bool GLIB_rectContainsPoint(const GLIB_Rectangle_t *pRect, int32_t x, int32_t y)
Checks if the point passed in is in the interior of the rectangle passed in.
int32_t xMax
Definition: glib.h:264
GLIB_Rectangle_t clippingRegion
Definition: glib.h:285
int32_t xMin
Definition: glib.h:260
GLIB Drawing Context (Multiple instances of GLIB_Context_t can exist)
Definition: glib.h:273