summaryrefslogtreecommitdiff
path: root/src/lib/evas/common/evas_text_utils.c
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 /src/lib/evas/common/evas_text_utils.c
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
Diffstat (limited to '')
-rw-r--r--src/lib/evas/common/evas_text_utils.c98
1 files changed, 82 insertions, 16 deletions
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)