color.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * File : color.h
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2009-10-16 Bernard first version
  13. * 2012-01-24 onelife add mono color support
  14. */
  15. #ifndef __RTGUI_COLOR_H__
  16. #define __RTGUI_COLOR_H__
  17. #include <rtgui/rtgui.h>
  18. #ifdef __cplusplus
  19. extern "C" {
  20. #endif
  21. /*
  22. * The color used in the GUI:
  23. *
  24. * bit bit
  25. * RGB565 15 R,G,B 0
  26. * BGR565 15 B,G,R 0
  27. * RGB888 23 R,G,B 0
  28. * ARGB888 31 A,R,G,B 0
  29. * RGBA888 31 R,G,B,A 0
  30. * ABGR888 31 A,B,G,R 0
  31. *
  32. * The rtgui_color is defined as ARGB888.
  33. * bit31 A,R,G,B bit0
  34. */
  35. #define RTGUI_ARGB(a, r, g, b) \
  36. ((rtgui_color_t)(((rt_uint8_t)(b)|\
  37. (((unsigned long)(rt_uint8_t)(g))<<8))|\
  38. (((unsigned long)(rt_uint8_t)(r))<<16)|\
  39. (((unsigned long)(rt_uint8_t)(a))<<24)))
  40. #define RTGUI_RGB(r, g, b) RTGUI_ARGB(255, (r), (g), (b))
  41. #define RTGUI_RGB_B(c) ((c) & 0xff)
  42. #define RTGUI_RGB_G(c) (((c) >> 8) & 0xff)
  43. #define RTGUI_RGB_R(c) (((c) >> 16) & 0xff)
  44. #define RTGUI_RGB_A(c) (((c) >> 24) & 0xff)
  45. extern const rtgui_color_t default_foreground;
  46. extern const rtgui_color_t default_background;
  47. /* it's better use these color definitions */
  48. #define RED RTGUI_RGB(0xff, 0x00, 0x00)
  49. #define GREEN RTGUI_RGB(0x00, 0xff, 0x00)
  50. #define BLUE RTGUI_RGB(0x00, 0x00, 0xff)
  51. #define BLACK RTGUI_RGB(0x00, 0x00, 0x00)
  52. #define WHITE RTGUI_RGB(0xff, 0xff, 0xff)
  53. #define HIGH_LIGHT RTGUI_RGB(0xfc, 0xfc, 0xfc)
  54. #define DARK_GREY RTGUI_RGB(0x7f, 0x7f, 0x7f)
  55. #define LIGHT_GREY RTGUI_RGB(0xc0, 0xc0, 0xc0)
  56. #define TRANSPARENT RTGUI_ARGB(0, 0, 0, 0)
  57. extern const rtgui_color_t red;
  58. extern const rtgui_color_t green;
  59. extern const rtgui_color_t blue;
  60. extern const rtgui_color_t black;
  61. extern const rtgui_color_t white;
  62. extern const rtgui_color_t high_light;
  63. extern const rtgui_color_t dark_grey;
  64. extern const rtgui_color_t light_grey;
  65. /*
  66. * RTGUI default color format: ARGB
  67. * AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB
  68. * 31 0
  69. */
  70. /* convert rtgui color to mono */
  71. rt_inline rt_uint8_t rtgui_color_to_mono(rtgui_color_t c)
  72. {
  73. rt_uint8_t pixel;
  74. pixel = (RTGUI_RGB_R(c) | RTGUI_RGB_G(c) | RTGUI_RGB_B(c)) ? 0x01 : 0x00;
  75. return pixel;
  76. }
  77. rt_inline rtgui_color_t rtgui_color_from_mono(rt_uint8_t pixel)
  78. {
  79. rtgui_color_t color;
  80. if (pixel)
  81. {
  82. color = white;
  83. }
  84. else
  85. {
  86. color = black;
  87. }
  88. return color;
  89. }
  90. /* convert rtgui color to RRRRRGGGGGGBBBBB */
  91. rt_inline rt_uint16_t rtgui_color_to_565(rtgui_color_t c)
  92. {
  93. rt_uint16_t pixel;
  94. pixel = (rt_uint16_t)(((RTGUI_RGB_R(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_B(c) >> 3));
  95. return pixel;
  96. }
  97. rt_inline rtgui_color_t rtgui_color_from_565(rt_uint16_t pixel)
  98. {
  99. rt_uint16_t r, g, b;
  100. rtgui_color_t color;
  101. r = (pixel >> 11) & 0x1f;
  102. g = (pixel >> 5) & 0x3f;
  103. b = pixel & 0x1f;
  104. color = b * 255 / 31 + ((g * 255 / 63) << 8) + ((r * 255 / 31) << 16);
  105. return color;
  106. }
  107. /* convert rtgui color to BBBBBGGGGGGRRRRR */
  108. rt_inline rt_uint16_t rtgui_color_to_565p(rtgui_color_t c)
  109. {
  110. rt_uint16_t pixel;
  111. pixel = (rt_uint16_t)(((RTGUI_RGB_B(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_R(c) >> 3));
  112. return pixel;
  113. }
  114. rt_inline rtgui_color_t rtgui_color_from_565p(rt_uint16_t pixel)
  115. {
  116. rt_uint8_t r, g, b;
  117. rtgui_color_t color;
  118. r = pixel & 0x1f;
  119. g = (pixel >> 5) & 0x3f;
  120. b = (pixel >> 11) & 0x1f;
  121. color = b * 255 / 31 + ((g * 255 / 63) << 8) + ((r * 255 / 31) << 16);
  122. return color;
  123. }
  124. /* convert rtgui color to RGB */
  125. rt_inline rt_uint32_t rtgui_color_to_888(rtgui_color_t c)
  126. {
  127. rt_uint32_t pixel;
  128. pixel = RTGUI_RGB_R(c) << 16 | RTGUI_RGB_G(c) << 8 | RTGUI_RGB_B(c);
  129. return pixel;
  130. }
  131. rt_inline rtgui_color_t rtgui_color_from_888(rt_uint32_t pixel)
  132. {
  133. rtgui_color_t color;
  134. color = RTGUI_RGB(((pixel >> 16) & 0xff), ((pixel >> 8) & 0xff), pixel & 0xff);
  135. return color;
  136. }
  137. /* get the bits of specified pixle format */
  138. rt_uint8_t rtgui_color_get_bits(rt_uint8_t pixel_format) RTGUI_PURE;
  139. /* get the bytes of specified pixle format */
  140. rt_uint8_t rtgui_color_get_bpp(rt_uint8_t pixel_format) RTGUI_PURE;
  141. #ifdef __cplusplus
  142. }
  143. #endif
  144. #endif