src/colors.c cleanup

123 <[email protected]>
Newsgroups gmane.comp.web.dillo.devel
Message-ID <[email protected]>
- Fixed indentation.
- Added LEN macro.
- Reimplemented binary search using bsearch(3).
- Changed NOTE: colors can be in any case, they are compared with dStrAsciiCasecmp.

_______________________________________________
Dillo-dev mailing list
[email protected]
http://lists.auriga.wearlab.de/cgi-bin/mailman/listinfo/dillo-dev
colors.diff (text/plain, 6.9 KB)
diff -r e77b384a9d4d src/colors.c
--- a/src/colors.c	Sat May 26 13:58:42 2012 +0200
+++ b/src/colors.c	Sat May 26 17:36:54 2012 +0400
@@ -9,24 +9,24 @@
  * (at your option) any later version.
  */
 
+#include <ctype.h>
+#include <stdlib.h>
 #include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
+
 #include "colors.h"
-
 #include "msg.h"
 
 /*
  * If EXTENDED_COLOR is defined, the extended set of named colors is supported.
  * These colors're not standard but they're supported in most browsers.
- * NOTE: The colors MUST be in alphabetical order and lower case because the
- *       code uses a binary search.
+ * NOTE: The colors MUST be in alphabetical order because the code uses a
+ * binary search.
  */
 
 #define EXTENDED_COLOR
 
 static const struct key {
-   char *key;
+   const char *key;
    int32_t val;
 } color_keyword [] = {
 #ifdef EXTENDED_COLOR
@@ -44,7 +44,7 @@
 #ifdef EXTENDED_COLOR
    { "blanchedalmond", 0xffebcd},
 #endif
-   {"blue", 0x0000ff},
+   { "blue", 0x0000ff},
 #ifdef EXTENDED_COLOR
    { "blueviolet", 0x8a2be2},
    { "brown", 0xa52a2a},
@@ -201,7 +201,12 @@
 #endif
 };
 
-#define NCOLORS   (sizeof(color_keyword) / sizeof(color_keyword[0]))
+#define LEN(x) (sizeof(x)/sizeof((x)[0]))
+
+/* Helper function for bsearch. */
+static int Color_cmp(const void *a, const void *b) {
+   return dStrAsciiCasecmp(((struct key *)a)->key, ((struct key *)b)->key);
+}
 
 /*
  * Parse a color in hex (RRGGBB) or (RGB)
@@ -212,22 +217,22 @@
  */
 static int32_t Color_parse_hex (const char *s, int32_t default_color, int *err)
 {
-  int32_t ret_color;
-  char *tail;
+   int32_t ret_color;
+   char *tail;
 
-  *err = 1;
-  ret_color = strtol(s, &tail, 16);
-  if (tail - s == 6)
-     *err = 0;
-  else if (tail - s == 3) {       /* #RGB as allowed by CSS */
-     *err = 0;
-         ret_color = ((ret_color & 0xf00) << 12) | ((ret_color & 0xf00) << 8) |
-                     ((ret_color & 0x0f0) << 8)  | ((ret_color & 0x0f0) << 4) |
-                     ((ret_color & 0x00f) << 4)  | ((ret_color & 0x00f) << 0);
-  } else
-     ret_color = default_color;
+   *err = 1;
+   ret_color = strtol(s, &tail, 16);
+   if (tail - s == 6)
+      *err = 0;
+   else if (tail - s == 3) { /* #RGB as allowed by CSS */
+      *err = 0;
+      ret_color = ((ret_color & 0xf00) << 12) | ((ret_color & 0xf00) << 8) |
+                  ((ret_color & 0x0f0) << 8)  | ((ret_color & 0x0f0) << 4) |
+                  ((ret_color & 0x00f) << 4)  | ((ret_color & 0x00f) << 0);
+   } else
+      ret_color = default_color;
 
-  return ret_color;
+   return ret_color;
 }
 
 /*
@@ -243,37 +248,27 @@
 {
    const char *cp;
    int32_t ret_color;
-   int ret, low, mid, high, st = 1;
+   int st = 1;
+   struct key key;
+   const struct key *color;
 
    /* skip leading spaces */
    for (cp = subtag; dIsspace(*cp); cp++);
 
    ret_color = default_color;
