raster pushed a commit to branch master.

http://git.enlightenment.org/core/efl.git/commit/?id=984a8f7f90e7457a3055101ec236a0f4288e179e

commit 984a8f7f90e7457a3055101ec236a0f4288e179e
Author: Jee-Yong Um <jc9...@samsung.com>
Date:   Mon Mar 28 15:36:41 2016 +0900

    edje_cc: support hexadecimal color code
    
    Summary:
    Support hexadecimal color code in EDC.
    
    Four types of color code are acceptable.
    All values below mean 'Red'. (255 0 0 255)
       color: "#F00";
       color: "#F00F";
       color: "#FF0000";
       color: "#FF0000FF";
    
    Color code tables are usually provided with hexadecimal numbers.
    Supporting hexadecimal color code will allow developers to skip
    manual conversion hex to decimal.
    
    Test Plan: Test case will provided with seperated commit.
    
    Reviewers: cedric, jpeg, raster
    
    Reviewed By: raster
    
    Subscribers: raster
    
    Differential Revision: https://phab.enlightenment.org/D3831
---
 src/bin/edje/edje_cc.h          |   1 +
 src/bin/edje/edje_cc_handlers.c | 191 ++++++++++++++++++++++++++++++++--------
 src/bin/edje/edje_cc_out.c      |  99 +++++++++++++++++++++
 3 files changed, 255 insertions(+), 36 deletions(-)

diff --git a/src/bin/edje/edje_cc.h b/src/bin/edje/edje_cc.h
index 159d7ca..65299b5 100644
--- a/src/bin/edje/edje_cc.h
+++ b/src/bin/edje/edje_cc.h
@@ -253,6 +253,7 @@ void edje_cc_handlers_pop_notify(const char *token);
 int get_param_index(char *str);
 
 void color_tree_root_free(void);
+void convert_color_code(char *str, int *r, int *g, int *b, int *a);
 
 /* global vars */
 extern Eina_List             *ext_dirs;
