<commit_before>/**
 * @file color.c
 * @author <NAME>
 * @date 2004-2006
 *
 * @brief Functions to handle loading colors.
 *
 */

#include "jwm.h"
#include "main.h"
#include "color.h"
#include "error.h"
#include "misc.h"

/** Mapping between color types and default values. */
typedef struct {
   ColorType type;
   unsigned int value;
} DefaultColorNode;

/** Mapping from color type to the value it from which it inherits. */
typedef struct {
   ColorType dest;
   ColorType src;
} ColorInheritNode;

unsigned long colors[COLOR_COUNT];
static unsigned long rgbColors[COLOR_COUNT];

/* Map a linear 8-bit RGB space to pixel values. */
static unsigned long *map;

/* Map 8-bit pixel values to a 24-bit linear RGB space. */
static unsigned long *rmap;

/* Number of JWM-specific colors allocated. */
static const unsigned BASE_COLORS = 64;
static const unsigned MAX_COLORS = 128;
static unsigned extraColors;

#ifdef USE_XFT
static XftColor *xftColors[COLOR_COUNT] = { NULL };
#endif

static const DefaultColorNode DEFAULT_COLORS[] = {

   { COLOR_TITLE_FG,                0xFFFFFF    },
   { COLOR_TITLE_ACTIVE_FG,         0xFFFFFF    },

   { COLOR_TITLE_BG1,               0x555555    },
   { COLOR_TITLE_BG2,               0x555555    },
   { COLOR_TITLE_ACTIVE_BG1,        0x0077CC    },
   { COLOR_TITLE_ACTIVE_BG2,        0x0077CC    },

   { COLOR_BORDER_LINE,             0x000000    },
   { COLOR_BORDER_ACTIVE_LINE,      0x000000    },

   { COLOR_TRAY_FG,                 0x000000    },
   { COLOR_TRAY_BG1,                0x999999    },
   { COLOR_TRAY_BG2,                0x999999    },
   { COLOR_TRAY_ACTIVE_FG,          0xFFFFFF    },
   { COLOR_TRAY_ACTIVE_BG1,         0x555555    },
   { COLOR_TRAY_ACTIVE_BG2,         0x555555    },

   { COLOR_PAGER_BG,                0x999999    },
   { COLOR_PAGER_FG,                0x333333    },
   { COLOR_PAGER_ACTIVE_BG,         0x555555    },
   { COLOR_PAGER_ACTIVE_FG,         0x0077CC    },
   { COLOR_PAGER_OUTLINE,           0x000000    },
   { COLOR_PAGER_TEXT,              0x000000    },

   { COLOR_MENU_BG,                 0x999999    },
   { COLOR_MENU_FG,                 0x000000    },
   { COLOR_MENU_ACTIVE_BG1,         0x0077CC    },
   { COLOR_MENU_ACTIVE_BG2,         0x0077CC    },
   { COLOR_MENU_ACTIVE_FG,          0x000000    },

   { COLOR_POPUP_BG,                0x999999    },
   { COLOR_POPUP_FG,                0x000000    }

};
static const unsigned DEFAULT_COUNT = ARRAY_LENGTH(DEFAULT_COLORS);

static struct {
   ColorType base;
   ColorType up;
   ColorType down;
} DERIVED_COLORS[] = {
   { COLOR_TITLE_BG1,        COLOR_TITLE_UP,        COLOR_TITLE_DOWN        },
   { COLOR_TITLE_ACTIVE_BG1, COLOR_TITLE_ACTIVE_UP, COLOR_TITLE_ACTIVE_DOWN },
   { COLOR_TRAY_BG1,         COLOR_TRAY_UP,         COLOR_TRAY_DOWN         },
   { COLOR_TRAY_ACTIVE_BG1,  COLOR_TRAY_ACTIVE_UP,  COLOR_TRAY_ACTIVE_DOWN  },
   { COLOR_MENU_BG,          COLOR_MENU_UP,         COLOR_MENU_DOWN         },
   { COLOR_MENU_ACTIVE_BG1,  COLOR_MENU_ACTIVE_UP,  COLOR_MENU_ACTIVE_DOWN  }
};
static const unsigned DERIVED_COUNT = ARRAY_LENGTH(DERIVED_COLORS);

