Cross-platform C SDK logo

Cross-platform C SDK

Colors

❮ Back
Next ❯
This page has been automatically translated using the Google Translate API services. We are working on improving texts. Thank you for your understanding and patience.

Functions

color_tcolor_rgb (...)
color_tcolor_rgba (...)
color_tcolor_rgbaf (...)
color_tcolor_hsbf (...)
color_tcolor_red (...)
color_tcolor_green (...)
color_tcolor_blue (...)
color_tcolor_gray (...)
color_tcolor_bgr (...)
color_tcolor_html (...)
color_tcolor_alt (...)
voidcolor_to_hsbf (...)
voidcolor_to_html (...)
voidcolor_get_rgb (...)
voidcolor_get_rgbf (...)
voidcolor_get_rgba (...)
voidcolor_get_rgbaf (...)
uint8_tcolor_get_alpha (...)
color_tcolor_set_alpha (...)
bool_tcolor_dark_mode (void)

Types and Constants

color_tkCOLOR_TRANSPARENT
color_tkCOLOR_DEFAULT
color_tkCOLOR_LABEL
color_tkCOLOR_VIEW
color_tkCOLOR_LINE
color_tkCOLOR_LINK
color_tkCOLOR_TEXT
color_tkCOLOR_SELTEXT
color_tkCOLOR_HOTTEXT
color_tkCOLOR_BGTEXT
color_tkCOLOR_BGSELTEXT
color_tkCOLOR_BGHOTTEXT
color_tkCOLOR_HEADER
color_tkCOLOR_ROW1
color_tkCOLOR_ROW2
color_tkCOLOR_BLACK
color_tkCOLOR_WHITE
color_tkCOLOR_RED
color_tkCOLOR_GREEN
color_tkCOLOR_BLUE
color_tkCOLOR_YELLOW
color_tkCOLOR_CYAN
color_tkCOLOR_MAGENTA

The colors in NAppGUI are encoded using a 32-bit integer with the four RGBA channels in Little-Endian: Red in byte 0, green in 1, blue in 2 and alpha (or transparency) in 3 (Figure 1). In the particular case that byte 3 is equal to 0 (fully transparent), the first three bytes will not contain RGB information, but an index with a special color.

32-bit integer representing an RGBA value.
Figure 1: color_t type structure.

You can set the RGBA values by directly manipulating the bits of a color_t variable or use the function color_rgb. However, you must take into account, when using colors in user interfaces, that RGB values are not usually "portable". Our software must be prepared to run on platforms with different desktop themes. For example in (Listing 1) we have changed the background color of some Label components. The result looks fine with light themes, but with dark themes it is confused with the default text color (Figure 2).

Listing 1: Change background color in Label.
1
2
3
4
5
6
7
8
9
Label *label1 = label_create();
Label *label2 = label_create();
Label *label3 = label_create();
label_text(label1, i_LABEL_01);
label_text(label2, i_LABEL_02);
label_text(label3, i_LABEL_03);
label_bgcolor(label1, color_rgb(192, 255, 255));
label_bgcolor(label2, color_rgb(255, 192, 255));
label_bgcolor(label3, color_rgb(255, 255, 192));
Comparison of setting RGB colors on light and dark desktop themes.
Figure 2: Portability of interfaces with fixed RGB colors.

To mitigate this effect, we can establish two alternative versions of the same color: one for light themes and one for dark themes (Listing 2). The RGB value will be effective at runtime depending on the window style (Figure 3).

Listing 2: Alternative colors for dark themes.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
Label *label1 = label_create();
Label *label2 = label_create();
Label *label3 = label_create();
color_t c1 = color_alt(color_rgb(192, 255, 255), color_rgb(48, 112, 112));
color_t c2 = color_alt(color_rgb(255, 192, 255), color_rgb(128, 48, 112));
color_t c3 = color_alt(color_rgb(255, 255, 192), color_rgb(112, 112, 48));
label_text(label1, i_LABEL_01);
label_text(label2, i_LABEL_02);
label_text(label3, i_LABEL_03);
label_bgcolor(label1, c1);
label_bgcolor(label2, c2);
label_bgcolor(label3, c3);
Interface window on a dark desktop theme.
Figure 3: Alternative color adaptation for dark window environments.

NAppGUI also provides access to a series of system default colors. For example, kCOLOR_LABEL indicates the color of text labels based on the active theme. The use of these colors will be very useful when creating custom controls that integrate perfectly into the aesthetics of the system without worrying about portability issues. In Color table you have an example application that shows these colors depending on the platform where the program is running (Figure 4) (Figure 5).