diff --git a/src/bin/edje/edje_cc_handlers.c b/src/bin/edje/edje_cc_handlers.c
index 26f53ca..382f4bc 100644
--- a/src/bin/edje/edje_cc_handlers.c
+++ b/src/bin/edje/edje_cc_handlers.c
@@ -2803,14 +2803,34 @@ static void
 st_color_class_color(void)
 {
    Edje_Color_Class *cc;
-
-   check_arg_count(4);
+   int nargs = get_arg_count();
 
    cc = eina_list_data_get(eina_list_last(edje_file->color_classes));
-   cc->r = parse_int_range(0, 0, 255);
-   cc->g = parse_int_range(1, 0, 255);
-   cc->b = parse_int_range(2, 0, 255);
-   cc->a = parse_int_range(3, 0, 255);
+
+   if (nargs == 1)
+     {
+        int r, g, b, a;
+        char *str = parse_str(0);
+
+        convert_color_code(str, &r, &g, &b, &a);
+        cc->r = r;
+        cc->g = g;
+        cc->b = b;
+        cc->a = a;
+     }
+   else if (nargs == 4)
+     {
+        cc->r = parse_int_range(0, 0, 255);
+        cc->g = parse_int_range(1, 0, 255);
+        cc->b = parse_int_range(2, 0, 255);
+        cc->a = parse_int_range(3, 0, 255);
+     }
+   else
+     {
+        ERR("%s:%i. color code should be a string or a set of 4 integers.",
+            file_in, line - 1);
+        exit(-1);
+     }
 }
 
 /**
@@ -2827,14 +2847,34 @@ static void
 st_color_class_color2(void)
 {
    Edje_Color_Class *cc;
-
-   check_arg_count(4);
+   int nargs = get_arg_count();
 
    cc = eina_list_data_get(eina_list_last(edje_file->color_classes));
-   cc->r2 = parse_int_range(0, 0, 255);
-   cc->g2 = parse_int_range(1, 0, 255);
-   cc->b2 = parse_int_range(2, 0, 255);
-   cc->a2 = parse_int_range(3, 0, 255);
+
+   if (nargs == 1)
+     {
+        int r, g, b, a;
+        char *str = parse_str(0);
+
+        convert_color_code(str, &r, &g, &b, &a);
+        cc->r2 = r;
+        cc->g2 = g;
+        cc->b2 = b;
+        cc->a2 = a;
+     }
+   else if (nargs == 4)
+     {
+        cc->r2 = parse_int_range(0, 0, 255);
+        cc->g2 = parse_int_range(1, 0, 255);
+        cc->b2 = parse_int_range(2, 0, 255);
+        cc->a2 = parse_int_range(3, 0, 255);
+     }
+   else
+     {
+        ERR("%s:%i. color code should be a string or a set of 4 integers.",
+            file_in, line - 1);
+        exit(-1);
+     }
 }
 
 /**
@@ -2851,14 +2891,34 @@ static void
 st_color_class_color3(void)
 {
    Edje_Color_Class *cc;
-
-   check_arg_count(4);
+   int nargs = get_arg_count();
 
    cc = eina_list_data_get(eina_list_last(edje_file->color_classes));
-   cc->r3 = parse_int_range(0, 0, 255);
-   cc->g3 = parse_int_range(1, 0, 255);
-   cc->b3 = parse_int_range(2, 0, 255);
-   cc->a3 = parse_int_range(3, 0, 255);
+
+   if (nargs == 1)
+     {
+        int r, g, b, a;
+        char *str = parse_str(0);
+
+        convert_color_code(str, &r, &g, &b, &a);
+        cc->r3 = r;
+        cc->g3 = g;
+        cc->b3 = b;
+        cc->a3 = a;
+     }
+   else if (nargs == 4)
+     {
+        cc->r3 = parse_int_range(0, 0, 255);
+        cc->g3 = parse_int_range(1, 0, 255);
+        cc->b3 = parse_int_range(2, 0, 255);
+        cc->a3 = parse_int_range(3, 0, 255);
+     }
+   else
+     {
+        ERR("%s:%i. color code should be a string or a set of 4 integers.",
+            file_in, line - 1);
+        exit(-1);
+     }
 }
 
 /**
@@ -8394,19 +8454,39 @@ 
st_collections_group_parts_part_description_color_class(void)
 static void
 st_collections_group_parts_part_description_color(void)
 {
-   check_arg_count(4);
+   int nargs = get_arg_count();
 
    if (current_part->type == EDJE_PART_TYPE_SPACER)
      {
        ERR("parse error %s:%i. SPACER part can't have a color defined",
-          file_in, line - 1);
+           file_in, line - 1);
        exit(-1);
      }
 
-   current_desc->color.r = parse_int_range(0, 0, 255);
-   current_desc->color.g = parse_int_range(1, 0, 255);
-   current_desc->color.b = parse_int_range(2, 0, 255);
-   current_desc->color.a = parse_int_range(3, 0, 255);
+   if (nargs == 1)
+     {
+        int r, g, b, a;
+        char *str = parse_str(0);
+
+        convert_color_code(str, &r, &g, &b, &a);
+        current_desc->color.r = r;
+        current_desc->color.g = g;
+        current_desc->color.b = b;
+        current_desc->color.a = a;
+     }
+   else if (nargs == 4)
+     {
+        current_desc->color.r = parse_int_range(0, 0, 255);
+        current_desc->color.g = parse_int_range(1, 0, 255);
+        current_desc->color.b = parse_int_range(2, 0, 255);
+        current_desc->color.a = parse_int_range(3, 0, 255);
+     }
+   else
+     {
+        ERR("%s:%i. color code should be a string or a set of 4 integers.",
+            file_in, line - 1);
+        exit(-1);
+     }
 }
 
 /**
@@ -8422,19 +8502,39 @@ st_collections_group_parts_part_description_color(void)
 static void
 st_collections_group_parts_part_description_color2(void)
 {
-   check_arg_count(4);
+   int nargs = get_arg_count();
 
    if (current_part->type == EDJE_PART_TYPE_SPACER)
      {
        ERR("parse error %s:%i. SPACER part can't have a color defined",
-          file_in, line - 1);
+           file_in, line - 1);
        exit(-1);
      }
 
-   current_desc->color2.r = parse_int_range(0, 0, 255);
-   current_desc->color2.g = parse_int_range(1, 0, 255);
-   current_desc->color2.b = parse_int_range(2, 0, 255);
-   current_desc->color2.a = parse_int_range(3, 0, 255);
+   if (nargs == 1)
+     {
+        int r, g, b, a;
+        char *str = parse_str(0);
+
+        convert_color_code(str, &r, &g, &b, &a);
+        current_desc->color2.r = r;
+        current_desc->color2.g = g;
+        current_desc->color2.b = b;
+        current_desc->color2.a = a;
+     }
+   else if (nargs == 4)
+     {
+        current_desc->color2.r = parse_int_range(0, 0, 255);
+        current_desc->color2.g = parse_int_range(1, 0, 255);
+        current_desc->color2.b = parse_int_range(2, 0, 255);
+        current_desc->color2.a = parse_int_range(3, 0, 255);
+     }
+   else
+     {
+        ERR("%s:%i. color code should be a string or a set of 4 integers.",
+            file_in, line - 1);
+        exit(-1);
+     }
 }
 
 /**
@@ -8452,8 +8552,7 @@ st_collections_group_parts_part_description_color3(void)
 {
    Edje_Part_Collection *pc;
    Edje_Part_Description_Text *ed;
-
-   check_arg_count(4);
+   int nargs = get_arg_count();
 
    pc = eina_list_data_get(eina_list_last(edje_collections));
 
@@ -8467,10 +8566,30 @@ st_collections_group_parts_part_description_color3(void)
 
    ed = (Edje_Part_Description_Text*)current_desc;
 
-   ed->text.color3.r = parse_int_range(0, 0, 255);
-   ed->text.color3.g = parse_int_range(1, 0, 255);
-   ed->text.color3.b = parse_int_range(2, 0, 255);
-   ed->text.color3.a = parse_int_range(3, 0, 255);
+   if (nargs == 1)
+     {
+        int r, g, b, a;
+        char *str = parse_str(0);
+
+        convert_color_code(str, &r, &g, &b, &a);
+        ed->text.color3.r = r;
+        ed->text.color3.g = g;
+        ed->text.color3.b = b;
+        ed->text.color3.a = a;
+     }
+   else if (nargs == 4)
+     {
+        ed->text.color3.r = parse_int_range(0, 0, 255);
+        ed->text.color3.g = parse_int_range(1, 0, 255);
+        ed->text.color3.b = parse_int_range(2, 0, 255);
+        ed->text.color3.a = parse_int_range(3, 0, 255);
+     }
+   else
+     {
+        ERR("%s:%i. color code should be a string or a set of 4 integers.",
+            file_in, line - 1);
+        exit(-1);
+     }
 }
 
 /**
diff --git a/src/bin/edje/edje_cc_out.c b/src/bin/edje/edje_cc_out.c
index 3a063ba..ee5fe14 100644
--- a/src/bin/edje/edje_cc_out.c
+++ b/src/bin/edje/edje_cc_out.c
@@ -4262,3 +4262,102 @@ process_color_tree(char *s, const char *f_in, int ln)
    eina_array_clean(array);
    eina_array_free(array);
 }
+
+char
+validate_hex_digit(char c)
+{
+   if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c <= 
'f'))
+    return c;
+
+   ERR("%s:%i. invalid character '%c' is used in color code.",
+       file_in, line - 1, c);
+   exit(-1);
+}
+
+void
+convert_color_code(char *str, int *r, int *g, int *b, int *a)
+{
+   char buf[3];
+   int len;
+
+   len = strlen(str);
+
+   if ((str[0] != '#') || (len != 4 && len != 5 && len != 7 && len != 9))
+     {
+        ERR("%s:%i color code should start with '#' and have 4 or 8 digit hex 
number. (3 or 6 digits are allowed to omit alpha value of 255)",
+            file_in, line - 1);
+        exit(-1);
+     }
+
+   buf[2] = '\0';
+
+   if (r)
+     {
+        if ((len == 4) || (len == 5))
+          {
+             buf[0] = validate_hex_digit(str[1]);
+             buf[1] = validate_hex_digit(str[1]);
+          }
+        else
+          {
+             buf[0] = validate_hex_digit(str[1]);
+             buf[1] = validate_hex_digit(str[2]);
+          }
+
+        *r = (int)strtol(buf, NULL, 16);
+     }
+   if (g)
+     {
+        if ((len == 4) || (len == 5))
+          {
+             buf[0] = validate_hex_digit(str[2]);
+             buf[1] = validate_hex_digit(str[2]);
+          }
+        else
+          {
+             buf[0] = validate_hex_digit(str[3]);
+             buf[1] = validate_hex_digit(str[4]);
+          }
+
+        *g = (int)strtol(buf, NULL, 16);
+     }
+   if (b)
+     {
+        if ((len == 4) || (len == 5))
+          {
+             buf[0] = validate_hex_digit(str[3]);
+             buf[1] = validate_hex_digit(str[3]);
+          }
+        else
+          {
+             buf[0] = validate_hex_digit(str[5]);
+             buf[1] = validate_hex_digit(str[6]);
+          }
+
+        *b = (int)strtol(buf, NULL, 16);
+     }
+   if (a)
+     {
+        if ((len == 5) || (len == 9))
+          {
+             if (len == 5)
+               {
+                  buf[0] = validate_hex_digit(str[4]);
+                  buf[1] = validate_hex_digit(str[4]);
+               }
+             else
+               {
+                  buf[0] = validate_hex_digit(str[7]);
+                  buf[1] = validate_hex_digit(str[8]);
+               }
+
+             *a = (int)strtol(buf, NULL, 16);
+          }
+        else
+          {
+             *a = 255;
+          }
+     }
+
+   free(str);
+}

-- 


Reply via email to