-   if (*cp == '#') {
+   if (*cp == '#')
       ret_color = Color_parse_hex(cp + 1, default_color, &st);
-
-   } else if (*cp == '0' && (cp[1] == 'x' || cp[1] == 'X') ) {
+   else if (*cp == '0' && (cp[1] == 'x' || cp[1] == 'X')) {
       ret_color = Color_parse_hex(cp + 2, default_color, &st);
       st = 2;
-
    } else {
-      /* Binary search */
-      low = 0;
-      high = NCOLORS - 1;
-      while (low <= high) {
-         mid = (low + high) / 2;
-         if ((ret = dStrAsciiCasecmp(cp, color_keyword[mid].key)) < 0)
-            high = mid - 1;
-         else if (ret > 0)
-            low = mid + 1;
-         else {
-            ret_color = color_keyword[mid].val;
-            st = 0;
-            break;
-         }
-      }
-
-      if (low > high) {
+      key.key = cp;
+      color = bsearch(&key, color_keyword, LEN(color_keyword),
+                      sizeof color_keyword[0], Color_cmp);
+      if (color) {
+         st = 0;
+         ret_color = color->val;
+      } else {
          /* try for RRGGBB lacking the leading '#' */
          ret_color = Color_parse_hex(cp, default_color, &st);
          st = 1;
@@ -325,46 +320,44 @@
  *   if candidate has good contrast with C_txt, C_lnk and C_bg  -> candidate
  *   else another color (from the internal list)
  */
-int32_t a_Color_vc(int32_t candidate,
-                   int32_t C_txt, int32_t C_lnk, int32_t C_bg)
+int32_t a_Color_vc(int32_t candidate, int32_t C_txt, int32_t C_lnk,
+                   int32_t C_bg)
 {
-                    /* candidate purple    darkcyan  darkmagenta olive   */
-  static int32_t v[] = {0x000000, 0x800080, 0x008b8b, 0x8b008b, 0x808000,
-                    /* darkred   coral     black                        */
-                       0x8b0000, 0xff7f50, 0x000000};
-  int v_size = sizeof(v) / sizeof(v[0]);
-  int i, max_i, score, max_score, d_bg, d_txt, d_lnk;
+                      /* candidate purple    darkcyan  darkmagenta olive */
+   static int32_t v[] = {0x000000, 0x800080, 0x008b8b, 0x8b008b, 0x808000,
+                      /* darkred   coral     black                       */
+                         0x8b0000, 0xff7f50, 0x000000};
+   int i, max_i, score, max_score, d_bg, d_txt, d_lnk, vsize = LEN(v);
 
+   /* set candidate in the list */
+   v[0] = candidate;
 
-  /* set candidate in the list */
-  v[0] = candidate;
-
-  /* Try to get good overall and individual contrast */
-  max_i = max_score = 0;
-  for (i = 0; i < v_size; ++i) {
-     _MSG("a_Color_vc: [%d]%.6x: %d %d %d\n", i, v[i],
+   /* Try to get good overall and individual contrast */
+   max_i = max_score = 0;
+   for (i = 0; i < vsize; ++i) {
+      _MSG("a_Color_vc: [%d]%.6x: %d %d %d\n", i, v[i],
          Color_distance2(C_txt, v[i]),
          Color_distance2(C_lnk, v[i]),
          Color_distance2(C_bg, v[i]));
 
-     /* Tuned with: slashdot.org, paulgraham.com, newsforge.com,
-      *             linuxjournal.com
-      */
-     d_txt = Color_distance2(C_txt, v[i]);
-     d_lnk = Color_distance2(C_lnk, v[i]);
-     d_bg  = Color_distance2(C_bg, v[i]);
-     score = (d_bg >= 2 ? 4 : 2 * d_bg) +
-             (d_txt + d_lnk >= 2 ? 2 : d_txt + d_lnk) +
-             (Color_distance3(C_lnk, v[i]) >= 1 ? 1 : 0);
-     if (score >= 7) {
-        /* enough distance, use this color */
-        max_i = i;
-        break;
-     } else if (score > max_score) {
-        /* keep track of the best candidate so far */
-        max_score = score;
-        max_i = i;
-     }
-  }
-  return v[max_i];
+      /* Tuned with: slashdot.org, paulgraham.com, newsforge.com,
+       * linuxjournal.com
+       */
+      d_txt = Color_distance2(C_txt, v[i]);
+      d_lnk = Color_distance2(C_lnk, v[i]);
+      d_bg  = Color_distance2(C_bg, v[i]);
+      score = (d_bg >= 2 ? 4 : 2 * d_bg) +
+              (d_txt + d_lnk >= 2 ? 2 : d_txt + d_lnk) +
+              (Color_distance3(C_lnk, v[i]) >= 1 ? 1 : 0);
+      if (score >= 7) {
+         /* enough distance, use this color */
+         max_i = i;
+         break;
+      } else if (score > max_score) {
+         /* keep track of the best candidate so far */
+         max_score = score;
+         max_i = i;
+      }
+   }
+   return v[max_i];
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.