Screenshot of application that displays system colors on a light desktop theme.
Figure 4: Color View in light themes.
Screenshot of application that displays system colors on a dark desktop theme.
Figure 5: Color View in dark themes.

kCOLOR_TRANSPARENT

const color_t kCOLOR_TRANSPARENT;

Totally transparent color, absence of color or null color.


kCOLOR_DEFAULT

const color_t kCOLOR_DEFAULT;

Default color.


kCOLOR_LABEL

const color_t kCOLOR_LABEL;

Color of text Label.


kCOLOR_VIEW

const color_t kCOLOR_VIEW;

Background color in controls View.


kCOLOR_LINE

const color_t kCOLOR_LINE;

Color of lines in tables or window separator elements.


kCOLOR_LINK

const color_t kCOLOR_LINK;

Text color in hyperlinks.


kCOLOR_TEXT

const color_t kCOLOR_TEXT;

Text color in tables or controls.


kCOLOR_SELTEXT

const color_t kCOLOR_SELTEXT;

Selected text color in tables or controls.


kCOLOR_HOTTEXT

const color_t kCOLOR_HOTTEXT;

Color of highlighted text in tables or controls.


kCOLOR_BGTEXT

const color_t kCOLOR_BGTEXT;

Text background color in tables or controls.


kCOLOR_BGSELTEXT

const color_t kCOLOR_BGSELTEXT;

Text background color selected in tables or controls.


kCOLOR_BGHOTTEXT

const color_t kCOLOR_BGHOTTEXT;

Background color of highlighted text in tables or controls.


kCOLOR_HEADER

const color_t kCOLOR_HEADER;

Text color in table headers.


kCOLOR_ROW1

const color_t kCOLOR_ROW1;

Background color of odd rows in tables or similar components.


kCOLOR_ROW2

const color_t kCOLOR_ROW2;

Background color of even rows in tables or similar components.


kCOLOR_BLACK

const color_t kCOLOR_BLACK;

BLACK color rgb(0,0,0).


kCOLOR_WHITE

const color_t kCOLOR_WHITE;

WHITE color rgb(255,255,255).


kCOLOR_RED

const color_t kCOLOR_RED;

RED color rgb(255,0,0).


kCOLOR_GREEN

const color_t kCOLOR_GREEN;

GREEN color rgb(0,255,0).


kCOLOR_BLUE

const color_t kCOLOR_BLUE;

BLUE color rgb(0,0,255).


kCOLOR_YELLOW

const color_t kCOLOR_YELLOW;

YELLOW color rgb(255,255,0).


kCOLOR_CYAN

const color_t kCOLOR_CYAN;

CYAN color rgb(0,255,255).


kCOLOR_MAGENTA

const color_t kCOLOR_MAGENTA;

MAGENTA color rgb(255,0,255).


color_rgb ()

Create a color from the channels R (red), G (green) y B (blue).

color_t
color_rgb(const uint8_t r,
          const uint8_t g,
          const uint8_t b);
r

Red channel.

g

Green channel.

b

Blue channel.

Return

Color.

Remarks

The alpha channel is set to 255 (totally opaque).


color_rgba ()

Create a color from the channels R (red), G (green), B (blue) and A (alpha).

color_t
color_rgba(const uint8_t r,
           const uint8_t g,
           const uint8_t b,
           const uint8_t a);
r

Red channel.

g

Green channel.

b

Blue channel.

a

Alpha channel (transparency).

Return

Color.

Remarks

a=0 not supported. Use kCOLOR_TRANSPARENT in those cases.


color_rgbaf ()

Create a color from the normalized RGBA channels from 0 to 1.

color_t
color_rgbaf(const real32_t r,
            const real32_t g,
            const real32_t b,
            const real32_t a);
r

Red channel.

g

Green channel.

b

Blue channel.

a

Alpha channel (transparency).

Return

Color.

Remarks

a=0 not supported. Use kCOLOR_TRANSPARENT in those cases.


color_hsbf ()

Creates a color (rgb) from its components Hue-Saturation-Brightness.

color_t
color_hsbf(const real32_t hue,
           const real32_t sat,
           const real32_t bright);
hue

Hue component.

sat

Saturation component.

bright

Brightness component.

Return

Color.


color_red ()

Create an RGB color using only the red channel.

color_t
color_red(const uint8_t r);
r

