1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
6 Various helper functions to work with RGB565 colors, including
7 color conversion, color blending, and a basic set of predefined
8 color constants (see colors.h).
12 Software License Agreement (BSD License)
14 Copyright (c) 2012, Kevin Townsend
17 Redistribution and use in source and binary forms, with or without
18 modification, are permitted provided that the following conditions are met:
19 1. Redistributions of source code must retain the above copyright
20 notice, this list of conditions and the following disclaimer.
21 2. Redistributions in binary form must reproduce the above copyright
22 notice, this list of conditions and the following disclaimer in the
23 documentation and/or other materials provided with the distribution.
24 3. Neither the name of the copyright holders nor the
25 names of its contributors may be used to endorse or promote products
26 derived from this software without specific prior written permission.
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
29 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
30 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
31 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
32 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
33 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 /**************************************************************************/
44 /**************************************************************************/
46 /* ----------------------- Private Methods ------------------------------ */
48 /**************************************************************************/
50 /**************************************************************************/
52 /* ----------------------- Public Methods ------------------------------- */
54 /**************************************************************************/
56 /**************************************************************************/
58 @brief Converts a 24-bit RGB color to an equivalent 16-bit RGB565 value
71 // Get 16-bit equivalent of 24-bit color
72 uint16_t gray = colorsRGB24toRGB565(0x33, 0x33, 0x33);
76 /**************************************************************************/
77 uint16_t colorsRGB24toRGB565(uint8_t r
, uint8_t g
, uint8_t b
)
79 return ((r
/ 8) << 11) | ((g
/ 4) << 5) | (b
/ 8);
82 /**************************************************************************/
84 @brief Converts a 16-bit RGB565 color to a standard 32-bit BGRA32
85 color (with alpha set to 0xFF)
94 // First convert 24-bit color to RGB565
95 uint16_t rgb565 = colorsRGB24toRGB565(0xFF, 0x00, 0x00);
97 // Convert RGB565 color back to BGRA32
98 uint32_t bgra32 = colorsRGB565toBGRA32(rgb565);
101 printf("BGRA32: 0x%08X R: %u G: %u B: %u A: %u \r\n",
103 (bgra32 & 0x000000FF), // Blue
104 (bgra32 & 0x0000FF00) >> 8, // Green
105 (bgra32 & 0x00FF0000) >> 16, // Red
106 (bgra32 & 0xFF000000) >> 24); // Alpha
110 /**************************************************************************/
111 uint32_t colorsRGB565toBGRA32(uint16_t color
)
113 uint32_t bits
= (uint32_t)color
;
114 uint32_t blue
= bits
& 0x001F; // 5 bits blue
115 uint32_t green
= bits
& 0x07E0; // 6 bits green
116 uint32_t red
= bits
& 0xF800; // 5 bits red
118 // Return shifted bits with alpha set to 0xFF
119 return (red
<< 8) | (green
<< 5) | (blue
<< 3) | 0xFF000000;
122 /**************************************************************************/
124 @brief Reverses a 16-bit color from BGR to RGB or vice verse
126 /**************************************************************************/
127 uint16_t colorsBGR2RGB(uint16_t color
)
131 b
= (color
>>0) & 0x1f;
132 g
= (color
>>5) & 0x3f;
133 r
= (color
>>11) & 0x1f;
135 return( (b
<<11) + (g
<<5) + (r
<<0) );
138 /**************************************************************************/
140 @brief Adjusts the supplied color to have the specified intensity
141 (0..100). 100 will leave the color as is at full intensity,
142 50 will reduce the color intensity by half, and 0 will return
145 This function is useful for anti-aliasing and sub-pixel
146 rendering since colors are returned as a percentage of
147 the original value, depending on the amount of space they
148 take up in the sub-pixel array.
153 Color intensity relative to the source (0..100)
159 #include "drivers/displays/tft/drawing.h"
160 #include "drivers/displays/tft/colors.h"
164 // Draw a pure red rectangle
165 drawRectangleFilled(10, 10, 200, 100, COLOR_RED);
167 // Draw a rectangle at 50% intensity red
168 newColor = colorsDim(COLOR_RED, 50);
169 drawRectangleFilled(20, 20, 190, 90, newColor);
171 // Draw a rectangle at 25% intensity red
172 newColor = colorsDim(COLOR_RED, 25);
173 drawRectangleFilled(30, 30, 180, 80, newColor);
175 // Draw a rectangle at 0% intensity red
176 newColor = colorsDim(COLOR_RED, 0);
177 drawRectangleFilled(40, 40, 170, 70, newColor);
181 /**************************************************************************/
182 uint16_t colorsDim(uint16_t color
, uint8_t intensity
)
184 uint16_t r
, g
, b
; // Individual component colors
186 // Add intensity adjusted forecolor
187 r
= ((((color
>> 11) & 0x1F) * intensity
) / 100) & 0x1F;
188 g
= ((((color
>> 5) & 0x3F) * intensity
) / 100) & 0x3F;
189 b
= (((color
& 0x1F) * intensity
) / 100) & 0x1F;
191 return (r
<< 11) | (g
<< 6) | b
;
194 /**************************************************************************/
196 @brief Returns an alpha-blended color based on the supplied bg color,
197 fore color, and intensity value (0..100).
199 This function is used when alpha-blending anti-aliased fonts
200 with an existing background image, amongst other things, and
201 can be used to create images that appear to be 'faded' or
202 semi-transparent, though at the expense of slow updates since
203 reading pixels from most LCD controllers is an extremely
207 Background color (rgb565)
209 Forground color (rgb565)
210 @param[in] fadePercent
211 Visibility of the background color in percent (0..100).
212 The higher the number, the more visible the back color
213 becomes. 100% signifies that the back color is entirely
214 visible (only the BG color is shown), 0% signifies
215 that only the fore color is shown, and 25% would
216 indicate that the background is visible at approximately
217 25% intensity (combined with 75% of the fore color).
223 #include "drivers/displays/tft/drawing.h"
224 #include "drivers/displays/tft/colors.h"
226 uint16_t bg = COLOR_GREEN;
227 uint16_t fore = COLOR_WHITE;
229 // Calculate the intermediate color with 25% bg blending
230 uint16_t result = colorsAlphaBlend(bg, fore, 25);
232 drawRectangleFilled(10, 10, 50, 50, bg);
233 drawRectangleFilled(60, 10, 100, 50, fore);
234 drawRectangleFilled(35, 60, 75, 100, result);
238 /**************************************************************************/
239 uint16_t colorsAlphaBlend(uint16_t bgColor
, uint16_t foreColor
, uint8_t fadePercent
)
241 uint16_t br
, bg
, bb
; // Background component colors
242 uint16_t fr
, fg
, fb
; // Foreground component colors
243 uint16_t newr
, newg
, newb
; // Blended component colors
245 if (fadePercent
> 100)
250 // Short cut if the color is full intensity
251 if (fadePercent
== 100)
254 // Note: This algorithm can definately be optimised!
256 // Break out component colors
257 br
= ((bgColor
>> 11) & 0x1F);
258 fr
= ((foreColor
>> 11) & 0x1F);
259 bg
= ((bgColor
>> 5) & 0x3F);
260 fg
= ((foreColor
>> 5) & 0x3F);
261 bb
= (bgColor
& 0x1F);
262 fb
= (foreColor
& 0x1F);
264 // Z = intensity * bgcolor + (100 - intensity) * forecolor
265 newr
= (fadePercent
* br
+ (100 - fadePercent
) * fr
) / 100;
266 newg
= (fadePercent
* bg
+ (100 - fadePercent
) * fg
) / 200; // Need to use 5-bit green for accurate colors :(
267 newb
= (fadePercent
* bb
+ (100 - fadePercent
) * fb
) / 100;
269 return (newr
<< 11) | (newg
<< 6) | newb
;
This page took 0.065066 seconds and 5 git commands to generate.