color.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * File : color.h
  3. * This file is part of RT-Thread GUI Engine
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-10-16 Bernard first version
  23. * 2012-01-24 onelife add mono color support
  24. */
  25. #ifndef __RTGUI_COLOR_H__
  26. #define __RTGUI_COLOR_H__
  27. #include <rtgui/rtgui.h>
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /*
  32. * The color used in the GUI:
  33. *
  34. * bit bit
  35. * RGB565 15 R,G,B 0
  36. * BGR565 15 B,G,R 0
  37. * RGB888 23 R,G,B 0
  38. * ARGB888 31 A,R,G,B 0
  39. * RGBA888 31 R,G,B,A 0
  40. * ABGR888 31 A,B,G,R 0
  41. *
  42. * The rtgui_color is defined as ARGB888.
  43. * bit31 A,R,G,B bit0
  44. */
  45. #define RTGUI_ARGB(a, r, g, b) \
  46. ((rtgui_color_t)(((rt_uint8_t)(b)|\
  47. (((unsigned long)(rt_uint8_t)(g))<<8))|\
  48. (((unsigned long)(rt_uint8_t)(r))<<16)|\
  49. (((unsigned long)(rt_uint8_t)(a))<<24)))
  50. #define RTGUI_RGB(r, g, b) RTGUI_ARGB(255, (r), (g), (b))
  51. #define RTGUI_RGB_B(c) ((c) & 0xff)
  52. #define RTGUI_RGB_G(c) (((c) >> 8) & 0xff)
  53. #define RTGUI_RGB_R(c) (((c) >> 16) & 0xff)
  54. #define RTGUI_RGB_A(c) (((c) >> 24) & 0xff)
  55. extern const rtgui_color_t default_foreground;
  56. extern const rtgui_color_t default_background;
  57. /* it's better use these color definitions */
  58. #define RED RTGUI_RGB(0xff, 0x00, 0x00)
  59. #define GREEN RTGUI_RGB(0x00, 0xff, 0x00)
  60. #define BLUE RTGUI_RGB(0x00, 0x00, 0xff)
  61. #define BLACK RTGUI_RGB(0x00, 0x00, 0x00)
  62. #define WHITE RTGUI_RGB(0xff, 0xff, 0xff)
  63. #define HIGH_LIGHT RTGUI_RGB(0xfc, 0xfc, 0xfc)
  64. #define DARK_GREY RTGUI_RGB(0x7f, 0x7f, 0x7f)
  65. #define LIGHT_GREY RTGUI_RGB(0xc0, 0xc0, 0xc0)
  66. #ifdef TRANSPARENT
  67. #undef TRANSPARENT
  68. #define TRANSPARENT RTGUI_ARGB(0, 0, 0, 0)
  69. #endif
  70. extern const rtgui_color_t red;
  71. extern const rtgui_color_t green;
  72. extern const rtgui_color_t blue;
  73. extern const rtgui_color_t black;
  74. extern const rtgui_color_t white;
  75. extern const rtgui_color_t high_light;
  76. extern const rtgui_color_t dark_grey;
  77. extern const rtgui_color_t light_grey;
  78. /*
  79. * RTGUI default color format: ARGB
  80. * AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB
  81. * 31 0
  82. */
  83. /* convert rtgui color to mono */
  84. rt_inline rt_uint8_t rtgui_color_to_mono(rtgui_color_t c)
  85. {
  86. rt_uint8_t pixel;
  87. pixel = (RTGUI_RGB_R(c) | RTGUI_RGB_G(c) | RTGUI_RGB_B(c)) ? 0x01 : 0x00;
  88. return pixel;
  89. }
  90. rt_inline rtgui_color_t rtgui_color_from_mono(rt_uint8_t pixel)
  91. {
  92. rtgui_color_t color;
  93. if (pixel)
  94. {
  95. color = white;
  96. }
  97. else
  98. {
  99. color = black;
  100. }
  101. return color;
  102. }
  103. /* convert rtgui color to RRRRRGGGGGGBBBBB */
  104. rt_inline rt_uint16_t rtgui_color_to_565(rtgui_color_t c)
  105. {
  106. rt_uint16_t pixel;
  107. pixel = (rt_uint16_t)(((RTGUI_RGB_R(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_B(c) >> 3));
  108. return pixel;
  109. }
  110. rt_inline rtgui_color_t rtgui_color_from_565(rt_uint16_t pixel)
  111. {
  112. rt_uint16_t r, g, b;
  113. rtgui_color_t color;
  114. r = (pixel >> 11) & 0x1f;
  115. g = (pixel >> 5) & 0x3f;
  116. b = pixel & 0x1f;
  117. color = b * 255 / 31 + ((g * 255 / 63) << 8) + ((r * 255 / 31) << 16);
  118. return color;
  119. }
  120. /* convert rtgui color to BBBBBGGGGGGRRRRR */
  121. rt_inline rt_uint16_t rtgui_color_to_565p(rtgui_color_t c)
  122. {
  123. rt_uint16_t pixel;
  124. pixel = (rt_uint16_t)(((RTGUI_RGB_B(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_R(c) >> 3));
  125. return pixel;
  126. }
  127. rt_inline rtgui_color_t rtgui_color_from_565p(rt_uint16_t pixel)
  128. {
  129. rt_uint8_t r, g, b;
  130. rtgui_color_t color;
  131. r = pixel & 0x1f;
  132. g = (pixel >> 5) & 0x3f;
  133. b = (pixel >> 11) & 0x1f;
  134. color = b * 255 / 31 + ((g * 255 / 63) << 8) + ((r * 255 / 31) << 16);
  135. return color;
  136. }
  137. /* convert rtgui color to RGB */
  138. rt_inline rt_uint32_t rtgui_color_to_888(rtgui_color_t c)
  139. {
  140. rt_uint32_t pixel;
  141. pixel = RTGUI_RGB_R(c) << 16 | RTGUI_RGB_G(c) << 8 | RTGUI_RGB_B(c);
  142. return pixel;
  143. }
  144. rt_inline rtgui_color_t rtgui_color_from_888(rt_uint32_t pixel)
  145. {
  146. rtgui_color_t color;
  147. color = RTGUI_RGB(((pixel >> 16) & 0xff), ((pixel >> 8) & 0xff), pixel & 0xff);
  148. return color;
  149. }
  150. /* get the bits of specified pixle format */
  151. rt_uint8_t rtgui_color_get_bits(rt_uint8_t pixel_format) RTGUI_PURE;
  152. /* get the bytes of specified pixle format */
  153. rt_uint8_t rtgui_color_get_bpp(rt_uint8_t pixel_format) RTGUI_PURE;
  154. #ifdef __cplusplus
  155. }
  156. #endif
  157. #endif