File: read_color.c

package info (click to toggle)
ggobi 2.1.9~20091212-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 19,340 kB
  • ctags: 5,083
  • sloc: ansic: 57,242; xml: 30,604; cpp: 833; makefile: 355; java: 225; perl: 201; sh: 122; python: 23
file content (316 lines) | stat: -rw-r--r-- 8,267 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/* read_color.c */
/*
 * ggobi
 * Copyright (C) AT&T, Duncan Temple Lang, Dianne Cook 1999-2005
 *
 * ggobi is free software; you may use, redistribute, and/or modify it
 * under the terms of the Common Public License, which is distributed
 * with the source code and displayed on the ggobi web site, 
 * www.ggobi.org.  For more information, contact the authors:
 *
 *   Deborah F. Swayne   dfs@research.att.com
 *   Di Cook             dicook@iastate.edu
 *   Duncan Temple Lang  duncan@wald.ucdavis.edu
 *   Andreas Buja        andreas.buja@wharton.upenn.edu
*/

#include "ggobi.h"

#include <string.h>

#include <libxml/parser.h>
#include <libxml/tree.h>

#include "read_init.h"
#include "read_xml.h"

/*
extern int xmlDoValidityCheckingDefaultValue;
*/

colorschemed *process_colorscheme (xmlNodePtr root, xmlDocPtr doc);
colorscaletype getColorSchemeType (const xmlChar * type);
colorsystem getColorSchemeSystem (const xmlChar * type);

gint getForegroundColor (gint index, xmlNodePtr node, xmlDocPtr doc,
                         colorschemed * scheme);
void getForegroundColors (xmlNodePtr node, xmlDocPtr doc,
                          colorschemed * scheme);
gint getAnnotationColor (xmlNodePtr node, xmlDocPtr doc,
                         colorschemed * scheme);
gint getBackgroundColor (xmlNodePtr node, xmlDocPtr doc,
                         colorschemed * scheme);
gint getColor (xmlNodePtr node, xmlDocPtr doc, gfloat ** original,
               GdkColor * col);

colorschemed *
read_colorscheme (gchar * fileName, GList ** list)
{
  xmlDocPtr doc;
  xmlNodePtr node;
  colorschemed *scheme = NULL; // compiler pacification

  if (!file_is_readable (fileName)
      && !(strncmp ("http", fileName, 4) == 0
           || strncmp ("ftp", fileName, 3) == 0)) {
    fprintf (stderr, "Couldn't read colorscheme from %s\n", fileName);
    fflush (stderr);
    return (NULL);
  }

/*  xmlSubstituteEntitiesDefault(1);    */
  doc = xmlParseFile (fileName);
  if (doc == NULL)
    return (NULL);

  /* If this is a colorschemes archive, then process each one individually. */

  node = xmlDocGetRootElement (doc);
  if (strcmp ((char *) node->name, "colormap") == 0) {
    scheme = process_colorscheme (node, doc);
    if (list) {
      *list = g_list_append (*list, scheme);
    }
    return (scheme);
  }

  node = XML_CHILDREN (node);
  while (node) {
    if (node->type != XML_TEXT_NODE && node->type != XML_COMMENT_NODE) {
      scheme = process_colorscheme (node, doc);
      if (list)
        *list = g_list_append (*list, scheme);
    }

    node = node->next;
  }
  xmlFreeDoc (doc);

  if (sessionOptions->verbose == GGOBI_VERBOSE)
    g_printerr ("Read colorscheme from %s\n", fileName);

  return (scheme);
}

colorschemed *
alloc_colorscheme ()
{
  colorschemed *scheme;

  scheme = (colorschemed *) g_malloc0 (sizeof (colorschemed));

  scheme->rgb = NULL;
  scheme->rgb_bg.pixel = -1;
  scheme->rgb_accent.pixel = -1;
  scheme->colorNames = g_array_new (false, false, sizeof (gchar *));

  return (scheme);
}

colorschemed *
process_colorscheme (xmlNodePtr root, xmlDocPtr doc)
{
  colorschemed *scheme;
  xmlNodePtr node;
  const xmlChar *tmp;
  xmlChar *val;

  scheme = alloc_colorscheme ();

  scheme->name = g_strdup ((gchar *) xmlGetProp (root, (xmlChar *) "name"));
  scheme->type = getColorSchemeType (xmlGetProp (root, (xmlChar *) "type"));
  scheme->system =
    getColorSchemeSystem (xmlGetProp (root, (xmlChar *) "system"));

/*
  scheme->system_min = 0.0;
  tmp = xmlGetProp(root, "system_min");
  if(tmp)
    scheme->system_min = (gfloat) asNumber(tmp);
  scheme->system_max = 1.0;
  tmp = xmlGetProp(root, "system_max");
  if(tmp)
    scheme->system_max = (gfloat) asNumber(tmp);
*/

  tmp = xmlGetProp (root, (xmlChar *) "criticalvalue");
  if (tmp)
    scheme->criticalvalue = (gint) asNumber ((char *) tmp);

  tmp = xmlGetProp (root, (xmlChar *) "ncolors");
  if (tmp)
    scheme->n = (gint) asNumber ((char *) tmp);

  node = getXMLElement (root, "description");
  val = xmlNodeListGetString (doc, XML_CHILDREN (node), 1);
  scheme->description = g_strdup (g_strstrip ((gchar *) val));
  g_free (val);

  node = getXMLElement (root, "foreground");
  getForegroundColors (node, doc, scheme);

  node = getXMLElement (root, "background");
  if (node)
    node = getXMLElement (node, "color");
  getBackgroundColor (node, doc, scheme);

  node = getXMLElement (root, "annotations");
  if (node)
    node = getXMLElement (node, "color");
  getAnnotationColor (node, doc, scheme);

  return (scheme);
}

