summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorali <ali198724@gmail.com>2019-08-01 23:00:07 +0000
committerCedric BAIL <cedric.bail@free.fr>2019-08-19 11:33:23 -0700
commita821eb456a8c89943f306286669093e2a43aa1f1 (patch)
treea01947b8912495ca731861833ed6e270acedd487
parent1d76452aa964c93cd7b8ea343f918866bf9cacb8 (diff)
evas_common: parse color in rgb()/rgba() format
evas_common_format_color_parse: support rgb()/rgba() format efl user can now specify colors in text formats and styles as rgb(0-255,0-255,0-255) & rgba(0-255,0-255,0-255,0-255) format. This is related to task: T8068 Reviewed-by: Cedric BAIL <cedric.bail@free.fr> Differential Revision: https://phab.enlightenment.org/D9309
-rw-r--r--src/bin/elementary/test_label.c2
-rw-r--r--src/lib/evas/canvas/evas_object_textblock.c20
-rw-r--r--src/lib/evas/common/evas_text_utils.c98
3 files changed, 104 insertions, 16 deletions
diff --git a/src/bin/elementary/test_label.c b/src/bin/elementary/test_label.c
index c00ae7986e..233ce015b4 100644
--- a/src/bin/elementary/test_label.c
+++ b/src/bin/elementary/test_label.c
@@ -393,6 +393,8 @@ test_label_colors(void *data EINA_UNUSED, Evas_Object *obj EINA_UNUSED, void *ev
393 elm_object_text_set(lb, 393 elm_object_text_set(lb,
394 "<color=red>this is red color line(color = red)<color><br>" 394 "<color=red>this is red color line(color = red)<color><br>"
395 "<color=#0000FF>this is blue color line (color = #0000FF)<color><br>" 395 "<color=#0000FF>this is blue color line (color = #0000FF)<color><br>"
396 "<color=rgb(0,255,0)>this is green color line (color = rgb(0,255,0))<color><br>"
397 "<color=rgba(0,255,0,255)>this is blue color line (color = rgba(0,255,0,255))<color><br>"
396 ); 398 );
397 evas_object_size_hint_weight_set(lb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND); 399 evas_object_size_hint_weight_set(lb, EVAS_HINT_EXPAND, EVAS_HINT_EXPAND);
398 elm_win_resize_object_add(win, lb); 400 elm_win_resize_object_add(win, lb);
diff --git a/src/lib/evas/canvas/evas_object_textblock.c b/src/lib/evas/canvas/evas_object_textblock.c
index 0ffcaf72e9..6299e3f195 100644
--- a/src/lib/evas/canvas/evas_object_textblock.c
+++ b/src/lib/evas/canvas/evas_object_textblock.c
@@ -1953,6 +1953,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
1953 * @li "#RRGGBBAA" 1953 * @li "#RRGGBBAA"
1954 * @li "#RGB" 1954 * @li "#RGB"
1955 * @li "#RGBA" 1955 * @li "#RGBA"
1956 * @li "rgb(r,g,b)"
1957 * @li "rgba(r,g,b,a)"
1956 * @li "color_name" like "red" 1958 * @li "color_name" like "red"
1957 * @code 1959 * @code
1958 * color=<color> 1960 * color=<color>
@@ -1972,6 +1974,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
1972 * @li "#RRGGBBAA" 1974 * @li "#RRGGBBAA"
1973 * @li "#RGB" 1975 * @li "#RGB"
1974 * @li "#RGBA" 1976 * @li "#RGBA"
1977 * @li "rgb(r,g,b)"
1978 * @li "rgba(r,g,b,a)"
1975 * @li "color_name" like "red" 1979 * @li "color_name" like "red"
1976 * @code 1980 * @code
1977 * underline_color=<color> 1981 * underline_color=<color>
@@ -1992,6 +1996,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
1992 * @li "#RRGGBBAA" 1996 * @li "#RRGGBBAA"
1993 * @li "#RGB" 1997 * @li "#RGB"
1994 * @li "#RGBA" 1998 * @li "#RGBA"
1999 * @li "rgb(r,g,b)"
2000 * @li "rgba(r,g,b,a)"
1995 * @li "color_name" like "red" 2001 * @li "color_name" like "red"
1996 * @code 2002 * @code
1997 * underline2_color=<color> 2003 * underline2_color=<color>
@@ -2011,6 +2017,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
2011 * @li "#RRGGBBAA" 2017 * @li "#RRGGBBAA"
2012 * @li "#RGB" 2018 * @li "#RGB"
2013 * @li "#RGBA" 2019 * @li "#RGBA"
2020 * @li "rgb(r,g,b)"
2021 * @li "rgba(r,g,b,a)"
2014 * @li "color_name" like "red" 2022 * @li "color_name" like "red"
2015 * @code 2023 * @code
2016 * underline_dash_color=<color> 2024 * underline_dash_color=<color>
@@ -2031,6 +2039,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
2031 * @li "#RRGGBBAA" 2039 * @li "#RRGGBBAA"
2032 * @li "#RGB" 2040 * @li "#RGB"
2033 * @li "#RGBA" 2041 * @li "#RGBA"
2042 * @li "rgb(r,g,b)"
2043 * @li "rgba(r,g,b,a)"
2034 * @li "color_name" like "red" 2044 * @li "color_name" like "red"
2035 * @code 2045 * @code
2036 * outline_color=<color> 2046 * outline_color=<color>
@@ -2051,6 +2061,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
2051 * @li "#RRGGBBAA" 2061 * @li "#RRGGBBAA"
2052 * @li "#RGB" 2062 * @li "#RGB"
2053 * @li "#RGBA" 2063 * @li "#RGBA"
2064 * @li "rgb(r,g,b)"
2065 * @li "rgba(r,g,b,a)"
2054 * @li "color_name" like "red" 2066 * @li "color_name" like "red"
2055 * @code 2067 * @code
2056 * shadow_color=<color> 2068 * shadow_color=<color>
@@ -2071,6 +2083,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
2071 * @li "#RRGGBBAA" 2083 * @li "#RRGGBBAA"
2072 * @li "#RGB" 2084 * @li "#RGB"
2073 * @li "#RGBA" 2085 * @li "#RGBA"
2086 * @li "rgb(r,g,b)"
2087 * @li "rgba(r,g,b,a)"
2074 * @li "color_name" like "red" 2088 * @li "color_name" like "red"
2075 * @code 2089 * @code
2076 * glow_color=<color> 2090 * glow_color=<color>
@@ -2091,6 +2105,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
2091 * @li "#RRGGBBAA" 2105 * @li "#RRGGBBAA"
2092 * @li "#RGB" 2106 * @li "#RGB"
2093 * @li "#RGBA" 2107 * @li "#RGBA"
2108 * @li "rgb(r,g,b)"
2109 * @li "rgba(r,g,b,a)"
2094 * @li "color_name" like "red" 2110 * @li "color_name" like "red"
2095 * @code 2111 * @code
2096 * glow2_color=<color> 2112 * glow2_color=<color>
@@ -2111,6 +2127,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
2111 * @li "#RRGGBBAA" 2127 * @li "#RRGGBBAA"
2112 * @li "#RGB" 2128 * @li "#RGB"
2113 * @li "#RGBA" 2129 * @li "#RGBA"
2130 * @li "rgb(r,g,b)"
2131 * @li "rgba(r,g,b,a)"
2114 * @li "color_name" like "red" 2132 * @li "color_name" like "red"
2115 * @code 2133 * @code
2116 * backing_color=<color> 2134 * backing_color=<color>
@@ -2131,6 +2149,8 @@ _format_command(Evas_Object *eo_obj, Evas_Object_Textblock_Format *fmt, const ch
2131 * @li "#RRGGBBAA" 2149 * @li "#RRGGBBAA"
2132 * @li "#RGB" 2150 * @li "#RGB"
2133 * @li "#RGBA" 2151 * @li "#RGBA"
2152 * @li "rgb(r,g,b)"
2153 * @li "rgba(r,g,b,a)"
2134 * @li "color_name" like "red" 2154 * @li "color_name" like "red"
2135 * @code 2155 * @code
2136 * strikethrough_color=<color> 2156 * strikethrough_color=<color>
diff --git a/src/lib/evas/common/evas_text_utils.c b/src/lib/evas/common/evas_text_utils.c
index b7cbe1b219..e9c5cc2c94 100644
--- a/src/lib/evas/common/evas_text_utils.c
+++ b/src/lib/evas/common/evas_text_utils.c
@@ -1299,7 +1299,7 @@ _hex_string_get(char ch, Eina_Bool *ok)
1299 * 1299 *
1300 * @param source source buffer 1300 * @param source source buffer
1301 * @param dest destination buffer 1301 * @param dest destination buffer
1302 * @param max maximum number of chars to be copyed 1302 * @param max maximum number of chars to be copied
1303 * @return number of spaces removed 1303 * @return number of spaces removed
1304 */ 1304 */
1305 1305
@@ -1323,6 +1323,36 @@ size_t remove_spaces_lowercase(const char* source,char * dest,size_t max)
1323 return spaces; 1323 return spaces;
1324} 1324}
1325 1325
1326/**
1327 * @internal
1328 * read color component from string
1329 *
1330 * @param source source string
1331 * @param[out] next position after the color compnent
1332 * @param byte store color value read from string
1333 * @return if read success
1334 */
1335
1336Eina_Bool read_byte_color_component(const char* source,char ** next,unsigned char * byte)
1337{
1338 const char *p_start = source;
1339 char *p_stop = NULL;
1340 Eina_Bool b_ret = EINA_TRUE;
1341 long value = 0;
1342 value = strtol(p_start, &p_stop, 10);
1343 if ( value > 255 || value < 0 || p_start == p_stop )
1344 {
1345 b_ret = EINA_FALSE;
1346 }
1347
1348 if (next)
1349 *next = p_stop;
1350 if (byte)
1351 *byte = (unsigned char) value;
1352
1353 return b_ret;
1354}
1355
1326 1356
1327/** 1357/**
1328 * @internal 1358 * @internal
@@ -1332,12 +1362,11 @@ size_t remove_spaces_lowercase(const char* source,char * dest,size_t max)
1332 * 3. "#RGB" 1362 * 3. "#RGB"
1333 * 4. "#RGBA" 1363 * 4. "#RGBA"
1334 * 5. "color names" 1364 * 5. "color names"
1365 * 6. "rgb(r,g,b)"
1366 * 7. "rgba(r,g,b,a)"
1335 * TODO (we may use specific color parser) 1367 * TODO (we may use specific color parser)
1336 * 6. "rgb(RR,GG,BB)" 1368 * 8. "hsl(H,S,L)"
1337 * 7. "rgba(RR,GG,BB,AA)" 1369 * 9. "hsla(H,S,L,A)"
1338 * 8. "rgb(%R,%G,%B)"
1339 * 9. "rgba(%R,%G,%B,%A)"
1340 * 10."hsv(HH,SS,VV)"
1341 * To the rgba values. 1370 * To the rgba values.
1342 * 1371 *
1343 * @param[in] str The string to parse - NOT NULL. 1372 * @param[in] str The string to parse - NOT NULL.
@@ -1393,24 +1422,61 @@ evas_common_format_color_parse(const char *str, int slen,
1393 } 1422 }
1394 else v = EINA_FALSE; 1423 else v = EINA_FALSE;
1395 } 1424 }
1396 else 1425 else if (slen <= 21)/* search for rgb(),hsv(),colorname, 20 is length of rgba(255,255,255,255) */
1397 { 1426 {
1398 /*remove spaces and convert name to lowercase*/ 1427 /*remove spaces and convert name to lowercase*/
1399 char color_name[0xFF] = {0}; 1428 char color_name[0xFF] = {0};
1400 slen = slen - (int) remove_spaces_lowercase(str,color_name,0xFF); 1429 slen = slen - (int) remove_spaces_lowercase(str,color_name,0xFF);
1401 static size_t color_array_length = sizeof(color_name_value_sorted)/sizeof(Color_Name_Value); 1430
1402 Color_Name_Value* pcolor = (Color_Name_Value*) bsearch(color_name, color_name_value_sorted, color_array_length, sizeof(Color_Name_Value),_color_name_search); 1431 if ((strncmp(color_name,"rgb(",4) == 0) && color_name[slen-1] == ')'&& slen >= 10 && slen <=16) /* rgb() */
1403 if (pcolor)
1404 { 1432 {
1405 *r = pcolor->r; 1433 char * p_color = &color_name[3];
1406 *g = pcolor->g; 1434
1407 *b = pcolor->b; 1435 if (
1408 *a = 0xff; 1436 (!read_byte_color_component(++p_color,&p_color,r) || !p_color || *p_color != ',') ||
1437 (!read_byte_color_component(++p_color,&p_color,g) || !p_color || *p_color != ',') ||
1438 (!read_byte_color_component(++p_color,&p_color,b) || !p_color || *p_color != ')')
1439 )
1440 {
1441 *r = *g = *b = *a = 0;
1442 v = EINA_FALSE;
1443 }
1444 else
1445 {
1446 *a = 0xff;
1447 }
1409 } 1448 }
1410 else 1449 else if ((strncmp(color_name,"rgba(",4) == 0) && color_name[slen-1] == ')'&& slen >= 13 && slen <=21) /* rgba() */
1411 { 1450 {
1412 v = EINA_FALSE; 1451 char * p_color = &color_name[4];
1452
1453 if (
1454 (!read_byte_color_component(++p_color,&p_color,r) || !p_color || *p_color != ',') ||
1455 (!read_byte_color_component(++p_color,&p_color,g) || !p_color || *p_color != ',') ||
1456 (!read_byte_color_component(++p_color,&p_color,b) || !p_color || *p_color != ',') ||
1457 (!read_byte_color_component(++p_color,&p_color,a) || !p_color || *p_color != ')')
1458 )
1459 {
1460 *r = *g = *b = *a = 0;
1461 v = EINA_FALSE;
1462 }
1413 } 1463 }
1464 else
1465 {
1466 static size_t color_array_length = sizeof(color_name_value_sorted)/sizeof(Color_Name_Value);
1467 Color_Name_Value* pcolor = (Color_Name_Value*) bsearch(color_name, color_name_value_sorted, color_array_length, sizeof(Color_Name_Value),_color_name_search);
1468 if (pcolor)
1469 {
1470 *r = pcolor->r;
1471 *g = pcolor->g;
1472 *b = pcolor->b;
1473 *a = 0xff;
1474 }
1475 else
1476 {
1477 v = EINA_FALSE;
1478 }
1479 }
1414 } 1480 }
1415 1481
1416 if(*a != 0xFF) 1482 if(*a != 0xFF)