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
00026
00027 #include <stdio.h>
00028 #include <string.h>
00029 #include "cr-rgb.h"
00030
00031 static CRRgb gv_standard_colors[] =
00032 {
00033 { "aliceblue", 240, 248, 255, 0},
00034 { "antiquewhite", 250, 235, 215, 0},
00035 { "aqua", 0, 255, 255, 0},
00036 { "aquamarine", 127, 255, 212, 0},
00037 { "azure", 240, 255, 255, 0},
00038 { "beige", 245, 245, 220, 0},
00039 { "bisque", 255, 228, 196, 0},
00040 { "black", 0, 0, 0, 0},
00041 { "blanchedalmond", 255, 235, 205, 0},
00042 { "blue", 0, 0, 255, 0},
00043 { "blueviolet", 138, 43, 226, 0},
00044 { "brown", 165, 42, 42, 0},
00045 { "burlywood", 222, 184, 135, 0},
00046 { "cadetblue", 95, 158, 160, 0},
00047 { "chartreuse", 127, 255, 0, 0},
00048 { "chocolate", 210, 105, 30, 0},
00049 { "coral", 255, 127, 80, 0},
00050 { "cornflowerblue", 100, 149, 237, 0},
00051 { "cornsilk", 255, 248, 220, 0},
00052 { "crimson", 220, 20, 60, 0},
00053 { "cyan", 0, 255, 255, 0},
00054 { "darkblue", 0, 0, 139, 0},
00055 { "darkcyan", 0, 139, 139, 0},
00056 { "darkgoldenrod", 184, 134, 11, 0},
00057 { "darkgray", 169, 169, 169, 0},
00058 { "darkgreen", 0, 100, 0, 0},
00059 { "darkgrey", 169, 169, 169, 0},
00060 { "darkkhaki", 189, 183, 107, 0},
00061 { "darkmagenta", 139, 0, 139, 0},
00062 { "darkolivegreen", 85, 107, 47, 0},
00063 { "darkorange", 255, 140, 0, 0},
00064 { "darkorchid", 153, 50, 204, 0},
00065 { "darkred", 139, 0, 0, 0},
00066 { "darksalmon", 233, 150, 122, 0},
00067 { "darkseagreen", 143, 188, 143, 0},
00068 { "darkslateblue", 72, 61, 139, 0},
00069 { "darkslategray", 47, 79, 79, 0},
00070 { "darkslategrey", 47, 79, 79, 0},
00071 { "darkturquoise", 0, 206, 209, 0},
00072 { "darkviolet", 148, 0, 211, 0},
00073 { "deeppink", 255, 20, 147, 0},
00074 { "deepskyblue", 0, 191, 255, 0},
00075 { "dimgray", 105, 105, 105, 0},
00076 { "dimgrey", 105, 105, 105, 0},
00077 { "dodgerblue", 30, 144, 255, 0},
00078 { "firebrick", 178, 34, 34, 0},
00079 { "floralwhite", 255, 250, 240, 0},
00080 { "forestgreen", 34, 139, 34, 0},
00081 { "fuchsia", 255, 0, 255, 0},
00082 { "gainsboro", 220, 220, 220, 0},
00083 { "ghostwhite", 248, 248, 255, 0},
00084 { "gold", 255, 215, 0, 0},
00085 { "goldenrod", 218, 165, 32, 0},
00086 { "gray", 128, 128, 128, 0},
00087 { "grey", 128, 128, 128, 0},
00088 { "green", 0, 128, 0, 0},
00089 { "greenyellow", 173, 255, 47, 0},
00090 { "honeydew", 240, 255, 240, 0},
00091 { "hotpink", 255, 105, 180, 0},
00092 { "indianred", 205, 92, 92, 0},
00093 { "indigo", 75, 0, 130, 0},
00094 { "ivory", 255, 255, 240, 0},
00095 { "khaki", 240, 230, 140, 0},
00096 { "lavender", 230, 230, 250, 0},
00097 { "lavenderblush", 255, 240, 245, 0},
00098 { "lawngreen", 124, 252, 0, 0},
00099 { "lemonchiffon", 255, 250, 205, 0},
00100 { "lightblue", 173, 216, 230, 0},
00101 { "lightcoral", 240, 128, 128, 0},
00102 { "lightcyan", 224, 255, 255, 0},
00103 { "lightgoldenrodyellow", 250, 250, 210, 0},
00104 { "lightgray", 211, 211, 211, 0},
00105 { "lightgreen", 144, 238, 144, 0},
00106 { "lightgrey", 211, 211, 211, 0},
00107 { "lightpink", 255, 182, 193, 0},
00108 { "lightsalmon", 255, 160, 122, 0},
00109 { "lightseagreen", 32, 178, 170, 0},
00110 { "lightskyblue", 135, 206, 250, 0},
00111 { "lightslategray", 119, 136, 153, 0},
00112 { "lightslategrey", 119, 136, 153, 0},
00113 { "lightsteelblue", 176, 196, 222, 0},
00114 { "lightyellow", 255, 255, 224, 0},
00115 { "lime", 0, 255, 0, 0},
00116 { "limegreen", 50, 205, 50, 0},
00117 { "linen", 250, 240, 230, 0},
00118 { "magenta", 255, 0, 255, 0},
00119 { "maroon", 128, 0, 0, 0},
00120 { "mediumaquamarine", 102, 205, 170, 0},
00121 { "mediumblue", 0, 0, 205, 0},
00122 { "mediumorchid", 186, 85, 211, 0},
00123 { "mediumpurple", 147, 112, 219, 0},
00124 { "mediumseagreen", 60, 179, 113, 0},
00125 { "mediumslateblue", 123, 104, 238, 0},
00126 { "mediumspringgreen", 0, 250, 154, 0},
00127 { "mediumturquoise", 72, 209, 204, 0},
00128 { "mediumvioletred", 199, 21, 133, 0},
00129 { "midnightblue", 25, 25, 112, 0},
00130 { "mintcream", 245, 255, 250, 0},
00131 { "mistyrose", 255, 228, 225, 0},
00132 { "moccasin", 255, 228, 181, 0},
00133 { "navajowhite", 255, 222, 173, 0},
00134 { "navy", 0, 0, 128, 0},
00135 { "oldlace", 253, 245, 230, 0},
00136 { "olive", 128, 128, 0, 0},
00137 { "olivedrab", 107, 142, 35, 0},
00138 { "orange", 255, 165, 0, 0},
00139 { "orangered", 255, 69, 0, 0},
00140 { "orchid", 218, 112, 214, 0},
00141 { "palegoldenrod", 238, 232, 170, 0},
00142 { "palegreen", 152, 251, 152, 0},
00143 { "paleturquoise", 175, 238, 238, 0},
00144 { "palevioletred", 219, 112, 147, 0},
00145 { "papayawhip", 255, 239, 213, 0},
00146 { "peachpuff", 255, 218, 185, 0},
00147 { "peru", 205, 133, 63, 0},
00148 { "pink", 255, 192, 203, 0},
00149 { "plum", 221, 160, 221, 0},
00150 { "powderblue", 176, 224, 230, 0},
00151 { "purple", 128, 0, 128, 0},
00152 { "red", 255, 0, 0, 0},
00153 { "rosybrown", 188, 143, 143, 0},
00154 { "royalblue", 65, 105, 225, 0},
00155 { "saddlebrown", 139, 69, 19, 0},
00156 { "salmon", 250, 128, 114, 0},
00157 { "sandybrown", 244, 164, 96, 0},
00158 { "seagreen", 46, 139, 87, 0},
00159 { "seashell", 255, 245, 238, 0},
00160 { "sienna", 160, 82, 45, 0},
00161 { "silver", 192, 192, 192, 0},
00162 { "skyblue", 135, 206, 235, 0},
00163 { "slateblue", 106, 90, 205, 0},
00164 { "slategray", 112, 128, 144, 0},
00165 { "slategrey", 112, 128, 144, 0},
00166 { "snow", 255, 250, 250, 0},
00167 { "springgreen", 0, 255, 127, 0},
00168 { "steelblue", 70, 130, 180, 0},
00169 { "tan", 210, 180, 140, 0},
00170 { "teal", 0, 128, 128, 0},
00171 { "thistle", 216, 191, 216, 0},
00172 { "tomato", 255, 99, 71, 0},
00173 { "turquoise", 64, 224, 208, 0},
00174 { "violet", 238, 130, 238, 0},
00175 { "wheat", 245, 222, 179, 0},
00176 { "white", 255, 255, 255, 0},
00177 { "whitesmoke", 245, 245, 245, 0},
00178 { "yellow", 255, 255, 0, 0},
00179 { "yellowgreen", 154, 205, 50, 0}
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 {
00195 cr_utils_trace_info ("No more memory") ;
00196 return NULL ;
00197 }
00198
00199 memset (result, 0, sizeof (CRRgb)) ;
00200
00201 return result ;
00202 }
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214 CRRgb *
00215 cr_rgb_new_with_vals (gulong a_red, gulong a_green,
00216 gulong a_blue, gboolean a_is_percentage)
00217 {
00218 CRRgb *result = NULL ;
00219
00220 result = cr_rgb_new () ;
00221
00222 g_return_val_if_fail (result, NULL) ;
00223
00224 result->red = a_red ;
00225 result->green = a_green ;
00226 result->blue = a_blue ;
00227 result->is_percentage = a_is_percentage ;
00228
00229 return result ;
00230 }
00231
00232
00233
00234
00235
00236
00237
00238 guchar *
00239 cr_rgb_to_string (CRRgb *a_this)
00240 {
00241 guchar *result = NULL ;
00242 GString *str_buf = NULL ;
00243
00244 str_buf = g_string_new (NULL) ;
00245 g_return_val_if_fail (str_buf, NULL) ;
00246
00247 if (a_this->is_percentage == TRUE)
00248 {
00249 g_string_append_printf (str_buf, "%ld", a_this->red) ;
00250
00251 g_string_append_c (str_buf, '%') ;
00252 g_string_append_printf (str_buf,", ") ;
00253
00254 g_string_append_printf (str_buf,"%ld", a_this->green) ;
00255 g_string_append_c (str_buf, '%') ; ;
00256 g_string_append_printf (str_buf,", ") ;
00257
00258 g_string_append_printf (str_buf,"%ld", a_this->blue) ;
00259 g_string_append_c (str_buf, '%') ;
00260 }
00261 else
00262 {
00263 g_string_append_printf (str_buf,"%ld", a_this->red) ;
00264 g_string_append_printf (str_buf,", ") ;
00265
00266 g_string_append_printf (str_buf,"%ld", a_this->green) ;
00267 g_string_append_printf (str_buf,", ") ;
00268
00269 g_string_append_printf (str_buf,"%ld", a_this->blue) ;
00270 }
00271
00272 if (str_buf)
00273 {
00274 result = str_buf->str ;
00275 g_string_free (str_buf, FALSE) ;
00276 }
00277
00278 return result ;
00279 }
00280
00281
00282
00283
00284
00285
00286
00287
00288 void
00289 cr_rgb_dump (CRRgb *a_this, FILE *a_fp)
00290 {
00291 guchar *str = NULL ;
00292
00293 g_return_if_fail (a_this) ;
00294
00295 str = cr_rgb_to_string (a_this) ;
00296
00297 if (str)
00298 {
00299 fprintf (a_fp, "%s",str) ;
00300 g_free (str) ;
00301 str = NULL ;
00302 }
00303 }
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316 enum CRStatus
00317 cr_rgb_set (CRRgb *a_this, gulong a_red,
00318 gulong a_green, gulong a_blue,
00319 gboolean a_is_percentage)
00320 {
00321 g_return_val_if_fail (a_this, CR_BAD_PARAM_ERROR) ;
00322 if (a_is_percentage != FALSE)
00323 {
00324 g_return_val_if_fail (a_red <= 100
00325 && a_green <= 100
00326 && a_blue <= 100,
00327 CR_BAD_PARAM_ERROR) ;
00328 }
00329
00330 a_this->is_percentage = a_is_percentage ;
00331
00332 a_this->red = a_red ;
00333 a_this->green = a_green ;
00334 if (a_is_percentage != FALSE)
00335 {
00336 if (a_red + a_green >= 100)
00337 {
00338 a_green = 100 - a_red ;
00339 }
00340 a_this->blue = 100 - a_red - a_green ;
00341 }
00342 else
00343 {
00344 a_this->blue = a_blue ;
00345 }
00346
00347 return CR_OK ;
00348 }
00349
00350
00351
00352
00353
00354
00355
00356 enum CRStatus
00357 cr_rgb_set_from_rgb (CRRgb *a_this, CRRgb *a_rgb)
00358 {
00359 g_return_val_if_fail (a_this && a_rgb,
00360 CR_BAD_PARAM_ERROR) ;
00361
00362 cr_rgb_set (a_this, a_rgb->red, a_rgb->green,
00363 a_rgb->blue, a_rgb->is_percentage) ;
00364
00365 return CR_OK ;
00366 }
00367
00368
00369 enum CRStatus
00370 cr_rgb_set_from_name (CRRgb *a_this, const guchar *a_color_name)
00371 {
00372 gulong i = 0 ;
00373 enum CRStatus status = CR_OK ;
00374
00375 g_return_val_if_fail (a_this && a_color_name, CR_BAD_PARAM_ERROR) ;
00376
00377 for (i = 0 ; i < sizeof (gv_standard_colors); i++)
00378 {
00379 if (!strcmp (a_color_name, gv_standard_colors[i].name))
00380 {
00381 cr_rgb_set_from_rgb (a_this, &gv_standard_colors[i]) ;
00382 break ;
00383 }
00384 }
00385
00386 if (i < sizeof (gv_standard_colors))
00387 status = CR_OK ;
00388 else
00389 status = CR_UNKNOWN_TYPE_ERROR ;
00390
00391 return status ;
00392 }
00393
00394 enum CRStatus
00395 cr_rgb_set_from_hex_str (CRRgb *a_this, const guchar * a_hex)
00396 {
00397 enum CRStatus status = CR_OK ;
00398 gulong i = 0 ;
00399 guchar colors[3] = {0} ;
00400
00401 g_return_val_if_fail (a_this && a_hex,
00402 CR_BAD_PARAM_ERROR) ;
00403
00404 if (strlen (a_hex) == 3)
00405 {
00406 for (i = 0 ;i < 3 ; i++)
00407 {
00408 switch (a_hex[i])
00409 {
00410 case '0' ... '9':
00411 colors[i] = a_hex[i] - '0';
00412 colors[i] = (colors[i] << 4) | colors[i] ;
00413 break ;
00414
00415 case 'a' ... 'z':
00416 colors[i] = 10 + a_hex[i] - 'a';
00417 colors[i] = (colors[i] << 4) | colors[i] ;
00418 break ;
00419
00420 case 'A' ... 'Z':
00421 colors[i] = 10 + a_hex[i] - 'A';
00422 colors[i] = (colors[i] << 4) | colors[i] ;
00423 break ;
00424 default:
00425 status = CR_UNKNOWN_TYPE_ERROR ;
00426 }
00427 }
00428 }
00429 else if (strlen (a_hex) == 6)
00430 {
00431 for (i = 0 ; i < 6 ; i++)
00432 {
00433
00434 switch (a_hex[i])
00435 {
00436 case '0' ... '9':
00437 colors[i/2] <<= 4 ;
00438 colors[i/2] |= a_hex[i] - '0';
00439 status = CR_OK ;
00440 break ;
00441
00442 case 'a' ... 'z':
00443 colors[i/2] <<= 4 ;
00444 colors[i/2] |= 10 + a_hex[i] - 'a';
00445 status = CR_OK ;
00446 break ;
00447
00448 case 'A' ... 'Z':
00449 colors[i/2] <<= 4 ;
00450 colors[i/2] |= 10 + a_hex[i] - 'A';
00451 status = CR_OK ;
00452 break ;
00453
00454 default:
00455 status = CR_UNKNOWN_TYPE_ERROR ;
00456 }
00457 }
00458 }
00459 else
00460 {
00461 status = CR_UNKNOWN_TYPE_ERROR ;
00462 }
00463
00464 if (status == CR_OK)
00465 {
00466 status = cr_rgb_set (a_this, colors[0],
00467 colors[1], colors[2],
00468 FALSE) ;
00469 }
00470 return status ;
00471 }
00472
00473
00474
00475
00476
00477
00478 void
00479 cr_rgb_destroy (CRRgb *a_this)
00480 {
00481 g_return_if_fail (a_this) ;
00482
00483 g_free (a_this) ;
00484 }