<commit_before>/****************************************************************************
 * Functions to handle loading colors.
 * Copyright (C) 2004 <NAME>
 ****************************************************************************/

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

typedef struct {
	ColorType type;
	const char *value;
} DefaultColorNode;

static const float COLOR_DELTA = 0.45;

unsigned long colors[COLOR_COUNT];
unsigned long rgbColors[COLOR_COUNT];
unsigned long white;
unsigned long black;

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

static DefaultColorNode DEFAULT_COLORS[] = {
	{ COLOR_BORDER_BG,          "gray"    },
	{ COLOR_BORDER_FG,          "black"   },
	{ COLOR_BORDER_ACTIVE_BG,   "red"     },
	{ COLOR_BORDER_ACTIVE_FG,   "white"   },
	{ COLOR_TRAY_BG,            "gray"    },
	{ COLOR_TRAY_FG,            "black"   },
	{ COLOR_TASK_BG,            "gray"    },
	{ COLOR_TASK_FG,            "black"   },
	{ COLOR_TASK_ACTIVE_BG,     "red"     },
	{ COLOR_TASK_ACTIVE_FG,     "white"   },
	{ COLOR_PAGER_BG,           "black"   },
	{ COLOR_PAGER_FG,           "gray"    },
	{ COLOR_PAGER_ACTIVE_BG,    "red"     },
	{ COLOR_PAGER_ACTIVE_FG,    "red"     },
	{ COLOR_PAGER_OUTLINE,      "black"   },
	{ COLOR_MENU_BG,            "gray"    },
	{ COLOR_MENU_FG,            "black"   },
	{ COLOR_MENU_ACTIVE_BG,     "red"     },
	{ COLOR_MENU_ACTIVE_FG,     "white"   },
	{ COLOR_POPUP_BG,           "yellow"  },
	{ COLOR_POPUP_FG,           "black"   },
	{ COLOR_POPUP_OUTLINE,      "black"   },
	{ COLOR_TRAYBUTTON_FG,      "black"   },
	{ COLOR_TRAYBUTTON_BG,      "gray"    },
	{ COLOR_CLOCK_FG,           "black"   },
	{ COLOR_CLOCK_BG,           "gray"    },
	{ COLOR_COUNT,              NULL      }
};

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 GetDirectColor(XColor *c);
static void GetDirectPixel(XColor *c);

static int ParseColor(ColorType type, const char *value);
static void SetDefaultColor(ColorType type); 

static unsigned long ReadHex(const char *hex);

static unsigned long GetRGBFromXColor(const XColor *c);
static XColor GetXColorFromRGB(unsigned long rgb);

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

static void LightenColor(ColorType oldColor, ColorType newColor);
static void DarkenColor(ColorType oldColor, ColorType newColor);

/****************************************************************************
 ****************************************************************************/
void InitializeColors() {

}

/****************************************************************************
 ****************************************************************************/
void StartupColors() {

	int x;

	/* Determine how to convert between RGB triples and pixels. */
	Assert(rootVisual);
	switch(rootVisual->class) {
	case DirectColor:
	case TrueColor:
		ComputeShiftMask(rootVisual->red_mask, &redShift, &redMask);
		ComputeShiftMask(rootVisual->green_mask, &greenShift, &greenMask);
		ComputeShiftMask(rootVisual->blue_mask, &blueShift, &blueMask);
		break;
	default:
		Warning("visual class not supported, performance will suffer");
		break;
	}

	/* Inherit unset colors from the tray for tray items. */
	if(names) {
		if(!names[COLOR_TASK_BG]) {
			names[COLOR_TASK_BG] = CopyString(names[COLOR_TRAY_BG]);
		}
		if(!names[COLOR_TRAYBUTTON_BG]) {
			names[COLOR_TRAYBUTTON_BG] = CopyString(names[COLOR_TRAY_BG]);
		}
		if(!names[COLOR_CLOCK_BG]) {
			names[COLOR_CLOCK_BG] = CopyString(names[COLOR_TRAY_BG]);
		}
		if(!names[COLOR_TASK_FG]) {
			names[COLOR_TASK_FG] = CopyString(names[COLOR_TRAY_FG]);
		}
		if(!names[COLOR_TRAYBUTTON_FG]) {
			names[COLOR_TRAYBUTTON_FG] = CopyString(names[COLOR_TRAY_FG]);
		}
		if(!names[COLOR_CLOCK_FG]) {
			names[COLOR_CLOCK_FG] = CopyString(names[COLOR_TRAY_FG]);
		}
	}

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

	if(names) {
		for(x = 0; x < COLOR_COUNT; x++) {
			if(names[x]) {
				Release(names[x]);
			}
		}
		Release(names);
		names = NULL;
	}

	LightenColor(COLOR_BORDER_BG, COLOR_BORDER_UP);
	DarkenColor(COLOR_BORDER_BG, COLOR_BORDER_DOWN);

	LightenColor(COLOR_BORDER_ACTIVE_BG, COLOR_BORDER_ACTIVE_UP);
	DarkenColor(COLOR_BORDER_ACTIVE_BG, COLOR_BORDER_ACTIVE_DOWN);

	LightenColor(COLOR_TRAY_BG, COLOR_TRAY_UP);
	DarkenColor(COLOR_TRAY_BG, COLOR_TRAY_DOWN);

	LightenColor(COLOR_TASK_BG, COLOR_TASK_UP);
	DarkenColor(COLOR_TASK_BG, COLOR_TASK_DOWN);

	LightenColor(COLOR_TASK_ACTIVE_BG, COLOR_TASK_ACTIVE_UP);
	DarkenColor(COLOR_TASK_ACTIVE_BG, COLOR_TASK_ACTIVE_DOWN);

	LightenColor(COLOR_MENU_BG, COLOR_MENU_UP);
	DarkenColor(COLOR_MENU_BG, COLOR_MENU_DOWN);

	LightenColor(COLOR_MENU_ACTIVE_BG, COLOR_MENU_ACTIVE_UP);
	DarkenColor(COLOR_MENU_ACTIVE_BG, COLOR_MENU_ACTIVE_DOWN);

	white = WhitePixel(display, rootScreen);
	black = BlackPixel(display, rootScreen);

}

