Skip to content

Commit

Permalink
Support alpha channel for colours
Browse files Browse the repository at this point in the history
Closes #39
  • Loading branch information
baylej committed Apr 27, 2020
1 parent d5eb225 commit 35aaa76
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
10 changes: 5 additions & 5 deletions src/tmx.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
TMX.H - TMX C LOADER
Copyright (c) 2013-2018, Bayle Jonathan <baylej@github>
Copyright (c) 2013-2020, Bayle Jonathan <baylej@github>
Data Structures storing the map, and function prototypes
Expand Down Expand Up @@ -83,7 +83,7 @@ typedef union {
int integer, boolean; /* type = int or bool */
float decimal; /* type = float */
char *string, *file; /* default and type = string or file */
unsigned int color; /* type = color */
uint32_t color; /* type = color, bytes : ARGB */
} tmx_property_value;

struct _tmx_prop { /* <properties> and <property> */
Expand Down Expand Up @@ -154,7 +154,7 @@ struct _tmx_shape { /* <polygon> and <polyline> */
struct _tmx_text { /* <text> */
char *fontfamily;
int pixelsize;
unsigned int color;
uint32_t color; /* bytes : ARGB */

int wrap; /* 0 == false */
int bold;
Expand Down Expand Up @@ -192,7 +192,7 @@ struct _tmx_obj { /* <object> */
};

struct _tmx_objgr { /* <objectgroup> */
unsigned int color; /* bytes : RGB */
uint32_t color; /* bytes : ARGB */
enum tmx_objgr_draworder draworder;
tmx_object *head;
};
Expand Down Expand Up @@ -232,7 +232,7 @@ struct _tmx_map { /* <map> (Head of the data structure) */
enum tmx_stagger_axis stagger_axis;
int hexsidelength;

unsigned int backgroundcolor; /* bytes : RGB */
uint32_t backgroundcolor; /* bytes : ARGB */
enum tmx_map_renderorder renderorder;

tmx_properties *properties;
Expand Down
21 changes: 18 additions & 3 deletions src/tmx_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,10 +501,25 @@ int parse_boolean(const char *boolean) {
return 0;
}

/* "#337FA2" -> 0x337FA2 */
int get_color_rgb(const char *c) {
/* HTML col -> (uint32) AARRGGBB
#RGB -> (uint32) FFRRGGBB
#ARGB -> (uint32) AARRGGBB
#RRGGBB -> (uint32) FFRRGGBB
#AARRGGBB -> (uint32) AARRGGBB */
uint32_t get_color_rgb(const char *c) {
unsigned long clen;
uint32_t res;
if (*c == '#') c++;
return (int)strtol(c, NULL, 16);
clen = strlen(c);
res = (uint32_t)strtol(c, NULL, 16);
if (clen < 6) {
res = (res & 0xF000u) << 16 | (res & 0xF000u) << 12 /* AXXX */
| (res & 0x0F00u) << 12 | (res & 0x0F00u) << 8 /* XRXX */
| (res & 0x00F0u) << 8 | (res & 0x00F0u) << 4 /* XXGX */
| (res & 0x000Fu) << 4 | (res & 0x000Fu); /* XXXB */
}
if (clen == 6 || clen == 3) { res |= 0xFF000000u; }
return res;
}

int count_char_occurences(const char *str, char c) {
Expand Down
2 changes: 1 addition & 1 deletion src/tmx_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ enum tmx_horizontal_align parse_horizontal_align(const char *horalign);
enum tmx_vertical_align parse_vertical_align(const char *veralign);
enum tmx_layer_type parse_layer_type(const char *nodename);
int parse_boolean(const char *boolean);
int get_color_rgb(const char *c);
uint32_t get_color_rgb(const char *c);

int count_char_occurences(const char *str, char c);
char* str_trim(char *str);
Expand Down

0 comments on commit 35aaa76

Please sign in to comment.