lv_color.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /**
  2. * @file lv_color.h
  3. *
  4. */
  5. #ifndef LV_COLOR_H
  6. #define LV_COLOR_H
  7. #ifdef __cplusplus
  8. extern "C" {
  9. #endif
  10. /*********************
  11. * INCLUDES
  12. *********************/
  13. #include "../../lv_conf.h"
  14. #include <stdint.h>
  15. /*********************
  16. * DEFINES
  17. *********************/
  18. #define LV_COLOR_BLACK LV_COLOR_MAKE(0x00,0x00,0x00)
  19. #define LV_COLOR_WHITE LV_COLOR_MAKE(0xFF,0xFF,0xFF)
  20. #define LV_COLOR_RED LV_COLOR_MAKE(0xFF,0x00,0x00)
  21. #define LV_COLOR_LIME LV_COLOR_MAKE(0x00,0xFF,0x00)
  22. #define LV_COLOR_BLUE LV_COLOR_MAKE(0x00,0x00,0xFF)
  23. #define LV_COLOR_YELLOW LV_COLOR_MAKE(0xFF,0xFF,0x00)
  24. #define LV_COLOR_CYAN LV_COLOR_MAKE(0x00,0xFF,0xFF)
  25. #define LV_COLOR_AQUA LV_COLOR_CYAN
  26. #define LV_COLOR_MAGENTA LV_COLOR_MAKE(0xFF,0x00,0xFF)
  27. #define LV_COLOR_SILVER LV_COLOR_MAKE(0xC0,0xC0,0xC0)
  28. #define LV_COLOR_GRAY LV_COLOR_MAKE(0x80,0x80,0x80)
  29. #define LV_COLOR_MARRON LV_COLOR_MAKE(0x80,0x00,0x00)
  30. #define LV_COLOR_OLIVE LV_COLOR_MAKE(0x80,0x80,0x00)
  31. #define LV_COLOR_GREEN LV_COLOR_MAKE(0x00,0x80,0x00)
  32. #define LV_COLOR_PURPLE LV_COLOR_MAKE(0x80,0x00,0x80)
  33. #define LV_COLOR_TEAL LV_COLOR_MAKE(0x00,0x80,0x80)
  34. #define LV_COLOR_NAVY LV_COLOR_MAKE(0x00,0x00,0x80)
  35. #define LV_COLOR_ORANGE LV_COLOR_MAKE(0xFF,0xA5,0x00)
  36. #define LV_OPA_TRANSP 0
  37. #define LV_OPA_0 0
  38. #define LV_OPA_10 25
  39. #define LV_OPA_20 51
  40. #define LV_OPA_30 76
  41. #define LV_OPA_40 102
  42. #define LV_OPA_50 127
  43. #define LV_OPA_60 153
  44. #define LV_OPA_70 178
  45. #define LV_OPA_80 204
  46. #define LV_OPA_90 229
  47. #define LV_OPA_100 255
  48. #define LV_OPA_COVER 255
  49. /**********************
  50. * TYPEDEFS
  51. **********************/
  52. typedef union
  53. {
  54. uint8_t blue :1;
  55. uint8_t green :1;
  56. uint8_t red :1;
  57. uint8_t full :1;
  58. }lv_color1_t;
  59. typedef union
  60. {
  61. struct
  62. {
  63. uint8_t blue :2;
  64. uint8_t green :3;
  65. uint8_t red :3;
  66. };
  67. uint8_t full;
  68. }lv_color8_t;
  69. typedef union
  70. {
  71. struct
  72. {
  73. uint16_t blue :5;
  74. uint16_t green :6;
  75. uint16_t red :5;
  76. };
  77. uint16_t full;
  78. }lv_color16_t;
  79. typedef union
  80. {
  81. struct
  82. {
  83. uint8_t blue;
  84. uint8_t green;
  85. uint8_t red;
  86. uint8_t alpha;
  87. };
  88. uint32_t full;
  89. }lv_color24_t;
  90. #if LV_COLOR_DEPTH == 1
  91. typedef uint8_t lv_color_int_t;
  92. typedef lv_color1_t lv_color_t;
  93. #elif LV_COLOR_DEPTH == 8
  94. typedef uint8_t lv_color_int_t;
  95. typedef lv_color8_t lv_color_t;
  96. #elif LV_COLOR_DEPTH == 16
  97. typedef uint16_t lv_color_int_t;
  98. typedef lv_color16_t lv_color_t;
  99. #elif LV_COLOR_DEPTH == 24
  100. typedef uint32_t lv_color_int_t;
  101. typedef lv_color24_t lv_color_t;
  102. #else
  103. #error "Invalid LV_COLOR_DEPTH in misc_conf.h! Set it to 1, 8, 16 or 24!"
  104. #endif
  105. typedef uint8_t lv_opa_t;
  106. typedef struct
  107. {
  108. uint16_t h;
  109. uint8_t s;
  110. uint8_t v;
  111. } lv_color_hsv_t;
  112. /**********************
  113. * GLOBAL PROTOTYPES
  114. **********************/
  115. /*In color conversations:
  116. * - When converting to bigger color type the LSB weight of 1 LSB is calculated
  117. * E.g. 16 bit Red has 5 bits
  118. * 8 bit Red has 2 bits
  119. * ----------------------
  120. * 8 bit red LSB = (2^5 - 1) / (2^2 - 1) = 31 / 3 = 10
  121. *
  122. * - When calculating to smaller color type simply shift out the LSBs
  123. * E.g. 8 bit Red has 2 bits
  124. * 16 bit Red has 5 bits
  125. * ----------------------
  126. * Shift right with 5 - 3 = 2
  127. */
  128. static inline uint8_t lv_color_to1(lv_color_t color)
  129. {
  130. #if LV_COLOR_DEPTH == 1
  131. return color.full;
  132. #elif LV_COLOR_DEPTH == 8
  133. if((color.red & 0x4) ||
  134. (color.green & 0x4) ||
  135. (color.blue & 0x2)) {
  136. return 1;
  137. } else {
  138. return 0;
  139. }
  140. #elif LV_COLOR_DEPTH == 16
  141. if((color.red & 0x10) ||
  142. (color.green & 0x20) ||
  143. (color.blue & 0x10)) {
  144. return 1;
  145. } else {
  146. return 0;
  147. }
  148. #elif LV_COLOR_DEPTH == 24
  149. if((color.red & 0x80) ||
  150. (color.green & 0x80) ||
  151. (color.blue & 0x80)) {
  152. return 1;
  153. } else {
  154. return 0;
  155. }
  156. #endif
  157. }
  158. static inline uint8_t lv_color_to8(lv_color_t color)
  159. {
  160. #if LV_COLOR_DEPTH == 1
  161. if(color.full == 0) return 0;
  162. else return 0xFF;
  163. #elif LV_COLOR_DEPTH == 8
  164. return color.full;
  165. #elif LV_COLOR_DEPTH == 16
  166. lv_color8_t ret;
  167. ret.red = color.red >> 2; /* 5 - 3 = 2*/
  168. ret.green = color.green >> 3; /* 6 - 3 = 3*/
  169. ret.blue = color.blue >> 3; /* 5 - 2 = 3*/
  170. return ret.full;
  171. #elif LV_COLOR_DEPTH == 24
  172. lv_color8_t ret;
  173. ret.red = color.red >> 5; /* 8 - 3 = 5*/
  174. ret.green = color.green >> 5; /* 8 - 3 = 5*/
  175. ret.blue = color.blue >> 6; /* 8 - 2 = 6*/
  176. return ret.full;
  177. #endif
  178. }
  179. static inline uint16_t lv_color_to16(lv_color_t color)
  180. {
  181. #if LV_COLOR_DEPTH == 1
  182. if(color.full == 0) return 0;
  183. else return 0xFFFF;
  184. #elif LV_COLOR_DEPTH == 8
  185. lv_color16_t ret;
  186. ret.red = color.red * 4; /*(2^5 - 1)/(2^3 - 1) = 31/7 = 4*/
  187. ret.green = color.green * 9; /*(2^6 - 1)/(2^3 - 1) = 63/7 = 9*/
  188. ret.blue = color.blue * 10; /*(2^5 - 1)/(2^2 - 1) = 31/3 = 10*/
  189. return ret.full;
  190. #elif LV_COLOR_DEPTH == 16
  191. return color.full;
  192. #elif LV_COLOR_DEPTH == 24
  193. lv_color16_t ret;
  194. ret.red = color.red >> 3; /* 8 - 5 = 3*/
  195. ret.green = color.green >> 2; /* 8 - 6 = 2*/
  196. ret.blue = color.blue >> 3; /* 8 - 5 = 3*/
  197. return ret.full;
  198. #endif
  199. }
  200. static inline uint32_t lv_color_to24(lv_color_t color)
  201. {
  202. #if LV_COLOR_DEPTH == 1
  203. if(color.full == 0) return 0;
  204. else return 0xFFFFFFFF;
  205. #elif LV_COLOR_DEPTH == 8
  206. lv_color24_t ret;
  207. ret.red = color.red * 36; /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
  208. ret.green = color.green * 36; /*(2^8 - 1)/(2^3 - 1) = 255/7 = 36*/
  209. ret.blue = color.blue * 85; /*(2^8 - 1)/(2^2 - 1) = 255/3 = 85*/
  210. ret.alpha = 0xFF;
  211. return ret.full;
  212. #elif LV_COLOR_DEPTH == 16
  213. lv_color24_t ret;
  214. ret.red = color.red * 8; /*(2^8 - 1)/(2^5 - 1) = 255/31 = 8*/
  215. ret.green = color.green * 4; /*(2^8 - 1)/(2^6 - 1) = 255/63 = 4*/
  216. ret.blue = color.blue * 8; /*(2^8 - 1)/(2^5 - 1) = 255/31 = 8*/
  217. ret.alpha = 0xFF;
  218. return ret.full;
  219. #elif LV_COLOR_DEPTH == 24
  220. return color.full;
  221. #endif
  222. }
  223. static inline lv_color_t lv_color_mix(lv_color_t c1, lv_color_t c2, uint8_t mix)
  224. {
  225. lv_color_t ret;
  226. #if LV_COLOR_DEPTH != 1
  227. ret.red = (uint16_t)((uint16_t) c1.red * mix + (c2.red * (255 - mix))) >> 8;
  228. ret.green = (uint16_t)((uint16_t) c1.green * mix + (c2.green * (255 - mix))) >> 8;
  229. ret.blue = (uint16_t)((uint16_t) c1.blue * mix + (c2.blue * (255 - mix))) >> 8;
  230. # if LV_COLOR_DEPTH == 24
  231. ret.alpha = 0xFF;
  232. # endif
  233. #else
  234. ret.full = mix > LV_OPA_50 ? c1.full : c2.full;
  235. #endif
  236. return ret;
  237. }
  238. /**
  239. * Get the brightness of a color
  240. * @param color a color
  241. * @return the brightness [0..255]
  242. */
  243. static inline uint8_t lv_color_brightness(lv_color_t color)
  244. {
  245. lv_color24_t c24;
  246. c24.full = lv_color_to24(color);
  247. uint16_t bright = 3 * c24.red + c24.blue + 4 * c24.green;
  248. return (uint16_t) bright >> 3;
  249. }
  250. /* The most simple macro to create a color from R,G and B values
  251. * The order of bit field is different on Big-endian and Little-endian machines*/
  252. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  253. #if LV_COLOR_DEPTH == 1
  254. #define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){(b8 >> 7 | g8 >> 7 | r8 >> 7)})
  255. #elif LV_COLOR_DEPTH == 8
  256. #define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){{b8 >> 6, g8 >> 5, r8 >> 5}})
  257. #elif LV_COLOR_DEPTH == 16
  258. #define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){{b8 >> 3, g8 >> 2, r8 >> 3}})
  259. #elif LV_COLOR_DEPTH == 24
  260. #define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){{b8, g8, r8, 0xff}}) /*Fix 0xff alpha*/
  261. #endif
  262. #else
  263. #if LV_COLOR_DEPTH == 1
  264. #define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){(r8 >> 7 | g8 >> 7 | b8 >> 7)})
  265. #elif LV_COLOR_DEPTH == 8
  266. #define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){{r8 >> 6, g8 >> 5, b8 >> 5}})
  267. #elif LV_COLOR_DEPTH == 16
  268. #define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){{r8 >> 3, g8 >> 2, b8 >> 3}})
  269. #elif LV_COLOR_DEPTH == 24
  270. #define LV_COLOR_MAKE(r8, g8, b8) ((lv_color_t){{0xff, r8, g8, b8}}) /*Fix 0xff alpha*/
  271. #endif
  272. #endif
  273. #define LV_COLOR_HEX(c) LV_COLOR_MAKE(((uint32_t)((uint32_t)c >> 16) & 0xFF), \
  274. ((uint32_t)((uint32_t)c >> 8) & 0xFF), \
  275. ((uint32_t) c & 0xFF))
  276. /*Usage LV_COLOR_HEX3(0x16C) which means LV_COLOR_HEX(0x1166CC)*/
  277. #define LV_COLOR_HEX3(c) LV_COLOR_MAKE((((c >> 4) & 0xF0) | ((c >> 8) & 0xF)), \
  278. ((uint32_t)(c & 0xF0) | ((c & 0xF0) >> 4)), \
  279. ((uint32_t)(c & 0xF) | ((c & 0xF) << 4)))
  280. /**
  281. * Convert a HSV color to RGB
  282. * @param h hue [0..359]
  283. * @param s saturation [0..100]
  284. * @param v value [0..100]
  285. * @return the given RGB color in RGB (with LV_COLOR_DEPTH depth)
  286. */
  287. lv_color_t lv_color_hsv_to_rgb(uint16_t h, uint8_t s, uint8_t v);
  288. /**
  289. * Convert an RGB color to HSV
  290. * @param r red
  291. * @param g green
  292. * @param b blue
  293. * @return the given RGB color n HSV
  294. */
  295. lv_color_hsv_t lv_color_rgb_to_hsv(uint8_t r, uint8_t g, uint8_t b);
  296. /**********************
  297. * MACROS
  298. **********************/
  299. #ifdef __cplusplus
  300. } /* extern "C" */
  301. #endif
  302. #endif /*USE_COLOR*/