home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / xp / xp_rgb.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  8.1 KB  |  271 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* *
  20.  * 
  21.  *
  22.  * xp_rgb.c --- parsing color names to RGB triplets.
  23.  *
  24.  * Created: John Giannandrea <jg@netscape.com>, 28-Sep-95
  25.  */
  26.  
  27. #ifdef GROSS_DEBUG
  28. #include <stdio.h>
  29. #include <string.h>
  30. typedef unsigned char uint8;
  31. typedef int int32;
  32. typedef int intn;
  33. #define XP_STRCASECMP strcasecmp
  34. #else
  35. #include "xp_rgb.h"
  36. #endif
  37.  
  38.  
  39. static struct {
  40.     uint8 r;
  41.     uint8 g;
  42.     uint8 b;
  43.     char *name;
  44. } xp_rgb_table[] = {
  45.         {240, 248, 255,  "aliceblue" },
  46.         {250, 235, 215,  "antiquewhite" },
  47.         {  0, 255, 255,  "aqua" },
  48.         {127, 255, 212,  "aquamarine" },
  49.         {240, 255, 255,  "azure" },
  50.         {245, 245, 220,  "beige" },
  51.         {255, 228, 196,  "bisque" },
  52.         {  0,   0,   0,  "black" },
  53.         {255, 235, 205,  "blanchedalmond" },
  54.         {  0,   0, 255,  "blue" },
  55.         {138,  43, 226,  "blueviolet" },
  56.         {165,  42,  42,  "brown" },
  57.         {222, 184, 135,  "burlywood" },
  58.         { 95, 158, 160,  "cadetblue" },
  59.         {127, 255,   0,  "chartreuse" },
  60.         {210, 105,  30,  "chocolate" },
  61.         {255, 127,  80,  "coral" },
  62.         {100, 149, 237,  "cornflowerblue" },
  63.         {255, 248, 220,  "cornsilk" },
  64.         {220,  20,  60,  "crimson" },
  65.         {  0, 255, 255,  "cyan" },
  66.         {  0,   0, 139,  "darkblue" },
  67.         {  0, 139, 139,  "darkcyan" },
  68.         {184, 134,  11,  "darkgoldenrod" },
  69.         {169, 169, 169,  "darkgray" },
  70.         {  0, 100,   0,  "darkgreen" },
  71.         {189, 183, 107,  "darkkhaki" },
  72.         {139,   0, 139,  "darkmagenta" },
  73.         { 85, 107,  47,  "darkolivegreen" },
  74.         {255, 140,   0,  "darkorange" },
  75.         {153,  50, 204,  "darkorchid" },
  76.         {139,   0,   0,  "darkred" },
  77.         {233, 150, 122,  "darksalmon" },
  78.         {143, 188, 143,  "darkseagreen" },
  79.         { 72,  61, 139,  "darkslateblue" },
  80.         { 47,  79,  79,  "darkslategray" },
  81.         {  0, 206, 209,  "darkturquoise" },
  82.         {148,   0, 211,  "darkviolet" },
  83.         {255,  20, 147,  "deeppink" },
  84.         {  0, 191, 255,  "deepskyblue" },
  85.         {105, 105, 105,  "dimgray" },
  86.         { 30, 144, 255,  "dodgerblue" },
  87.         {178,  34,  34,  "firebrick" },
  88.         {255, 250, 240,  "floralwhite" },
  89.         { 34, 139,  34,  "forestgreen" },
  90.         {255,   0, 255,  "fuchsia" },
  91.         {220, 220, 220,  "gainsboro" },
  92.         {248, 248, 255,  "ghostwhite" },
  93.         {255, 215,   0,  "gold" },
  94.         {218, 165,  32,  "goldenrod" },
  95.         {128, 128, 128,  "gray" },
  96.         {  0, 128,   0,  "green" },
  97.         {173, 255,  47,  "greenyellow" },
  98.         {240, 255, 240,  "honeydew" },
  99.         {255, 105, 180,  "hotpink" },
  100.         {205,  92,  92,  "indianred" },
  101.         { 75,   0, 130,  "indigo" },
  102.         {255, 255, 240,  "ivory" },
  103.         {240, 230, 140,  "khaki" },
  104.         {230, 230, 250,  "lavender" },
  105.         {255, 240, 245,  "lavenderblush" },
  106.         {124, 252,   0,  "lawngreen" },
  107.         {255, 250, 205,  "lemonchiffon" },
  108.         {173, 216, 230,  "lightblue" },
  109.         {240, 128, 128,  "lightcoral" },
  110.         {224, 255, 255,  "lightcyan" },
  111.         {250, 250, 210,  "lightgoldenrodyellow" },
  112.         {144, 238, 144,  "lightgreen" },
  113.         {211, 211, 211,  "lightgrey" },
  114.         {255, 182, 193,  "lightpink" },
  115.         {255, 160, 122,  "lightsalmon" },
  116.         { 32, 178, 170,  "lightseagreen" },
  117.         {135, 206, 250,  "lightskyblue" },
  118.         {119, 136, 153,  "lightslategray" },
  119.         {176, 196, 222,  "lightsteelblue" },
  120.         {255, 255, 224,  "lightyellow" },
  121.         {  0, 255,   0,  "lime" },
  122.         { 50, 205,  50,  "limegreen" },
  123.         {250, 240, 230,  "linen" },
  124.         {255,   0, 255,  "magenta" },
  125.         {128,   0,   0,  "maroon" },
  126.         {102, 205, 170,  "mediumaquamarine" },
  127.         {  0,   0, 205,  "mediumblue" },
  128.         {186,  85, 211,  "mediumorchid" },
  129.         {147, 112, 219,  "mediumpurple" },
  130.         { 60, 179, 113,  "mediumseagreen" },
  131.         {123, 104, 238,  "mediumslateblue" },
  132.         {  0, 250, 154,  "mediumspringgreen" },
  133.         { 72, 209, 204,  "mediumturquoise" },
  134.         {199,  21, 133,  "mediumvioletred" },
  135.         { 25,  25, 112,  "midnightblue" },
  136.         {245, 255, 250,  "mintcream" },
  137.         {255, 228, 225,  "mistyrose" },
  138.         {255, 228, 181,  "moccasin" },
  139.         {255, 222, 173,  "navajowhite" },
  140.         {  0,   0, 128,  "navy" },
  141.         {253, 245, 230,  "oldlace" },
  142.         {128, 128,   0,  "olive" },
  143.         {107, 142,  35,  "olivedrab" },
  144.         {255, 165,   0,  "orange" },
  145.         {255,  69,   0,  "orangered" },
  146.         {218, 112, 214,  "orchid" },
  147.         {238, 232, 170,  "palegoldenrod" },
  148.         {152, 251, 152,  "palegreen" },
  149.         {175, 238, 238,  "paleturquoise" },
  150.         {219, 112, 147,  "palevioletred" },
  151.         {255, 239, 213,  "papayawhip" },
  152.         {255, 218, 185,  "peachpuff" },
  153.         {205, 133,  63,  "peru" },
  154.         {255, 192, 203,  "pink" },
  155.         {221, 160, 221,  "plum" },
  156.         {176, 224, 230,  "powderblue" },
  157.         {128,   0, 128,  "purple" },
  158.         {255,   0,   0,  "red" },
  159.         {188, 143, 143,  "rosybrown" },
  160.         { 65, 105, 225,  "royalblue" },
  161.         {139,  69,  19,  "saddlebrown" },
  162.         {250, 128, 114,  "salmon" },
  163.         {244, 164,  96,  "sandybrown" },
  164.         { 46, 139,  87,  "seagreen" },
  165.         {255, 245, 238,  "seashell" },
  166.         {160,  82,  45,  "sienna" },
  167.         {192, 192, 192,  "silver" },
  168.         {135, 206, 235,  "skyblue" },
  169.         {106,  90, 205,  "slateblue" },
  170.         {112, 128, 144,  "slategray" },
  171.         {255, 250, 250,  "snow" },
  172.         {  0, 255, 127,  "springgreen" },
  173.         { 70, 130, 180,  "steelblue" },
  174.         {210, 180, 140,  "tan" },
  175.         {  0, 128, 128,  "teal" },
  176.         {216, 191, 216,  "thistle" },
  177.         {255,  99,  71,  "tomato" },
  178.         { 64, 224, 208,  "turquoise" },
  179.         {238, 130, 238,  "violet" },
  180.         {245, 222, 179,  "wheat" },
  181.         {255, 255, 255,  "white" },
  182.         {245, 245, 245,  "whitesmoke" },
  183.         {255, 255,   0,  "yellow" },
  184.         {154, 205,  50,  "yellowgreen" },
  185. };
  186.  
  187. #define TABLESIZE ((sizeof xp_rgb_table) / (sizeof xp_rgb_table[0]))
  188.  
  189. intn XP_ColorNameToRGB(char *name, uint8 *r, uint8 *g, uint8 *b)
  190. {
  191.     int comp, low, mid, high;
  192.  
  193.     if (name == NULL)
  194.     {
  195.         return 1;
  196.     }
  197.  
  198.     low = 0;
  199.     high = TABLESIZE - 1;
  200.     /*
  201.     ** Binary search, starting at zero, which is slightly less
  202.     ** efficient, but makes the exit condition simple.
  203.     */
  204.     mid = 0;
  205.     for (;;)
  206.     {
  207.         if(!(comp = XP_STRCASECMP(name, xp_rgb_table[mid].name)))
  208.         {
  209. #ifdef GROSS_DEBUG
  210.             if (mid < 0 || mid >= TABLESIZE) {
  211.               printf("Found %s out of range\n", name);
  212.               return 1;
  213.             }
  214. #endif
  215.             *r = xp_rgb_table[mid].r;
  216.             *g = xp_rgb_table[mid].g;
  217.             *b = xp_rgb_table[mid].b;
  218.             return 0;
  219.         }
  220.         if ((high - low) <= 1)
  221.           return 1;
  222.         if(comp>0)
  223.             low = mid;
  224.         else
  225.             high = mid;
  226.         mid = low + (high-low+1)/2;
  227.     }
  228.     /* NOTREACHED */
  229.     return 1;
  230. }
  231.  
  232. #ifdef GROSS_DEBUG
  233. /*
  234. ** This is stupid, but this is the second release where the non-function of
  235. ** a simple binary search has turned out to be a bug, and I am tired of it.
  236. ** Compile and run this file with -DGROSS_DEBUG, if it generates no output then
  237. ** the routine is probably ok.
  238. **
  239. ** The truly paranoid should re-run this test any time they change the
  240. ** xp_rgb_table array.
  241. **/
  242. main()
  243. {
  244.   uint8 r, g, b;
  245.   int i;
  246.   for (i = 0; i < TABLESIZE; i++) {
  247.     char color[200];
  248.     if (XP_ColorNameToRGB(xp_rgb_table[i].name, &r, &g, &b) != 0)
  249.       printf("Couldn't find %s @ %d of %d\n", xp_rgb_table[i].name, i, TABLESIZE);
  250.     /*
  251.     ** test to see if we loop infintely between the cracks
  252.     */
  253.     strcpy(color, xp_rgb_table[i].name);
  254.     strcat(color, "Z");
  255.     if (XP_ColorNameToRGB(color, &r, &g, &b) == 0)
  256.       printf("Souldn't't find %s @ %d of %d\n", color, i, TABLESIZE);
  257.     strcpy(color, xp_rgb_table[i].name);
  258.     color[strlen(color)-1]--;
  259.     if (XP_ColorNameToRGB(color, &r, &g, &b) == 0)
  260.       printf("Souldn't't find %s @ %d of %d\n", color, i, TABLESIZE);
  261.   }
  262.   /*
  263.   ** test the beginning on the end
  264.   */
  265.   if (XP_ColorNameToRGB("\001", &r, &g, &b) == 0)
  266.     printf("Mistakenly found early string\n");
  267.   if (XP_ColorNameToRGB("\177", &r, &g, &b) == 0)
  268.     printf("Mistakenly found late string\n");
  269. }
  270. #endif
  271.