colorscaletype
getColorSchemeType (const xmlChar * type)
{
  if (strcmp ((char *) type, "diverging") == 0)
    return (diverging);
  else if (strcmp ((char *) type, "sequential") == 0)
    return (sequential);
  else if (strcmp ((char *) type, "spectral") == 0)
    return (spectral);
  else if (strcmp ((char *) type, "qualitative") == 0)
    return (qualitative);
  else
    return (UNKNOWN_COLOR_TYPE);
}

colorsystem
getColorSchemeSystem (const xmlChar * type)
{
  if (strcmp ((char *) type, "rgb") == 0)
    return (rgb);
  else if (strcmp ((char *) type, "hsv") == 0)
    return (hsv);
  else if (strcmp ((char *) type, "cmy") == 0)
    return (cmy);
  else if (strcmp ((char *) type, "cmyk") == 0)
    return (cmyk);
  else
    return (UNKNOWN_COLOR_SYSTEM);
}

/**
  Read the foreground colors node, processing each of the 
  colors using getForegroundColor().
 */
void
getForegroundColors (xmlNodePtr node, xmlDocPtr doc, colorschemed * scheme)
{
  gint n = 0;
  xmlNodePtr tmp;
  tmp = XML_CHILDREN (node);
  while (tmp) {
    if (tmp->type != XML_TEXT_NODE)
      n++;
    tmp = tmp->next;
  }

  scheme->n = n;
  scheme->data = (gfloat **) g_malloc (n * sizeof (gfloat *));
  scheme->rgb = (GdkColor *) g_malloc (n * sizeof (GdkColor));

  tmp = XML_CHILDREN (node);
  n = 0;
  while (tmp) {
    if (tmp->type != XML_TEXT_NODE) {
      getForegroundColor (n, tmp, doc, scheme);
      n++;
    }
    tmp = tmp->next;
  }
}


gint
getForegroundColor (gint index, xmlNodePtr node, xmlDocPtr doc,
                    colorschemed * scheme)
{
  gint value;
  gchar *name;
  xmlChar *ptr;
  value = getColor (node, doc, &(scheme->data[index]), &scheme->rgb[index]);

  ptr = xmlGetProp (node, (xmlChar *) "name");
  {
    gchar *tmp;
    tmp = name = (gchar *) g_malloc (sizeof (gchar) * (xmlStrlen (ptr) + 1));
    while (ptr[0]) {
      *tmp++ = *ptr++;
    }
    tmp[0] = '\0';
  }

  g_array_append_val (scheme->colorNames, name);

  return (value);
}

gint
getBackgroundColor (xmlNodePtr node, xmlDocPtr doc, colorschemed * scheme)
{
  return (getColor (node, doc, &scheme->bg, &scheme->rgb_bg));
}

gint
getAnnotationColor (xmlNodePtr node, xmlDocPtr doc, colorschemed * scheme)
{
  return (getColor (node, doc, &scheme->accent, &scheme->rgb_accent));
}

/**
 Read a color of the form
  <color><element></element><element></element>....<element></element></color>
 Puts the actual values into original and fills in the RGB settings for `col'.
 */
gint
getColor (xmlNodePtr node, xmlDocPtr doc, gfloat ** original, GdkColor * col)
{
  xmlNodePtr tmp;
  gint i = 0, numElements = 3;  /* RGB only at present. */
  gfloat *vals;
  gfloat colorsystem_min = 0.0;
  gfloat colorsystem_max = 1.0;
  gfloat max = 65535;

  /*-- color values must be scaled onto [0,65535] --*/
  gchar *tmpVal;
  tmpVal = (gchar *) xmlGetProp (node, (xmlChar *) "min");
  if (tmpVal) {
    colorsystem_min /= asNumber (tmpVal);
  }
  tmpVal = (gchar *) xmlGetProp (node, (xmlChar *) "max");
  if (tmpVal) {
    colorsystem_max /= asNumber (tmpVal);
  }

  tmp = XML_CHILDREN (node);

  vals = (gfloat *) g_malloc (3 * sizeof (gfloat));
  while (tmp) {
    xmlChar *val;
    if (tmp->type != XML_TEXT_NODE) {
      val = xmlNodeListGetString (doc, XML_CHILDREN (tmp), 1);
      vals[i] = asNumber ((char *) val);
      g_free (val);
      i++;
    }
    tmp = tmp->next;
  }
  if (original)
    *original = vals;

  /*-- scale onto [0,1] --*/
  for (i = 0; i < 3; i++)
    vals[i] =
      (vals[i] - colorsystem_min) / (colorsystem_max - colorsystem_min);

  /*-- scale onto [0,65535] --*/
  col->red = (guint16) (max * vals[0]);
  col->green = (guint16) (max * vals[1]);
  col->blue = (guint16) (max * vals[2]);

  return (numElements);
}