1// This file is part of SmallBASIC
2//
3// Copyright(C) 2001-2021 Chris Warren-Smith.
4//
5// This program is distributed under the terms of the GPL v2.0 or later
6// Download the GNU Public License (GPL) from www.gnu.org
7//
8
9#ifndef UI_RGB
10#define UI_RGB
11
12typedef uint32_t pixel_t;
13
14#if defined(_SDL)
15#define PIXELFORMAT SDL_PIXELFORMAT_RGB888
16#elif defined(_ANDROID)
17#define PIXELFORMAT AHARDWAREBUFFER_FORMAT_R8G8B8X8_UNORM
18#endif
19
20inline void v_get_argb(int64_t c, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
21 if (c == 0) {
22 a = 0;
23 r = 0;
24 g = 0;
25 b = 0;
26 } else if (c < 0) {
27 // from RGB
28 a = 255;
29 r = (-c & 0xff0000) >> 16;
30 g = (-c & 0xff00) >> 8;
31 b = (-c & 0xff);
32 } else {
33 a = (c & 0xff000000) >> 24;
34 r = (c & 0xff0000) >> 16;
35 g = (c & 0xff00) >> 8;
36 b = (c & 0xff);
37 }
38}
39
40#define v_get_argb_px(a, r, g, b) (a << 24 | (r << 16) | (g << 8) | (b))
41
42#if defined(_SDL)
43// SDL_PACKEDORDER_XRGB
44// A = byte 3
45// R = byte 2
46// G = byte 1
47// B = byte 0
48
49#define GET_RGB_PX(r, g, b) ((0xff000000) | (r << 16) | (g << 8) | (b))
50
51// same as internal format
52#define GET_FROM_RGB888(c) (c)
53
54inline void GET_RGB(pixel_t c, uint8_t &r, uint8_t &g, uint8_t &b) {
55 r = (c & 0xff0000) >> 16;
56 g = (c & 0xff00) >> 8;
57 b = (c & 0xff);
58}
59
60inline void GET_IMAGE_ARGB(const uint8_t *image, unsigned offs, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
61 a = image[offs + 3];
62 r = image[offs + 2];
63 g = image[offs + 1];
64 b = image[offs + 0];
65}
66
67inline void SET_IMAGE_ARGB(uint8_t *image, unsigned offs, uint8_t a, uint8_t r, uint8_t g, uint8_t b) {
68 image[offs + 3] = a;
69 image[offs + 2] = r;
70 image[offs + 1] = g;
71 image[offs + 0] = b;
72}
73
74#else
75
76#define GET_RGB_PX(r, g, b) ((0xff000000) | (b << 16) | (g << 8) | (r))
77
78inline pixel_t GET_FROM_RGB888(unsigned c) {
79 uint8_t r = (c & 0xff0000) >> 16;
80 uint8_t g = (c & 0xff00) >> 8;
81 uint8_t b = (c & 0xff);
82 return ((0xff000000) | (b << 16) | (g << 8) | (r));
83}
84
85inline void GET_RGB(pixel_t c, uint8_t &r, uint8_t &g, uint8_t &b) {
86 b = (c & 0xff0000) >> 16;
87 g = (c & 0xff00) >> 8;
88 r = (c & 0xff);
89}
90
91inline void GET_IMAGE_ARGB(const uint8_t *image, unsigned offs, uint8_t &a, uint8_t &r, uint8_t &g, uint8_t &b) {
92 a = image[offs + 3];
93 b = image[offs + 2];
94 g = image[offs + 1];
95 r = image[offs + 0];
96}
97
98inline void SET_IMAGE_ARGB(uint8_t *image, unsigned offs, uint8_t a, uint8_t r, uint8_t g, uint8_t b) {
99 image[offs + 3] = a;
100 image[offs + 2] = b;
101 image[offs + 1] = g;
102 image[offs + 0] = r;
103}
104
105#endif
106
107#endif
108