summaryrefslogtreecommitdiff
path: root/src/lib/efl/interfaces/efl_gfx_color.c
diff options
context:
space:
mode:
authorAmitesh Singh <amitesh.sh@samsung.com>2017-12-21 14:48:41 +0900
committerAmitesh Singh <amitesh.sh@samsung.com>2017-12-22 13:29:20 +0900
commite2cb16c78b6dff0ce7d06a24447a8340aff8b1fa (patch)
tree4b8ebeb5e7b0bede38c0afec34a6691374268590 /src/lib/efl/interfaces/efl_gfx_color.c
parent9db0ec554bf3f92c8e0c8e1733455955e2351e51 (diff)
Efl.Gfx.Color: add color_code{}
color_code allows user to pass/get the color hex string (e.g. efl_gfx_color_code_set(o, "#FFAA22CC")) Also make this interface as mixin class.
Diffstat (limited to 'src/lib/efl/interfaces/efl_gfx_color.c')
-rw-r--r--src/lib/efl/interfaces/efl_gfx_color.c120
1 files changed, 120 insertions, 0 deletions
diff --git a/src/lib/efl/interfaces/efl_gfx_color.c b/src/lib/efl/interfaces/efl_gfx_color.c
new file mode 100644
index 0000000..1e65f62
--- /dev/null
+++ b/src/lib/efl/interfaces/efl_gfx_color.c
@@ -0,0 +1,120 @@
1#include "config.h"
2#include "Efl.h"
3
4static int
5_format_clean_param(Eina_Tmpstr *s)
6{
7 Eina_Tmpstr *ss;
8 char *ds;
9 int len = 0;
10
11 ds = (char*) s;
12 for (ss = s; *ss; ss++, ds++, len++)
13 {
14 if ((*ss == '\\') && *(ss + 1)) ss++;
15 if (ds != ss) *ds = *ss;
16 }
17 *ds = 0;
18
19 return len;
20}
21
22static int
23_hex_string_get(char ch, Eina_Bool *ok)
24{
25 if ((ch >= '0') && (ch <= '9')) return (ch - '0');
26 else if ((ch >= 'A') && (ch <= 'F')) return (ch - 'A' + 10);
27 else if ((ch >= 'a') && (ch <= 'f')) return (ch - 'a' + 10);
28 *ok = EINA_FALSE;
29 return 0;
30}
31
32/**
33 * @internal
34 * Parses a string of one of the formas:
35 * 1. "#RRGGBB"
36 * 2. "#RRGGBBAA"
37 * 3. "#RGB"
38 * 4. "#RGBA"
39 * To the rgba values.
40 *
41 * @param[in] str The string to parse - NOT NULL.
42 * @param[out] r The Red value - NOT NULL.
43 * @param[out] g The Green value - NOT NULL.
44 * @param[out] b The Blue value - NOT NULL.
45 * @param[out] a The Alpha value - NOT NULL.
46 */
47static Eina_Bool
48_format_color_parse(const char *str, int slen,
49 unsigned char *r, unsigned char *g,
50 unsigned char *b, unsigned char *a)
51{
52 Eina_Bool v = EINA_TRUE;
53
54 *r = *g = *b = *a = 0;
55
56 if (slen == 7) /* #RRGGBB */
57 {
58 *r = (_hex_string_get(str[1], &v) << 4) | (_hex_string_get(str[2], &v));
59 *g = (_hex_string_get(str[3], &v) << 4) | (_hex_string_get(str[4], &v));
60 *b = (_hex_string_get(str[5], &v) << 4) | (_hex_string_get(str[6], &v));
61 *a = 0xff;
62 }
63 else if (slen == 9) /* #RRGGBBAA */
64 {
65 *r = (_hex_string_get(str[1], &v) << 4) | (_hex_string_get(str[2], &v));
66 *g = (_hex_string_get(str[3], &v) << 4) | (_hex_string_get(str[4], &v));
67 *b = (_hex_string_get(str[5], &v) << 4) | (_hex_string_get(str[6], &v));
68 *a = (_hex_string_get(str[7], &v) << 4) | (_hex_string_get(str[8], &v));
69 }
70 else if (slen == 4) /* #RGB */
71 {
72 *r = _hex_string_get(str[1], &v);
73 *r = (*r << 4) | *r;
74 *g = _hex_string_get(str[2], &v);
75 *g = (*g << 4) | *g;
76 *b = _hex_string_get(str[3], &v);
77 *b = (*b << 4) | *b;
78 *a = 0xff;
79 }
80 else if (slen == 5) /* #RGBA */
81 {
82 *r = _hex_string_get(str[1], &v);
83 *r = (*r << 4) | *r;
84 *g = _hex_string_get(str[2], &v);
85 *g = (*g << 4) | *g;
86 *b = _hex_string_get(str[3], &v);
87 *b = (*b << 4) | *b;
88 *a = _hex_string_get(str[4], &v);
89 *a = (*a << 4) | *a;
90 }
91 else v = EINA_FALSE;
92
93 *r = (*r * *a) / 255;
94 *g = (*g * *a) / 255;
95 *b = (*b * *a) / 255;
96 return v;
97}
98
99EOLIAN static void
100_efl_gfx_color_color_code_set(Eo *obj, void *_pd EINA_UNUSED, const char *colorcode)
101{
102 int len;
103 unsigned char r, g, b, a;
104
105 len = _format_clean_param(colorcode);
106
107 _format_color_parse(colorcode, len, &r, &g, &b, &a);
108 efl_gfx_color_set(obj, r, g, b, a);
109}
110
111EOLIAN static const char *
112_efl_gfx_color_color_code_get(Eo *obj, void *_pd EINA_UNUSED)
113{
114 int r, g, b, a;
115
116 efl_gfx_color_get(obj, &r, &g, &b, &a);
117 return eina_slstr_printf("#%02X%02X%02X%02X", r, g, b, a);
118}
119
120#include "interfaces/efl_gfx_color.eo.c" \ No newline at end of file