summaryrefslogtreecommitdiff
path: root/components/gui/include/rtgui/color.h
blob: c9b82824251f9a1b91a18845f96bf410d62c545e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
 * File      : color.h
 * This file is part of RT-Thread GUI Engine
 * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-10-16     Bernard      first version
 * 2012-01-24     onelife      add mono color support
 */
#ifndef __RTGUI_COLOR_H__
#define __RTGUI_COLOR_H__

#include <rtgui/rtgui.h>

#ifdef __cplusplus
extern "C" {
#endif

/*
 * The color used in the GUI:
 *
 *         bit        bit
 * RGB565  15 R,G,B   0
 * BGR565  15 B,G,R   0
 * RGB888  23 R,G,B   0
 * ARGB888 31 A,R,G,B 0
 * RGBA888 31 R,G,B,A 0
 * ABGR888 31 A,B,G,R 0
 *
 * The rtgui_color is defined as ARGB888.
 *        bit31 A,R,G,B bit0
 */
#define RTGUI_ARGB(a, r, g, b)  \
        ((rtgui_color_t)(((rt_uint8_t)(b)|\
        (((unsigned long)(rt_uint8_t)(g))<<8))|\
        (((unsigned long)(rt_uint8_t)(r))<<16)|\
        (((unsigned long)(rt_uint8_t)(a))<<24)))
#define RTGUI_RGB(r, g, b)  RTGUI_ARGB(255, (r), (g), (b))

#define RTGUI_RGB_B(c)  ((c) & 0xff)
#define RTGUI_RGB_G(c)  (((c) >> 8)  & 0xff)
#define RTGUI_RGB_R(c)  (((c) >> 16) & 0xff)
#define RTGUI_RGB_A(c)  (((c) >> 24) & 0xff)

extern const rtgui_color_t default_foreground;
extern const rtgui_color_t default_background;

/* it's better use these color definitions */
#define RED                RTGUI_RGB(0xff, 0x00, 0x00)
#define GREEN              RTGUI_RGB(0x00, 0xff, 0x00)
#define BLUE               RTGUI_RGB(0x00, 0x00, 0xff)
#define BLACK              RTGUI_RGB(0x00, 0x00, 0x00)
#define WHITE              RTGUI_RGB(0xff, 0xff, 0xff)
#define HIGH_LIGHT         RTGUI_RGB(0xfc, 0xfc, 0xfc)
#define DARK_GREY          RTGUI_RGB(0x7f, 0x7f, 0x7f)
#define LIGHT_GREY         RTGUI_RGB(0xc0, 0xc0, 0xc0)

#ifdef  TRANSPARENT
#undef  TRANSPARENT
#endif
#define TRANSPARENT        RTGUI_ARGB(0, 0, 0, 0)

extern const rtgui_color_t red;
extern const rtgui_color_t green;
extern const rtgui_color_t blue;
extern const rtgui_color_t black;
extern const rtgui_color_t white;
extern const rtgui_color_t high_light;
extern const rtgui_color_t dark_grey;
extern const rtgui_color_t light_grey;

/*
 * RTGUI default color format: ARGB
 * AAAA AAAA RRRR RRRR GGGG GGGG BBBB BBBB
 * 31                                    0
 */

/* convert rtgui color to mono */
rt_inline rt_uint8_t rtgui_color_to_mono(rtgui_color_t c)
{
    rt_uint8_t pixel;

    pixel = (RTGUI_RGB_R(c) | RTGUI_RGB_G(c) | RTGUI_RGB_B(c)) ? 0x01 : 0x00;
    return pixel;
}

rt_inline rtgui_color_t rtgui_color_from_mono(rt_uint8_t pixel)
{
    rtgui_color_t color;

    if (pixel)
    {
        color = white;
    }
    else
    {
        color = black;
    }
    return color;
}

/* convert rtgui color to RRRRRGGGGGGBBBBB */
rt_inline rt_uint16_t rtgui_color_to_565(rtgui_color_t c)
{
    rt_uint16_t pixel;

    pixel = (rt_uint16_t)(((RTGUI_RGB_R(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_B(c) >> 3));

    return pixel;
}

rt_inline rtgui_color_t rtgui_color_from_565(rt_uint16_t pixel)
{
    rt_uint16_t r, g, b;
    rtgui_color_t color;

    r = (pixel >> 11) & 0x1f;
    g = (pixel >> 5)  & 0x3f;
    b = pixel & 0x1f;

    color = b * 255 / 31 + ((g * 255 / 63) << 8) + ((r * 255 / 31) << 16);

    return color;
}

/* convert rtgui color to BBBBBGGGGGGRRRRR */
rt_inline rt_uint16_t rtgui_color_to_565p(rtgui_color_t c)
{
    rt_uint16_t pixel;

    pixel = (rt_uint16_t)(((RTGUI_RGB_B(c) >> 3) << 11) | ((RTGUI_RGB_G(c) >> 2) << 5) | (RTGUI_RGB_R(c) >> 3));
    return pixel;
}

rt_inline rtgui_color_t rtgui_color_from_565p(rt_uint16_t pixel)
{
    rt_uint8_t r, g, b;
    rtgui_color_t color;

    r = pixel & 0x1f;
    g = (pixel >> 5) & 0x3f;
    b = (pixel >> 11) & 0x1f;

    color = b * 255 / 31 + ((g * 255 / 63) << 8) + ((r * 255 / 31) << 16);

    return color;
}

/* convert rtgui color to RGB */
rt_inline rt_uint32_t rtgui_color_to_888(rtgui_color_t c)
{
    rt_uint32_t pixel;

    pixel = RTGUI_RGB_R(c) << 16 | RTGUI_RGB_G(c) << 8 | RTGUI_RGB_B(c);
    return pixel;
}

rt_inline rtgui_color_t rtgui_color_from_888(rt_uint32_t pixel)
{
    rtgui_color_t color;

    color = RTGUI_RGB(((pixel >> 16) & 0xff), ((pixel >> 8) & 0xff), pixel & 0xff);

    return color;
}

/* get the bits of specified pixle format */
rt_uint8_t rtgui_color_get_bits(rt_uint8_t pixel_format) RTGUI_PURE;
/* get the bytes of specified pixle format */
rt_uint8_t rtgui_color_get_bpp(rt_uint8_t pixel_format) RTGUI_PURE;

#ifdef __cplusplus
}
#endif

#endif