00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include <stdio.h>
00026 #include <string.h>
00027 #include "cr-rgb.h"
00028 #include "cr-term.h"
00029 #include "cr-parser.h"
00030
00031 static CRRgb gv_standard_colors[] = {
00032 {"aliceblue", 240, 248, 255, 0,},
00033 {"antiquewhite", 250, 235, 215, 0},
00034 {"aqua", 0, 255, 255, 0},
00035 {"aquamarine", 127, 255, 212, 0},
00036 {"azure", 240, 255, 255, 0},
00037 {"beige", 245, 245, 220, 0},
00038 {"bisque", 255, 228, 196, 0},
00039 {"black", 0, 0, 0, 0},
00040 {"blanchedalmond", 255, 235, 205, 0},
00041 {"blue", 0, 0, 255, 0},
00042 {"blueviolet", 138, 43, 226, 0},
00043 {"brown", 165, 42, 42, 0},
00044 {"burlywood", 222, 184, 135, 0},
00045 {"cadetblue", 95, 158, 160, 0},
00046 {"chartreuse", 127, 255, 0, 0},
00047 {"chocolate", 210, 105, 30, 0},
00048 {"coral", 255, 127, 80, 0},
00049 {"cornflowerblue", 100, 149, 237, 0},
00050 {"cornsilk", 255, 248, 220, 0},
00051 {"crimson", 220, 20, 60, 0},
00052 {"cyan", 0, 255, 255, 0},
00053 {"darkblue", 0, 0, 139, 0},
00054 {"darkcyan", 0, 139, 139, 0},
00055 {"darkgoldenrod", 184, 134, 11, 0},
00056 {"darkgray", 169, 169, 169, 0},
00057 {"darkgreen", 0, 100, 0, 0},
00058 {"darkgrey", 169, 169, 169, 0},
00059 {"darkkhaki", 189, 183, 107, 0},
00060 {"darkmagenta", 139, 0, 139, 0},
00061 {"darkolivegreen", 85, 107, 47, 0},
00062 {"darkorange", 255, 140, 0, 0},
00063 {"darkorchid", 153, 50, 204, 0},
00064 {"darkred", 139, 0, 0, 0},
00065 {"darksalmon", 233, 150, 122, 0},
00066 {"darkseagreen", 143, 188, 143, 0},
00067 {"darkslateblue", 72, 61, 139, 0},
00068 {"darkslategray", 47, 79, 79, 0},
00069 {"darkslategrey", 47, 79, 79, 0},
00070 {"darkturquoise", 0, 206, 209, 0},
00071 {"darkviolet", 148, 0, 211, 0},
00072 {"deeppink", 255, 20, 147, 0},
00073 {"deepskyblue", 0, 191, 255, 0},
00074 {"dimgray", 105, 105, 105, 0},
00075 {"dimgrey", 105, 105, 105, 0},
00076 {"dodgerblue", 30, 144, 255, 0},
00077 {"firebrick", 178, 34, 34, 0},
00078 {"floralwhite", 255, 250, 240, 0},
00079 {"forestgreen", 34, 139, 34, 0},
00080 {"fuchsia", 255, 0, 255, 0},
00081 {"gainsboro", 220, 220, 220, 0},
00082 {"ghostwhite", 248, 248, 255, 0},
00083 {"gold", 255, 215, 0, 0},
00084 {"goldenrod", 218, 165, 32, 0},
00085 {"gray", 128, 128, 128, 0},
00086 {"grey", 128, 128, 128, 0},
00087 {"green", 0, 128, 0, 0},
00088 {"greenyellow", 173, 255, 47, 0},
00089 {"honeydew", 240, 255, 240, 0},
00090 {"hotpink", 255, 105, 180, 0},
00091 {"indianred", 205, 92, 92, 0},
00092 {"indigo", 75, 0, 130, 0},
00093 {"ivory", 255, 255, 240, 0},
00094 {"khaki", 240, 230, 140, 0},
00095 {"lavender", 230, 230, 250, 0},
00096 {"lavenderblush", 255, 240, 245, 0},
00097 {"lawngreen", 124, 252, 0, 0},
00098 {"lemonchiffon", 255, 250, 205, 0},
00099 {"lightblue", 173, 216, 230, 0},
00100 {"lightcoral", 240, 128, 128, 0},
00101 {"lightcyan", 224, 255, 255, 0},
00102 {"lightgoldenrodyellow", 250, 250, 210, 0},
00103 {"lightgray", 211, 211, 211, 0},
00104 {"lightgreen", 144, 238, 144, 0},
00105 {"lightgrey", 211, 211, 211, 0},
00106 {"lightpink", 255, 182, 193, 0},
00107 {"lightsalmon", 255, 160, 122, 0},
00108 {"lightseagreen", 32, 178, 170, 0},
00109 {"lightskyblue", 135, 206, 250, 0},
00110 {"lightslategray", 119, 136, 153, 0},
00111 {"lightslategrey", 119, 136, 153, 0},
00112 {"lightsteelblue", 176, 196, 222, 0},
00113 {"lightyellow", 255, 255, 224, 0},
00114 {"lime", 0, 255, 0, 0},
00115 {"limegreen", 50, 205, 50, 0},
00116 {"linen", 250, 240, 230, 0},
00117 {"magenta", 255, 0, 255, 0},
00118 {"maroon", 128, 0, 0, 0},
00119 {"mediumaquamarine", 102, 205, 170, 0},
00120 {"mediumblue", 0, 0, 205, 0},
00121 {"mediumorchid", 186, 85, 211, 0},
00122 {"mediumpurple", 147, 112, 219, 0},
00123 {"mediumseagreen", 60, 179, 113, 0},
00124 {"mediumslateblue", 123, 104, 238, 0},
00125 {"mediumspringgreen", 0, 250, 154, 0},
00126 {"mediumturquoise", 72, 209, 204, 0},
00127 {"mediumvioletred", 199, 21, 133, 0},
00128 {"midnightblue", 25, 25, 112, 0},
00129 {"mintcream", 245, 255, 250, 0},
00130 {"mistyrose", 255, 228, 225, 0},
00131 {"moccasin", 255, 228, 181, 0},
00132 {"navajowhite", 255, 222, 173, 0},
00133 {"navy", 0, 0, 128, 0},
00134 {"oldlace", 253, 245, 230, 0},
00135 {"olive", 128, 128, 0, 0},
00136 {"olivedrab", 107, 142, 35, 0},
00137 {"orange", 255, 165, 0, 0},
00138 {"orangered", 255, 69, 0, 0},
00139 {"orchid", 218, 112, 214, 0},
00140 {"palegoldenrod", 238, 232, 170, 0},
00141 {"palegreen", 152, 251, 152, 0},
00142 {"paleturquoise", 175, 238, 238, 0},
00143 {"palevioletred", 219, 112, 147, 0},
00144 {"papayawhip", 255, 239, 213, 0},
00145 {"peachpuff", 255, 218, 185, 0},
00146 {"peru", 205, 133, 63, 0},
00147 {"pink", 255, 192, 203, 0},
00148 {"plum", 221, 160, 221, 0},
00149 {"powderblue", 176, 224, 230, 0},
00150 {"purple", 128, 0, 128, 0},
00151 {"red", 255, 0, 0, 0},
00152 {"rosybrown", 188, 143, 143, 0},
00153 {"royalblue", 65, 105, 225, 0},
00154 {"saddlebrown", 139, 69, 19, 0},
00155 {"salmon", 250, 128, 114, 0},
00156 {"sandybrown", 244, 164, 96, 0},
00157 {"seagreen", 46, 139, 87, 0},
00158 {"seashell", 255, 245, 238, 0},
00159 {"sienna", 160, 82, 45, 0},
00160 {"silver", 192, 192, 192, 0},
00161 {"skyblue", 135, 206, 235, 0},
00162 {"slateblue", 106, 90, 205, 0},
00163 {"slategray", 112, 128, 144, 0},
00164 {"slategrey", 112, 128, 144, 0},
00165 {"snow", 255, 250, 250, 0},
00166 {"springgreen", 0, 255, 127, 0},
00167 {"steelblue", 70, 130, 180, 0},
00168 {"tan", 210, 180, 140, 0},
00169 {"teal", 0, 128, 128, 0},
00170 {"thistle", 216, 191, 216, 0},
00171 {"tomato", 255, 99, 71, 0},
00172 {"turquoise", 64, 224, 208, 0},
00173 {"violet", 238, 130, 238, 0},
00174 {"wheat", 245, 222, 179, 0},
00175 {"white", 255, 255, 255, 0},
00176 {"whitesmoke", 245, 245, 245, 0,},
00177 {"yellow", 255, 255, 0, 0,},
00178 {"yellowgreen", 154, 205, 50, 0,},
00179 {"transparent", 255, 255, 255, 0, 0, 1}
00180 };
00181
00182
00183
00184
00185
00186 CRRgb *
00187 cr_rgb_new (void)
00188 {
00189 CRRgb *result = NULL;
00190
00191 result = g_try_malloc (sizeof (CRRgb));
00192
00193 if (result == NULL) {
00194 cr_utils_trace_info ("No more memory");
00195 return NULL;
00196 }
00197
00198 memset (result, 0, sizeof (CRRgb));
00199
00200 return result;
00201 }
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212 CRRgb *
00213 cr_rgb_new_with_vals (gulong a_red, gulong a_green,
00214 gulong a_blue, gboolean a_is_percentage)
00215 {
00216 CRRgb *result = NULL;
00217
00218 result = cr_rgb_new ();
00219
00220 g_return_val_if_fail (result, NULL);
00221
00222 result->red = a_red;
00223 result->green = a_green;
00224 result->blue = a_blue;
00225 result->is_percentage = a_is_percentage;
00226
00227 return result;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236 guchar *
00237 cr_rgb_to_string (CRRgb * a_this)
00238 {
00239 guchar *result = NULL;
00240 GString *str_buf = NULL;
00241
00242 str_buf = g_string_new (NULL);
00243 g_return_val_if_fail (str_buf, NULL);
00244
00245 if (a_this->is_percentage == 1) {
00246 g_string_append_printf (str_buf, "%ld", a_this->red);
00247
00248 g_string_append (str_buf, "%, ");
00249
00250 g_string_append_printf (str_buf, "%ld", a_this->green);
00251 g_string_append (str_buf, "%, ");
00252
00253 g_string_append_printf (str_buf, "%ld", a_this->blue);
00254 g_string_append_c (str_buf, '%');
00255 } else {
00256 g_string_append_printf (str_buf, "%ld", a_this->red);
00257 g_string_append (str_buf, ", ");
00258
00259 g_string_append_printf (str_buf, "%ld", a_this->green);
00260 g_string_append (str_buf, ", ");
00261
00262 g_string_append_printf (str_buf, "%ld", a_this->blue);
00263 }
00264
00265 if (str_buf) {
00266 result = str_buf->str;
00267 g_string_free (str_buf, FALSE);
00268 }
00269
00270 return result;
00271 }
00272
00273
00274
00275
00276
00277
00278
00279
00280 void
00281 cr_rgb_dump (CRRgb * a_this, FILE * a_fp)
00282 {
00283 guchar *str = NULL;
00284
00285 g_return_if_fail (a_this);
00286
00287 str = cr_rgb_to_string (a_this);
00288
00289 if (str) {
00290 fprintf (a_fp, "%s", str);
00291 g_free (str);
00292 str = NULL;
00293 }
00294 }
00295
00296
00297
00298
00299
00300
00301
00302 enum CRStatus
00303 cr_rgb_compute_from_percentage (CRRgb * a_this)
00304 {
00305 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00306
00307 if (a_this->is_percentage == FALSE)
00308 return CR_OK;
00309 a_this->red = a_this->red * 255 / 100;
00310 a_this->green = a_this->green * 255 / 100;
00311 a_this->blue = a_this->blue * 255 / 100;
00312 a_this->is_percentage = FALSE;
00313 return CR_OK;
00314 }
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325 enum CRStatus
00326 cr_rgb_set (CRRgb * a_this, gulong a_red,
00327 gulong a_green, gulong a_blue, gboolean a_is_percentage)
00328 {
00329 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR);
00330 if (a_is_percentage != FALSE) {
00331 g_return_val_if_fail (a_red <= 100
00332 && a_green <= 100
00333 && a_blue <= 100, CR_BAD_PARAM_ERROR);
00334 }
00335
00336 a_this->is_percentage = a_is_percentage;
00337
00338 a_this->red = a_red;
00339 a_this->green = a_green;
00340 a_this->blue = a_blue;
00341 a_this->inherit = FALSE ;
00342 a_this->is_transparent = FALSE ;
00343 return CR_OK;
00344 }
00345
00346
00347
00348
00349
00350
00351
00352
00353 enum CRStatus
00354 cr_rgb_set_to_inherit (CRRgb *a_this, gboolean a_inherit)
00355 {
00356 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ;
00357
00358 a_this->inherit = a_inherit ;
00359
00360 return CR_OK ;
00361 }
00362
00363 gboolean
00364 cr_rgb_is_set_to_inherit (CRRgb *a_this)
00365 {
00366 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ;
00367
00368 return a_this->inherit ;
00369 }
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 gboolean
00380 cr_rgb_is_set_to_transparent (CRRgb *a_this)
00381 {
00382 g_return_val_if_fail (a_this, FALSE) ;
00383 return a_this->is_transparent ;
00384 }
00385
00386
00387
00388
00389
00390
00391
00392 enum CRStatus
00393 cr_rgb_set_to_transparent (CRRgb *a_this,
00394 gboolean a_is_transparent)
00395 {
00396 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ;
00397 a_this->is_transparent = a_is_transparent ;
00398 return CR_OK ;
00399 }
00400
00401
00402
00403
00404
00405
00406
00407 enum CRStatus
00408 cr_rgb_set_from_rgb (CRRgb * a_this, CRRgb * a_rgb)
00409 {
00410 g_return_val_if_fail (a_this && a_rgb, CR_BAD_PARAM_ERROR);
00411
00412 cr_rgb_copy (a_this, a_rgb) ;
00413
00414 return CR_OK;
00415 }
00416
00417 enum CRStatus
00418 cr_rgb_set_from_name (CRRgb * a_this, const guchar * a_color_name)
00419 {
00420 gulong i = 0;
00421 enum CRStatus status = CR_OK;
00422
00423 g_return_val_if_fail (a_this && a_color_name, CR_BAD_PARAM_ERROR);
00424
00425 for (i = 0; i < sizeof (gv_standard_colors); i++) {
00426 if (!strcmp (a_color_name, gv_standard_colors[i].name)) {
00427 cr_rgb_set_from_rgb (a_this, &gv_standard_colors[i]);
00428 break;
00429 }
00430 }
00431
00432 if (i < sizeof (gv_standard_colors))
00433 status = CR_OK;
00434 else
00435 status = CR_UNKNOWN_TYPE_ERROR;
00436
00437 return status;
00438 }
00439
00440 enum CRStatus
00441 cr_rgb_set_from_hex_str (CRRgb * a_this, const guchar * a_hex)
00442 {
00443 enum CRStatus status = CR_OK;
00444 gulong i = 0;
00445 guchar colors[3] = { 0 };
00446
00447 g_return_val_if_fail (a_this && a_hex, CR_BAD_PARAM_ERROR);
00448
00449 if (strlen (a_hex) == 3) {
00450 for (i = 0; i < 3; i++) {
00451 if (a_hex[i] >= '0' && a_hex[i] <= '9') {
00452 colors[i] = a_hex[i] - '0';
00453 colors[i] = (colors[i] << 4) | colors[i];
00454 } else if (a_hex[i] >= 'a' && a_hex[i] <= 'z') {
00455 colors[i] = 10 + a_hex[i] - 'a';
00456 colors[i] = (colors[i] << 4) | colors[i];
00457 } else if (a_hex[i] >= 'A' && a_hex[i] <= 'Z') {
00458 colors[i] = 10 + a_hex[i] - 'A';
00459 colors[i] = (colors[i] << 4) | colors[i];
00460 } else {
00461 status = CR_UNKNOWN_TYPE_ERROR;
00462 }
00463 }
00464 } else if (strlen (a_hex) == 6) {
00465 for (i = 0; i < 6; i++) {
00466 if (a_hex[i] >= '0' && a_hex[i] <= '9') {
00467 colors[i / 2] <<= 4;
00468 colors[i / 2] |= a_hex[i] - '0';
00469 status = CR_OK;
00470 } else if (a_hex[i] >= 'a' && a_hex[i] <= 'z') {
00471 colors[i / 2] <<= 4;
00472 colors[i / 2] |= 10 + a_hex[i] - 'a';
00473 status = CR_OK;
00474 } else if (a_hex[i] >= 'A' && a_hex[i] <= 'Z') {
00475 colors[i / 2] <<= 4;
00476 colors[i / 2] |= 10 + a_hex[i] - 'A';
00477 status = CR_OK;
00478 } else {
00479 status = CR_UNKNOWN_TYPE_ERROR;
00480 }
00481 }
00482 } else {
00483 status = CR_UNKNOWN_TYPE_ERROR;
00484 }
00485
00486 if (status == CR_OK) {
00487 status = cr_rgb_set (a_this, colors[0],
00488 colors[1], colors[2], FALSE);
00489 cr_rgb_set_to_transparent (a_this, FALSE) ;
00490 }
00491 return status;
00492 }
00493
00494
00495
00496
00497
00498
00499 enum CRStatus
00500 cr_rgb_set_from_term (CRRgb *a_this, const struct _CRTerm *a_value)
00501 {
00502 enum CRStatus status = CR_OK ;
00503 g_return_val_if_fail (a_this && a_value,
00504 CR_BAD_PARAM_ERROR) ;
00505
00506 switch(a_value->type) {
00507 case TERM_RGB:
00508 if (a_value->content.rgb) {
00509 cr_rgb_set_from_rgb
00510 (a_this, a_value->content.rgb) ;
00511 }
00512 break ;
00513 case TERM_IDENT:
00514 if (a_value->content.str
00515 && a_value->content.str->stryng
00516 && a_value->content.str->stryng->str) {
00517 if (!strncmp ("inherit",
00518 a_value->content.str->stryng->str,
00519 sizeof ("inherit")-1)) {
00520 a_this->inherit = TRUE;
00521 a_this->is_transparent = FALSE ;
00522 } else {
00523 status = cr_rgb_set_from_name
00524 (a_this,
00525 a_value->content.str->stryng->str) ;
00526 }
00527 } else {
00528 cr_utils_trace_info
00529 ("a_value has NULL string value") ;
00530 }
00531 break ;
00532 case TERM_HASH:
00533 if (a_value->content.str
00534 && a_value->content.str->stryng
00535 && a_value->content.str->stryng->str) {
00536 status = cr_rgb_set_from_hex_str
00537 (a_this,
00538 a_value->content.str->stryng->str) ;
00539 } else {
00540 cr_utils_trace_info
00541 ("a_value has NULL string value") ;
00542 }
00543 break ;
00544 default:
00545 status = CR_UNKNOWN_TYPE_ERROR ;
00546 }
00547 return status ;
00548 }
00549
00550 enum CRStatus
00551 cr_rgb_copy (CRRgb *a_dest, CRRgb*a_src)
00552 {
00553 g_return_val_if_fail (a_dest && a_src,
00554 CR_BAD_PARAM_ERROR) ;
00555
00556 memcpy (a_dest, a_src, sizeof (CRRgb)) ;
00557 return CR_OK ;
00558 }
00559
00560
00561
00562
00563
00564
00565 void
00566 cr_rgb_destroy (CRRgb * a_this)
00567 {
00568 g_return_if_fail (a_this);
00569 g_free (a_this);
00570 }
00571
00572
00573
00574
00575
00576
00577
00578
00579 CRRgb *cr_rgb_parse_from_buf (const guchar *a_str,
00580 enum CREncoding a_enc)
00581 {
00582 enum CRStatus status = CR_OK ;
00583 CRTerm *value = NULL ;
00584 CRParser * parser = NULL;
00585 CRRgb *result = NULL;
00586
00587 g_return_val_if_fail (a_str, NULL);
00588
00589 parser = cr_parser_new_from_buf ((guchar*)a_str, strlen (a_str),
00590 a_enc, FALSE) ;
00591
00592 g_return_val_if_fail (parser, NULL);
00593
00594 status = cr_parser_try_to_skip_spaces_and_comments (parser) ;
00595 if (status != CR_OK)
00596 goto cleanup;
00597
00598 status = cr_parser_parse_term (parser, &value);
00599 if (status != CR_OK)
00600 goto cleanup;
00601
00602 result = cr_rgb_new ();
00603 if (!result)
00604 goto cleanup;
00605
00606 status = cr_rgb_set_from_term (result, value);
00607
00608 cleanup:
00609 if (parser) {
00610 cr_parser_destroy (parser);
00611 parser = NULL;
00612 }
00613 if (value) {
00614 cr_term_destroy(value);
00615 value = NULL;
00616 }
00617 return result ;
00618 }
00619
00620
00621