Red Channel.

Return

Color.

Remarks

Equivalent to color_rgb(r, 0, 0).


color_green ()

Create an RGB color using only the green channel.

color_t
color_green(const uint8_t g);
g

Green channel.

Return

Color.

Remarks

Equivalent to color_rgb(0, g, 0).


color_blue ()

Create an RGB color using only the blue channel.

color_t
color_blue(const uint8_t b);
b

Blue channel.

Return

Color.

Remarks

Equivalent to color_rgb(0, 0, b).


color_gray ()

Creates a gray RGB color from intensity value.

color_t
color_gray(const uint8_t l);
l

Intensity (luminance).

Return

Color.

Remarks

Equivalent to color_rgb(l, l, l).


color_bgr ()

Create a color from a 32-bit BGR value. Byte 0 corresponds to channel B , 1 to G and 2 to R. The highest order byte is ignored (set to 255).

color_t
color_bgr(const uint32_t bgr);
bgr

The bgr 32bits value.

Return

Color.

Remarks

This byte order is typical in Web colors.


color_html ()

Create a color from a string in HTML or CSS format.

color_t
color_html(const char_t *html);
1
2
color_t c1 = color_html("#FF0000"); // Red
color_t c2 = color_html("#000080"); // Navy
html

The text string with the HTML color.

Return

The color transformed to RGB.


color_alt ()

Create a color with two alternative versions.

color_t
color_alt(const color_t light_color,
          const color_t dark_color);
light_color

Color for LIGHT desktop themes.

dark_color

Color for DARK desktop themes.

Return

Color.

Remarks

The system will set the final color based on the "clarity" of the window manager colors (Light/Dark). NO nested alternative colors ARE ALLOWED. The values light and dark must be a RGB or system colors.


color_to_hsbf ()

Convert a color (rgb) to HSB space (hue, saturation, brightness).

void
color_to_hsbf(const color_t color,
              real32_t *hue,
              real32_t *sat,
              real32_t *sat);
color

Color.

hue

Hue component.

sat

Saturation component.

sat

Brightness component.


color_to_html ()

Convert a color to the HTML or CSS format (#RRGGBB).

void
color_to_html(const color_t color,
              char_t *html,
              const uint32_t size);
color

The color to convert.

html

Buffer where to write the result.

size

Result buffer size.


color_get_rgb ()

Returns RGB color values.

void
color_get_rgb(const color_t color,
              uint8_t *r,
              uint8_t *g,
              uint8_t *b);
color

Color.

r

Red channel.

g

Green channel.

b

Blue channel.

Remarks

In system or indexed colors, it makes effective the RGB value.


color_get_rgbf ()

Returns RGB color values, normalized from 0 to 1.

void
color_get_rgbf(const color_t color,
               real32_t *r,
               real32_t *g,
               real32_t *b);
color

Color.

r

Red channel.

g

Green channel.

b

Blue channel.

Remarks

In system or indexed colors, it makes effective the RGB value.


color_get_rgba ()

Returns the RGBA values of the color.

void
color_get_rgba(const color_t color,
               uint8_t *r,
               uint8_t *g,
               uint8_t *b,
               uint8_t *a);
color

Color.

r

Red channel.

g

Green channel.

b

Blue channel.

a

Alpha channel (transparency).

Remarks

In system or indexed colors, it makes effective the RGBA value.


color_get_rgbaf ()

Returns the RGBA values of the color, normalized from 0 to 1.

void
color_get_rgbaf(const color_t color,
                real32_t *r,
                real32_t *g,
                real32_t *b,
                real32_t *a);
color

Color.

r

Red channel.

g

Green channel.

b

Blue channel.

a

Alpha channel (transparency).

Remarks

In system or indexed colors, it makes effective the RGBA value.


color_get_alpha ()

Get the alpha (transparency) color component.

uint8_t
color_get_alpha(const color_t color);
color

Color.

Return

The alpha component. If it is equal 0 it means that the color is indexed (does not contain RGB values).


color_set_alpha ()

Changes the alpha (transparency) value of a color.

color_t
color_set_alpha(const color_t color,
                const uint8_t alpha);
color

Color.

alpha

Alpha component.

Return

The new color, with the altered alpha component.


color_dark_mode ()

Determine if the window environment has a light or dark theme.

bool_t
color_dark_mode(void);

Return

TRUE for Dark mode, FALSE for light mode.

Remarks

It is calculated based on the windows background color.

❮ Back
Next ❯