color.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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. #define TRANSPARENT RTGUI_ARGB(0, 0, 0, 0)
  67. extern const rtgui_color_t red;
  68. extern const rtgui_color_t green;
  69. extern const rtgui_color_t blue;
  70. extern const rtgui_color_t black;
  71. extern const rtgui_color_t white;
  72. extern const rtgui_color_t high_light;
  73. extern const rtgui_color_t dark_grey;
  74. extern const rtgui_color_t light_grey;
  75. /*
  76. * RTGUI default color format: ARGB
  77. * AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB
  78. * 31 0
  79. */
  80. /* convert rtgui color to mono */
  81. rt_inline rt_uint8_t rtgui_color_to_mono(rtgui_color_t c)
  82. {
  83. rt_uint8_t pixel;
  84. pixel = (RTGUI_RGB_R(c) | RTGUI_RGB_G(c) | RTGUI_RGB_B(c)) ? 0x01 : 0x00;
  85. return pixel;
  86. }
  87. rt_inline rtgui_color_t rtgui_color_from_mono(rt_uint8_t pixel)
  88. {
  89. rtgui_color_t color;
  90. if (pixel)
  91. {
  92. color = white;
  93. }
  94. else
  95. {
  96. color = black;
  97. }
  98. return color;
  99. }
  100. /* convert rtgui color to RRRRRGGGGGGBBBBB */
  101. rt_inline rt_uint16_t rtgui_color_to_565(rtgui_color_t c)
  102. {
  103. rt_uint16_t pixel;
  104. pixel = (rt_uint16_t)(((RTGUI_RGB_R(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_B(c) >> 3));
  105. return pixel;
  106. }
  107. rt_inline rtgui_color_t rtgui_color_from_565(rt_uint16_t pixel)
  108. {
  109. rt_uint16_t r, g, b;
  110. rtgui_color_t color;
  111. r = (pixel >> 11) & 0x1f;
  112. g = (pixel >> 5) & 0x3f;
  113. b = pixel & 0x1f;
  114. color = b * 255 / 31 + ((g * 255 / 63) << 8) + ((r * 255 / 31) << 16);
  115. return color;
  116. }
  117. /* convert rtgui color to BBBBBGGGGGGRRRRR */
  118. rt_inline rt_uint16_t rtgui_color_to_565p(rtgui_color_t c)
  119. {
  120. rt_uint16_t pixel;
  121. pixel = (rt_uint16_t)(((RTGUI_RGB_B(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_R(c) >> 3));
  122. return pixel;
  123. }
  124. rt_inline rtgui_color_t rtgui_color_from_565p(rt_uint16_t pixel)
  125. {
  126. rt_uint8_t r, g, b;
  127. rtgui_color_t color;
  128. r = pixel & 0x1f;
  129. g = (pixel >> 5) & 0x3f;
  130. b = (pixel >> 11) & 0x1f;
  131. color = b * 255 / 31 + ((g * 255 / 63) << 8) + ((r * 255 / 31) << 16);
  132. return color;
  133. }
  134. /* convert rtgui color to RGB */
  135. rt_inline rt_uint32_t rtgui_color_to_888(rtgui_color_t c)
  136. {
  137. rt_uint32_t pixel;
  138. pixel = RTGUI_RGB_R(c) << 16 | RTGUI_RGB_G(c) << 8 | RTGUI_RGB_B(c);
  139. return pixel;
  140. }
  141. rt_inline rtgui_color_t rtgui_color_from_888(rt_uint32_t pixel)
  142. {
  143. rtgui_color_t color;
  144. color = RTGUI_RGB(((pixel >> 16) & 0xff), ((pixel >> 8) & 0xff), pixel & 0xff);
  145. return color;
  146. }
  147. /* get the bits of specified pixle format */
  148. rt_uint8_t rtgui_color_get_bits(rt_uint8_t pixel_format) RTGUI_PURE;
  149. /* get the bytes of specified pixle format */
  150. rt_uint8_t rtgui_color_get_bpp(rt_uint8_t pixel_format) RTGUI_PURE;
  151. #ifdef __cplusplus
  152. }
  153. #endif
  154. #endif