static char **names = NULL;

static unsigned long redShift;
static unsigned long greenShift;
static unsigned long blueShift;
static unsigned long redMask;
static unsigned long greenMask;
static unsigned long blueMask;

static void ComputeShiftMask(unsigned long maskIn,
                             unsigned long *shiftOut,
                             unsigned long *maskOut);

static void GetDirectPixel(XColor *c);
static void GetMappedPixel(XColor *c, char alloc);

static void SetDefaultColor(ColorType type); 

static unsigned long ReadHex(const char *hex);

static unsigned long GetRGBFromXColor(const XColor *c);

static int GetColorByName(const char *str, XColor *c);
static void InitializeNames(void);

static XColor GetXColorFromRGB(unsigned long rgb);
static void LightenColor(ColorType oldColor, ColorType newColor);
static void DarkenColor(ColorType oldColor, ColorType newColor);

/** Startup color support. */
void StartupColors(void)
{

   unsigned int x;
   int red, green, blue;
   XColor c;

   /* Determine how to convert between RGB triples and pixels. */
   switch(rootVisual.visual->class) {
   case DirectColor:
   case TrueColor:
      ComputeShiftMask(rootVisual.visual->red_mask, &redShift, &redMask);
      ComputeShiftMask(rootVisual.visual->green_mask, &greenShift, &greenMask);
      ComputeShiftMask(rootVisual.visual->blue_mask, &blueShift, &blueMask);
      map = NULL;
      break;
   default:

      /* Attempt to get 64 colors, pretend it worked. */
      /* RGB: 2, 2, 2 */
      extraColors = 0;
      redMask = 0x30;
      greenMask = 0x0C;
      blueMask = 0x03;
      ComputeShiftMask(redMask, &redShift, &redMask);
      ComputeShiftMask(greenMask, &greenShift, &greenMask);
      ComputeShiftMask(blueMask, &blueShift, &blueMask);
      map = Allocate(sizeof(unsigned long) * MAX_COLORS);

      x = 0;
      for(red = 0; red < 4; red++) {
         for(green = 0; green < 4; green++) {
            for(blue = 0; blue < 4; blue++) {
               c.red = (unsigned short)(87381 * red / 4);
               c.green = (unsigned short)(87381 * green / 4);
               c.blue = (unsigned short)(87381 * blue / 4);
               c.flags = DoRed | DoGreen | DoBlue;
               JXAllocColor(display, rootColormap, &c);
               map[x] = c.pixel;
               x += 1;
            }
         }
      }

      /* Compute the reverse pixel mapping (pixel -> 24-bit RGB). */
      rmap = Allocate(sizeof(unsigned long) * BASE_COLORS);
      for(x = 0; x < BASE_COLORS; x++) {
         c.pixel = x;
         JXQueryColor(display, rootColormap, &c);
         GetDirectPixel(&c);
         rmap[x] = c.pixel;
      }

      break;
   }

   /* Get color information used for JWM stuff. */
   for(x = 0; x < COLOR_COUNT; x++) {
      if(names && names[x]) {
         if(ParseColor(names[x], &c)) {
            colors[x] = c.pixel;
            rgbColors[x] = GetRGBFromXColor(&c);
         } else {
            SetDefaultColor(x);
         }
      } else {
         SetDefaultColor(x);
      }
   }

   /* Derive colors. */
   for(x = 0; x < DERIVED_COUNT; x++) {
      LightenColor(DERIVED_COLORS[x].base, DERIVED_COLORS[x].up);
      DarkenColor(DERIVED_COLORS[x].base, DERIVED_COLORS[x].down);
   }

   if(names) {
      for(x = 0; x < COLOR_COUNT; x++) {
         if(names[x]) {
            Release(names[x]);
         }
      }
      Release(names);
      names = NULL;
   }<commit_msg>Allocate fewer colors with flat decorations.<commit_after>/**
 * @file color.c
 * @author <NAME>
 * @date 2004-2006
 *
 * @brief Functions to handle loading colors.
 *
 */

#include "jwm.h"
#include "main.h"
#include "color.h"
#include "error.h"
#include "misc.h"
#include "settings.h"

/** Mapping between color types and default values. */
typedef struct {
   ColorType type;
   unsigned int value;
} DefaultColorNode;

/** Mapping from color type to the value it from which it inherits. */
typedef struct {
   ColorType dest;
   ColorType src;
} ColorInheritNode;

unsigned long colors[COLOR_COUNT];
static unsigned long rgbColors[COLOR_COUNT];

/* Map a linear 8-bit RGB space to pixel values. */
static unsigned long *map;

/* Map 8-bit pixel values to a 24-bit linear RGB space. */
static unsigned long *rmap;

/* Number of JWM-specific colors allocated. */
static const unsigned BASE_COLORS = 64;
static const unsigned MAX_COLORS = 128;
static unsigned extraColors;

#ifdef USE_XFT
static XftColor *xftColors[COLOR_COUNT] = { NULL };
#endif

static const DefaultColorNode DEFAULT_COLORS[] = {

   { COLOR_TITLE_FG,                0xFFFFFF    },
   { COLOR_TITLE_ACTIVE_FG,         0xFFFFFF    },

   { COLOR_TITLE_BG1,               0x555555    },
   { COLOR_TITLE_BG2,               0x555555    },
   { COLOR_TITLE_ACTIVE_BG1,        0x0077CC    },
   { COLOR_TITLE_ACTIVE_BG2,        0x0077CC    },

   { COLOR_BORDER_LINE,             0x000000    },
   { COLOR_BORDER_ACTIVE_LINE,      0x000000    },

   { COLOR_TRAY_FG,                 0x000000    },
   { COLOR_TRAY_BG1,                0x999999    },
   { COLOR_TRAY_BG2,                0x999999    },
   { COLOR_TRAY_ACTIVE_FG,          0xFFFFFF    },
   { COLOR_TRAY_ACTIVE_BG1,         0x555555    },
   { COLOR_TRAY_ACTIVE_BG2,         0x555555    },

   { COLOR_PAGER_BG,                0x999999    },
   { COLOR_PAGER_FG,                0x333333    },
   { COLOR_PAGER_ACTIVE_BG,         0x555555    },
   { COLOR_PAGER_ACTIVE_FG,         0x0077CC    },
   { COLOR_PAGER_OUTLINE,           0x000000    },
   { COLOR_PAGER_TEXT,              0x000000    },

   { COLOR_MENU_BG,                 0x999999    },
   { COLOR_MENU_FG,                 0x000000    },
   { COLOR_MENU_ACTIVE_BG1,         0x0077CC    },
   { COLOR_MENU_ACTIVE_BG2,         0x0077CC    },
   { COLOR_MENU_ACTIVE_FG,          0x000000    },

   { COLOR_POPUP_BG,                0x999999    },
   { COLOR_POPUP_FG,                0x000000    }

};
static const unsigned DEFAULT_COUNT = ARRAY_LENGTH(DEFAULT_COLORS);

static struct {
   ColorType base;
   ColorType up;
   ColorType down;
} DERIVED_COLORS[] = {
   { COLOR_TITLE_BG1,        COLOR_TITLE_UP,        COLOR_TITLE_DOWN        },
   { COLOR_TITLE_ACTIVE_BG1, COLOR_TITLE_ACTIVE_UP, COLOR_TITLE_ACTIVE_DOWN },
   { COLOR_TRAY_BG1,         COLOR_TRAY_UP,         COLOR_TRAY_DOWN         },
   { COLOR_TRAY_ACTIVE_BG1,  COLOR_TRAY_ACTIVE_UP,  COLOR_TRAY_ACTIVE_DOWN  },
   { COLOR_MENU_BG,          COLOR_MENU_UP,         COLOR_MENU_DOWN         },
   { COLOR_MENU_ACTIVE_BG1,  COLOR_MENU_ACTIVE_UP,  COLOR_MENU_ACTIVE_DOWN  }
};
static const unsigned DERIVED_COUNT = ARRAY_LENGTH(DERIVED_COLORS);

static char **names = NULL;

static unsigned long redShift;
static unsigned long greenShift;
static unsigned long blueShift;
static unsigned long redMask;
static unsigned long greenMask;
static unsigned long blueMask;

static void ComputeShiftMask(unsigned long maskIn,
                             unsigned long *shiftOut,
                             unsigned long *maskOut);

static void GetDirectPixel(XColor *c);
static void GetMappedPixel(XColor *c, char alloc);

static void SetDefaultColor(ColorType type); 

static unsigned long ReadHex(const char *hex);

static unsigned long GetRGBFromXColor(const XColor *c);

static int GetColorByName(const char *str, XColor *c);
static void InitializeNames(void);

static XColor GetXColorFromRGB(unsigned long rgb);
static void LightenColor(ColorType oldColor, ColorType newColor);
static void DarkenColor(ColorType oldColor, ColorType newColor);

/** Startup color support. */
void StartupColors(void)
{

   unsigned int x;
   int red, green, blue;
   XColor c;

   /* Determine how to convert between RGB triples and pixels. */
   switch(rootVisual.visual->class) {
   case DirectColor:
   case TrueColor:
      ComputeShiftMask(rootVisual.visual->red_mask, &redShift, &redMask);
      ComputeShiftMask(rootVisual.visual->green_mask, &greenShift, &greenMask);
      ComputeShiftMask(rootVisual.visual->blue_mask, &blueShift, &blueMask);
      map = NULL;
      break;
   default:

      /* Attempt to get 64 colors, pretend it worked. */
      /* RGB: 2, 2, 2 */
      extraColors = 0;
      redMask = 0x30;
      greenMask = 0x0C;
      blueMask = 0x03;
      ComputeShiftMask(redMask, &redShift, &redMask);
      ComputeShiftMask(greenMask, &greenShift, &greenMask);
      ComputeShiftMask(blueMask, &blueShift, &blueMask);
      map = Allocate(sizeof(unsigned long) * MAX_COLORS);

      x = 0;
      for(red = 0; red < 4; red++) {
         for(green = 0; green < 4; green++) {
            for(blue = 0; blue < 4; blue++) {
               c.red = (unsigned short)(87381 * red / 4);
               c.green = (unsigned short)(87381 * green / 4);
               c.blue = (unsigned short)(87381 * blue / 4);
               c.flags = DoRed | DoGreen | DoBlue;
               JXAllocColor(display, rootColormap, &c);
               map[x] = c.pixel;
               x += 1;
            }
         }
      }

      /* Compute the reverse pixel mapping (pixel -> 24-bit RGB). */
      rmap = Allocate(sizeof(unsigned long) * BASE_COLORS);
      for(x = 0; x < BASE_COLORS; x++) {
         c.pixel = x;
         JXQueryColor(display, rootColormap, &c);
         GetDirectPixel(&c);
         rmap[x] = c.pixel;
      }

      break;
   }

   /* Get color information used for JWM stuff. */
   for(x = 0; x < COLOR_COUNT; x++) {
      if(names && names[x]) {
         if(ParseColor(names[x], &c)) {
            colors[x] = c.pixel;
            rgbColors[x] = GetRGBFromXColor(&c);
         } else {
            SetDefaultColor(x);
         }
      } else {
         SetDefaultColor(x);
      }
   }

   /* Derive colors. */
   if(settings.handles) {
      for(x = 0; x < DERIVED_COUNT; x++) {
         LightenColor(DERIVED_COLORS[x].base, DERIVED_COLORS[x].up);
         DarkenColor(DERIVED_COLORS[x].base, DERIVED_COLORS[x].down);
      }
   }

   if(names) {
      for(x = 0; x < COLOR_COUNT; x++) {
         if(names[x]) {
            Release(names[x]);
         }
      }
      Release(names);
      names = NULL;
   }
{'fraction_non_alphanumeric': 0.1037821482602118, 'fraction_numerical': 0.03615733736762481, 'mean_word_length': 2.323529411764706, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5340968', 'n_tokens_mistral': 4863, 'n_tokens_neox': 4456, 'n_words': 992}