PageRenderTime 38ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/dx-4.4.4/src/exec/libdx/lbcolor.c

#
C | 311 lines | 244 code | 35 blank | 32 comment | 47 complexity | 9696bb90a3d365dcb11f926353b93496 MD5 | raw file
Possible License(s): Unlicense, IPL-1.0
  1. /***********************************************************************/
  2. /* Open Visualization Data Explorer */
  3. /* (C) Copyright IBM Corp. 1989,1999 */
  4. /* ALL RIGHTS RESERVED */
  5. /* This code licensed under the */
  6. /* "IBM PUBLIC LICENSE - Open Visualization Data Explorer" */
  7. /***********************************************************************/
  8. #include <dxconfig.h>
  9. #include <math.h>
  10. #include <stdio.h>
  11. #include <ctype.h>
  12. #include <string.h>
  13. #include <stdlib.h>
  14. #include <dx/dx.h>
  15. struct colortable {
  16. char colorname[80];
  17. RGBColor colorvalue;
  18. };
  19. static struct colortable ct[] = {
  20. { "aquamarine", {0.4392157, 0.8588235, 0.5764706} },
  21. { "mediumaquamarine", {0.1960784, 0.8000000, 0.6000000} },
  22. { "black", {0.0000000, 0.0000000, 0.0000000} },
  23. { "blue", {0.0000000, 0.0000000, 1.0000000} },
  24. { "cadetblue", {0.3725490, 0.6235294, 0.6235294} },
  25. { "cornflowerblue", {0.2588235, 0.2588235, 0.4352941} },
  26. { "darkslateblue", {0.4196078, 0.1372549, 0.5568628} },
  27. { "lightblue", {0.7490196, 0.8470588, 0.8470588} },
  28. { "lightsteelblue", {0.5607843, 0.5607843, 0.7372549} },
  29. { "mediumblue", {0.1960784, 0.1960784, 0.8000000} },
  30. { "mediumslateblue", {0.4980392, 0.0000000, 1.0000000} },
  31. { "midnightblue", {0.1843137, 0.1843137, 0.3098039} },
  32. { "navyblue", {0.1372549, 0.1372549, 0.5568628} },
  33. { "navy", {0.1372549, 0.1372549, 0.5568628} },
  34. { "skyblue", {0.1960784, 0.6000000, 0.8000000} },
  35. { "slateblue", {0.0000000, 0.4980392, 1.0000000} },
  36. { "steelblue", {0.1372549, 0.4196078, 0.5568628} },
  37. { "coral", {1.0000000, 0.4980392, 0.0000000} },
  38. { "cyan", {0.0000000, 1.0000000, 1.0000000} },
  39. { "firebrick", {0.5568628, 0.1372549, 0.1372549} },
  40. { "brown", {0.6470588, 0.1647059, 0.1647059} },
  41. { "gold", {0.8000000, 0.4980392, 0.1960784} },
  42. { "goldenrod", {0.8588235, 0.8588235, 0.4392157} },
  43. { "mediumgoldenrod", {0.9176471, 0.9176471, 0.6784314} },
  44. { "green", {0.0000000, 1.0000000, 0.0000000} },
  45. { "darkgreen", {0.1843137, 0.3098039, 0.1843137} },
  46. { "darkolivegreen", {0.3098039, 0.3098039, 0.1843137} },
  47. { "forestgreen", {0.1372549, 0.5568628, 0.1372549} },
  48. { "limegreen", {0.1960784, 0.8000000, 0.1960784} },
  49. { "mediumforestgreen", {0.4196078, 0.5568628, 0.1372549} },
  50. { "mediumseagreen", {0.2588235, 0.4352941, 0.2588235} },
  51. { "mediumspringgreen", {0.4980392, 1.0000000, 0.0000000} },
  52. { "palegreen", {0.5607843, 0.7372549, 0.5607843} },
  53. { "seagreen", {0.1372549, 0.5568628, 0.4196078} },
  54. { "springgreen", {0.0000000, 1.0000000, 0.4980392} },
  55. { "yellowgreen", {0.6000000, 0.8000000, 0.1960784} },
  56. { "darkslategrey", {0.1843137, 0.3098039, 0.3098039} },
  57. { "darkslategray", {0.1843137, 0.3098039, 0.3098039} },
  58. { "dimgrey", {0.3294118, 0.3294118, 0.3294118} },
  59. { "dimgray", {0.3294118, 0.3294118, 0.3294118} },
  60. { "gray", {0.7529412, 0.7529412, 0.7529412} },
  61. { "grey", {0.7529412, 0.7529412, 0.7529412} },
  62. { "lightgrey", {0.8274510, 0.8274510, 0.8274510} },
  63. { "lightgray", {0.8274510, 0.8274510, 0.8274510} },
  64. { "khaki", {0.6235294, 0.6235294, 0.3725490} },
  65. { "magenta", {1.0000000, 0.0000000, 1.0000000} },
  66. { "maroon", {0.5568628, 0.1372549, 0.4196078} },
  67. { "orange", {0.8000000, 0.1960784, 0.1960784} },
  68. { "orchid", {0.8588235, 0.4392157, 0.8588235} },
  69. { "darkorchid", {0.6000000, 0.1960784, 0.8000000} },
  70. { "mediumorchid", {0.5764706, 0.4392157, 0.8588235} },
  71. { "pink", {0.7372549, 0.5607843, 0.5607843} },
  72. { "plum", {0.9176471, 0.6784314, 0.9176471} },
  73. { "red", {1.0000000, 0.0000000, 0.0000000} },
  74. { "indianred", {0.3098039, 0.1843137, 0.1843137} },
  75. { "mediumvioletred", {0.8588235, 0.4392157, 0.5764706} },
  76. { "orangered", {1.0000000, 0.0000000, 0.4980392} },
  77. { "violetred", {0.8000000, 0.1960784, 0.6000000} },
  78. { "salmon", {0.4352941, 0.2588235, 0.2588235} },
  79. { "sienna", {0.5568628, 0.4196078, 0.1372549} },
  80. { "tan", {0.8588235, 0.5764706, 0.4392157} },
  81. { "thistle", {0.8470588, 0.7490196, 0.8470588} },
  82. { "turquoise", {0.6784314, 0.9176471, 0.9176471} },
  83. { "darkturquoise", {0.4392157, 0.5764706, 0.8588235} },
  84. { "mediumturquoise", {0.4392157, 0.8588235, 0.8588235} },
  85. { "violet", {0.3098039, 0.1843137, 0.3098039} },
  86. { "blueviolet", {0.6235294, 0.3725490, 0.6235294} },
  87. { "wheat", {0.8470588, 0.8470588, 0.7490196} },
  88. { "white", {1.0000000, 1.0000000, 1.0000000} },
  89. { "yellow", {1.0000000, 1.0000000, 0.0000000} },
  90. { "greenyellow", {0.5764706, 0.8588235, 0.4392157} },
  91. { "", {0.0, 0.0, 0.0} },
  92. };
  93. extern Error DXColorNameToRGB(char *, RGBColor *);
  94. static Error FreeColorTable(Pointer);
  95. static int GetNextStringRedGreenBlue(FILE *, char *,
  96. float *, float *, float *);
  97. Error DXColorNameToRGB(char *colorstr, RGBColor *colorvec)
  98. {
  99. struct colortable *ctread=NULL, *cp;
  100. int count, i, numread, matchedcolor;
  101. float r, g, b;
  102. char newstring[30], laststring[80];
  103. char compactstring[80];
  104. char *rootstring, colorfile[100];
  105. Private cachedtable;
  106. FILE *in;
  107. /* get color name */
  108. /* convert color name to all lower case and remove spaces */
  109. count = 0;
  110. i = 0;
  111. while ( i<29 && colorstr[i] != '\0') {
  112. if (isalpha(colorstr[i])) {
  113. if (isupper(colorstr[i]))
  114. newstring[count] = tolower(colorstr[i]);
  115. else
  116. newstring[count] = colorstr[i];
  117. count++;
  118. }
  119. else {
  120. if (colorstr[i] != ' ') {
  121. newstring[count] = colorstr[i];
  122. count++;
  123. }
  124. }
  125. i++;
  126. }
  127. newstring[count]='\0';
  128. matchedcolor = 0;
  129. cachedtable = (Private)DXGetCacheEntry("rgbcolortable", 0, 0);
  130. if (!cachedtable) {
  131. /* better make the table */
  132. /* first check the environment variable DXCOLORS */
  133. rootstring = (char *)getenv("DXCOLORS");
  134. if (rootstring) {
  135. sprintf(colorfile,"%s",rootstring);
  136. in = fopen(colorfile, "r");
  137. if (in) goto got_colorfile;
  138. }
  139. /* if we haven't succeeded yet, try DXEXECROOT */
  140. rootstring = (char *)getenv("DXEXECROOT");
  141. if (rootstring) {
  142. sprintf(colorfile,"%s/lib/colors.txt",rootstring);
  143. in = fopen(colorfile, "r");
  144. if (in) goto got_colorfile;
  145. }
  146. /* if we haven't succeeded yet, try DXROOT */
  147. rootstring = (char *)getenv("DXROOT");
  148. if (rootstring) {
  149. sprintf(colorfile,"%s/lib/colors.txt",rootstring);
  150. in = fopen(colorfile, "r");
  151. if (in) goto got_colorfile;
  152. }
  153. /* if we still haven't succeeded, try the system version */
  154. in = fopen("/usr/local/dx/lib/colors.txt","r");
  155. if (in) goto got_colorfile;
  156. /* all has failed; use our old table */
  157. ctread = ct;
  158. got_colorfile:
  159. if (in) {
  160. /* read the system table */
  161. numread = 0;
  162. strcpy(laststring,"");
  163. while (GetNextStringRedGreenBlue(in, compactstring, &r, &g, &b)) {
  164. if (strcmp(compactstring,laststring)) {
  165. numread++;
  166. strcpy(laststring, compactstring);
  167. }
  168. }
  169. /* the table MUST BE IN GLOBAL MEMORY FOR THE CACHE TO WORK */
  170. ctread = (struct colortable *)DXAllocate
  171. ((numread+1) * sizeof(struct colortable));
  172. if (!ctread)
  173. goto error;
  174. cp = ctread;
  175. /* rewind the file */
  176. fseek(in,0,0);
  177. strcpy(laststring,"");
  178. while (GetNextStringRedGreenBlue(in, compactstring, &r, &g, &b)) {
  179. if (strcmp(compactstring,laststring)) {
  180. strcpy(cp->colorname, compactstring);
  181. cp->colorvalue = DXRGB(r,g,b);
  182. strcpy(laststring, compactstring);
  183. cp++;
  184. }
  185. }
  186. /* need to set the last "colorname" to a null string */
  187. strcpy(cp->colorname,"");
  188. cp->colorvalue = DXRGB(0.0, 0.0, 0.0);
  189. fclose(in);
  190. }
  191. }
  192. else {
  193. /* we found the table in the cache*/
  194. ctread = (struct colortable *)DXGetPrivateData(cachedtable);
  195. if (!ctread)
  196. goto error;
  197. }
  198. /* run through the color table to find the rgb value */
  199. for (cp = ctread; strcmp(cp->colorname,""); cp++) {
  200. if (strcmp(cp->colorname,newstring))
  201. continue;
  202. else {
  203. *colorvec = cp->colorvalue ;
  204. matchedcolor = 1;
  205. continue;
  206. }
  207. }
  208. /* put the thing in the cache */
  209. if (!cachedtable) {
  210. cachedtable = DXNewPrivate((Pointer)ctread, FreeColorTable);
  211. DXReference((Object)cachedtable);
  212. if (!DXSetCacheEntry((Object)cachedtable, CACHE_PERMANENT, "rgbcolortable", 0, 0))
  213. goto error;
  214. DXDelete((Object)cachedtable);
  215. }
  216. else {
  217. /* delete the table since it has an extra reference count XXX */
  218. DXDelete((Object)cachedtable);
  219. }
  220. if (!matchedcolor) {
  221. DXSetError(ERROR_BAD_PARAMETER, "#11760", newstring);
  222. return ERROR;
  223. }
  224. return OK;
  225. error:
  226. /* delete the table since it has an extra reference count XXX */
  227. DXDelete((Object)cachedtable);
  228. return ERROR;
  229. }
  230. static Error FreeColorTable(Pointer ptr)
  231. {
  232. struct colortable *ctp = ptr;
  233. /* do not free the pointer if we used the static color table ct[] */
  234. if (ctp != ct)
  235. DXFree((Pointer)ctp);
  236. return OK;
  237. }
  238. static int GetNextStringRedGreenBlue(FILE *in, char *compactstring, float *r,
  239. float *g, float *b)
  240. {
  241. char string[80];
  242. int i, j;
  243. /* read the colorname (at least the first word of it) */
  244. if (fscanf(in,"%s", string) != EOF) {
  245. j=0;
  246. more_string:
  247. i=0;
  248. while (i < 80 && string[i] != '\0') {
  249. if (string[i] != ' ') {
  250. compactstring[j] = tolower(string[i]);
  251. j++;
  252. }
  253. i++;
  254. }
  255. /* now read the next token. Might be more color name, or
  256. might be red color */
  257. if(fscanf(in,"%s", string) != EOF) {
  258. if ((isdigit(string[0]))||(string[0] == '.')) {
  259. /* it's red */
  260. *r = atof(string);
  261. if(fscanf(in,"%f %f \n", g, b) != 2) {
  262. return 0;
  263. }
  264. }
  265. else {
  266. /* it's more color name */
  267. goto more_string;
  268. }
  269. }
  270. /* null terminate */
  271. compactstring[j] = '\0';
  272. return 1;
  273. }
  274. /* EOF */
  275. return 0;
  276. }