/****************************************************************************
 ****************************************************************************/
void ShutdownColors() {

	int x;

#ifdef USE_XFT
	for(x = 0; x < COLOR_COUNT; x++) {
		if(xftColors[x]) {
			JXftColorFree(display, rootVisual, rootColormap, xftColors[x]);
			Release(xftColors[x]);
			xftColors[x] = NULL;
		}
	}
#endif

}

/****************************************************************************
 ****************************************************************************/
void DestroyColors() {

	int x;

	if(names) {
		for(x = 0; x < COLOR_COUNT; x++) {
			if(names[x]) {
				Release(names[x]);
			}
		}
		Release(names);
		names = NULL;
	}

}

/****************************************************************************
 ****************************************************************************/
void ComputeShiftMask(unsigned long maskIn,
	unsigned long *shiftOut, unsigned long *maskOut) {

	int shift;

	/* Components are stored in 16 bits.<commit_msg>Fix a warning.<commit_after>/****************************************************************************
 * Functions to handle loading colors.
 * Copyright (C) 2004 <NAME>
 ****************************************************************************/

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

typedef struct {
	ColorType type;
	const char *value;
} DefaultColorNode;

static const float COLOR_DELTA = 0.45;

unsigned long colors[COLOR_COUNT];
unsigned long rgbColors[COLOR_COUNT];
unsigned long white;
unsigned long black;

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

static DefaultColorNode DEFAULT_COLORS[] = {
	{ COLOR_BORDER_BG,          "gray"    },
	{ COLOR_BORDER_FG,          "black"   },
	{ COLOR_BORDER_ACTIVE_BG,   "red"     },
	{ COLOR_BORDER_ACTIVE_FG,   "white"   },
	{ COLOR_TRAY_BG,            "gray"    },
	{ COLOR_TRAY_FG,            "black"   },
	{ COLOR_TASK_BG,            "gray"    },
	{ COLOR_TASK_FG,            "black"   },
	{ COLOR_TASK_ACTIVE_BG,     "red"     },
	{ COLOR_TASK_ACTIVE_FG,     "white"   },
	{ COLOR_PAGER_BG,           "black"   },
	{ COLOR_PAGER_FG,           "gray"    },
	{ COLOR_PAGER_ACTIVE_BG,    "red"     },
	{ COLOR_PAGER_ACTIVE_FG,    "red"     },
	{ COLOR_PAGER_OUTLINE,      "black"   },
	{ COLOR_MENU_BG,            "gray"    },
	{ COLOR_MENU_FG,            "black"   },
	{ COLOR_MENU_ACTIVE_BG,     "red"     },
	{ COLOR_MENU_ACTIVE_FG,     "white"   },
	{ COLOR_POPUP_BG,           "yellow"  },
	{ COLOR_POPUP_FG,           "black"   },
	{ COLOR_POPUP_OUTLINE,      "black"   },
	{ COLOR_TRAYBUTTON_FG,      "black"   },
	{ COLOR_TRAYBUTTON_BG,      "gray"    },
	{ COLOR_CLOCK_FG,           "black"   },
	{ COLOR_CLOCK_BG,           "gray"    },
	{ COLOR_COUNT,              NULL      }
};

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 GetDirectColor(XColor *c);
static void GetDirectPixel(XColor *c);

static int ParseColor(ColorType type, const char *value);
static void SetDefaultColor(ColorType type); 

static unsigned long ReadHex(const char *hex);

static unsigned long GetRGBFromXColor(const XColor *c);
static XColor GetXColorFromRGB(unsigned long rgb);

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

static void LightenColor(ColorType oldColor, ColorType newColor);
static void DarkenColor(ColorType oldColor, ColorType newColor);

/****************************************************************************
 ****************************************************************************/
void InitializeColors() {

}

/****************************************************************************
 ****************************************************************************/
void StartupColors() {

	int x;

	/* Determine how to convert between RGB triples and pixels. */
	Assert(rootVisual);
	switch(rootVisual->class) {
	case DirectColor:
	case TrueColor:
		ComputeShiftMask(rootVisual->red_mask, &redShift, &redMask);
		ComputeShiftMask(rootVisual->green_mask, &greenShift, &greenMask);
		ComputeShiftMask(rootVisual->blue_mask, &blueShift, &blueMask);
		break;
	default:
		Warning("visual class not supported, performance will suffer");
		break;
	}

	/* Inherit unset colors from the tray for tray items. */
	if(names) {
		if(!names[COLOR_TASK_BG]) {
			names[COLOR_TASK_BG] = CopyString(names[COLOR_TRAY_BG]);
		}
		if(!names[COLOR_TRAYBUTTON_BG]) {
			names[COLOR_TRAYBUTTON_BG] = CopyString(names[COLOR_TRAY_BG]);
		}
		if(!names[COLOR_CLOCK_BG]) {
			names[COLOR_CLOCK_BG] = CopyString(names[COLOR_TRAY_BG]);
		}
		if(!names[COLOR_TASK_FG]) {
			names[COLOR_TASK_FG] = CopyString(names[COLOR_TRAY_FG]);
		}
		if(!names[COLOR_TRAYBUTTON_FG]) {
			names[COLOR_TRAYBUTTON_FG] = CopyString(names[COLOR_TRAY_FG]);
		}
		if(!names[COLOR_CLOCK_FG]) {
			names[COLOR_CLOCK_FG] = CopyString(names[COLOR_TRAY_FG]);
		}
	}

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

	if(names) {
		for(x = 0; x < COLOR_COUNT; x++) {
			if(names[x]) {
				Release(names[x]);
			}
		}
		Release(names);
		names = NULL;
	}

	LightenColor(COLOR_BORDER_BG, COLOR_BORDER_UP);
	DarkenColor(COLOR_BORDER_BG, COLOR_BORDER_DOWN);

	LightenColor(COLOR_BORDER_ACTIVE_BG, COLOR_BORDER_ACTIVE_UP);
	DarkenColor(COLOR_BORDER_ACTIVE_BG, COLOR_BORDER_ACTIVE_DOWN);

	LightenColor(COLOR_TRAY_BG, COLOR_TRAY_UP);
	DarkenColor(COLOR_TRAY_BG, COLOR_TRAY_DOWN);

	LightenColor(COLOR_TASK_BG, COLOR_TASK_UP);
	DarkenColor(COLOR_TASK_BG, COLOR_TASK_DOWN);

	LightenColor(COLOR_TASK_ACTIVE_BG, COLOR_TASK_ACTIVE_UP);
	DarkenColor(COLOR_TASK_ACTIVE_BG, COLOR_TASK_ACTIVE_DOWN);

	LightenColor(COLOR_MENU_BG, COLOR_MENU_UP);
	DarkenColor(COLOR_MENU_BG, COLOR_MENU_DOWN);

	LightenColor(COLOR_MENU_ACTIVE_BG, COLOR_MENU_ACTIVE_UP);
	DarkenColor(COLOR_MENU_ACTIVE_BG, COLOR_MENU_ACTIVE_DOWN);

	white = WhitePixel(display, rootScreen);
	black = BlackPixel(display, rootScreen);

}

/****************************************************************************
 ****************************************************************************/
void ShutdownColors() {


#ifdef USE_XFT

	int x;

	for(x = 0; x < COLOR_COUNT; x++) {
		if(xftColors[x]) {
			JXftColorFree(display, rootVisual, rootColormap, xftColors[x]);
			Release(xftColors[x]);
			xftColors[x] = NULL;
		}
	}

#endif

}

/****************************************************************************
 ****************************************************************************/
void DestroyColors() {

	int x;

	if(names) {
		for(x = 0; x < COLOR_COUNT; x++) {
			if(names[x]) {
				Release(names[x]);
			}
		}
		Release(names);
		names = NULL;
	}

}

/****************************************************************************
 ****************************************************************************/
void ComputeShiftMask(unsigned long maskIn,
	unsigned long *shiftOut, unsigned long *maskOut) {

	int shift;

	/* Components are stored in 16 bits.
{'fraction_non_alphanumeric': 0.2473929961089494, 'fraction_numerical': 0.0020233463035019454, 'mean_word_length': 4.833408987743986, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '7074990', 'n_tokens_mistral': 4587, 'n_tokens_neox': 4260, 'n_words': 783}