Cleanup code from last commit
o9000 mrovi9000@gmail.com
5 files changed,
117 insertions(+),
119 deletions(-)
M
src/launcher/launcher.c
→
src/launcher/launcher.c
@@ -348,7 +348,7 @@ DATA32 *data = imlib_image_get_data();
adjust_asb(data, icon_size, icon_size, - launcher_alpha, + launcher_alpha / 100.0, launcher_saturation / 100.0, launcher_brightness / 100.0); imlib_image_put_back_data(data);
M
src/systray/systraybar.c
→
src/systray/systraybar.c
@@ -1338,7 +1338,7 @@ if (systray.alpha != 100 || systray.brightness != 0 || systray.saturation != 0)
adjust_asb(data, traywin->width, traywin->height, - systray.alpha, + systray.alpha / 100.0, systray.saturation / 100.0, systray.brightness / 100.0); imlib_image_put_back_data(data);
M
src/taskbar/task.c
→
src/taskbar/task.c
@@ -315,7 +315,7 @@ data32 = imlib_image_get_data();
adjust_asb(data32, task->icon_width, task->icon_height, - panel->g_task.alpha[k], + panel->g_task.alpha[k] / 100.0, panel->g_task.saturation[k] / 100.0, panel->g_task.brightness[k] / 100.0); imlib_image_put_back_data(data32);
M
src/util/common.c
→
src/util/common.c
@@ -238,128 +238,110 @@ g_strstrip(*value3);
} } -void adjust_asb(DATA32 *data, int w, int h, int alpha, float satur, float bright) +void adjust_asb(DATA32 *data, int w, int h, float alpha_adjust, float satur_adjust, float bright_adjust) { - unsigned int x, y; - unsigned int argb; - int a, r, g, b; - unsigned long id; - int cmax, cmin; - float h2, f, p, q, t; - float hue, saturation, brightness; - float redc, greenc, bluec; - - for (y = 0; y < h; y++) { - for (id = y * w, x = 0; x < w; x++, id++) { - argb = data[id]; - a = (argb >> 24) & 0xff; - // transparent => nothing to do. - if (a == 0) - continue; - r = (argb >> 16) & 0xff; - g = (argb >> 8) & 0xff; - b = (argb)&0xff; + for (int id = 0; id < w * h; id++) { + unsigned int argb = data[id]; + int a = (argb >> 24) & 0xff; + // transparent => nothing to do. + if (a == 0) + continue; + int r = (argb >> 16) & 0xff; + int g = (argb >> 8) & 0xff; + int b = (argb) & 0xff; - // convert RGB to HSB - cmax = (r > g) ? r : g; - if (b > cmax) - cmax = b; - cmin = (r < g) ? r : g; - if (b < cmin) - cmin = b; - brightness = ((float)cmax) / 255.0f; - if (cmax != 0) - saturation = ((float)(cmax - cmin)) / ((float)cmax); + // Convert RGB to HSV + int cmax = MAX3(r, g, b); + int cmin = MIN3(r, g, b); + int delta = cmax - cmin; + float brightness = cmax / 255.0f; + float saturation; + if (cmax != 0) + saturation = delta / (float)cmax; + else + saturation = 0; + float hue; + if (saturation == 0) { + hue = 0; + } else { + float redc = (cmax - r) / (float)delta; + float greenc = (cmax - g) / (float)delta; + float bluec = (cmax - b) / (float)delta; + if (r == cmax) + hue = bluec - greenc; + else if (g == cmax) + hue = 2.0f + redc - bluec; else - saturation = 0; - if (saturation == 0) - hue = 0; - else { - redc = ((float)(cmax - r)) / ((float)(cmax - cmin)); - greenc = ((float)(cmax - g)) / ((float)(cmax - cmin)); - bluec = ((float)(cmax - b)) / ((float)(cmax - cmin)); - if (r == cmax) - hue = bluec - greenc; - else if (g == cmax) - hue = 2.0f + redc - bluec; - else - hue = 4.0f + greenc - redc; - hue = hue / 6.0f; - if (hue < 0) - hue = hue + 1.0f; - } + hue = 4.0f + greenc - redc; + hue = hue / 6.0f; + if (hue < 0) + hue = hue + 1.0f; + } - // adjust - saturation += satur; - if (saturation < 0.0) - saturation = 0.0; - if (saturation > 1.0) - saturation = 1.0; - //brightness += bright; - if (brightness < 0.0) - brightness = 0.0; - if (brightness > 1.0) - brightness = 1.0; - if (alpha != 100) - a = (a * alpha) / 100; + // Adjust H, S + saturation += satur_adjust; + saturation = CLAMP(saturation, 0.0, 1.0); - // convert HSB to RGB - if (saturation == 0) { - r = g = b = (int)(brightness * 255.0f + 0.5f); - } else { - h2 = (hue - (int)hue) * 6.0f; - f = h2 - (int)(h2); - p = brightness * (1.0f - saturation); - q = brightness * (1.0f - saturation * f); - t = brightness * (1.0f - (saturation * (1.0f - f))); - switch ((int)h2) { - case 0: - r = (int)(brightness * 255.0f + 0.5f); - g = (int)(t * 255.0f + 0.5f); - b = (int)(p * 255.0f + 0.5f); - break; - case 1: - r = (int)(q * 255.0f + 0.5f); - g = (int)(brightness * 255.0f + 0.5f); - b = (int)(p * 255.0f + 0.5f); - break; - case 2: - r = (int)(p * 255.0f + 0.5f); - g = (int)(brightness * 255.0f + 0.5f); - b = (int)(t * 255.0f + 0.5f); - break; - case 3: - r = (int)(p * 255.0f + 0.5f); - g = (int)(q * 255.0f + 0.5f); - b = (int)(brightness * 255.0f + 0.5f); - break; - case 4: - r = (int)(t * 255.0f + 0.5f); - g = (int)(p * 255.0f + 0.5f); - b = (int)(brightness * 255.0f + 0.5f); - break; - case 5: - r = (int)(brightness * 255.0f + 0.5f); - g = (int)(p * 255.0f + 0.5f); - b = (int)(q * 255.0f + 0.5f); - break; - } + a *= alpha_adjust; + a = CLAMP(a, 0, 255); + + // Convert HSV to RGB + if (saturation == 0) { + r = g = b = (int)(brightness * 255.0f + 0.5f); + } else { + float h2 = (hue - (int)hue) * 6.0f; + float f = h2 - (int)(h2); + float p = brightness * (1.0f - saturation); + float q = brightness * (1.0f - saturation * f); + float t = brightness * (1.0f - (saturation * (1.0f - f))); + + switch ((int)h2) { + case 0: + r = (int)(brightness * 255.0f + 0.5f); + g = (int)(t * 255.0f + 0.5f); + b = (int)(p * 255.0f + 0.5f); + break; + case 1: + r = (int)(q * 255.0f + 0.5f); + g = (int)(brightness * 255.0f + 0.5f); + b = (int)(p * 255.0f + 0.5f); + break; + case 2: + r = (int)(p * 255.0f + 0.5f); + g = (int)(brightness * 255.0f + 0.5f); + b = (int)(t * 255.0f + 0.5f); + break; + case 3: + r = (int)(p * 255.0f + 0.5f); + g = (int)(q * 255.0f + 0.5f); + b = (int)(brightness * 255.0f + 0.5f); + break; + case 4: + r = (int)(t * 255.0f + 0.5f); + g = (int)(p * 255.0f + 0.5f); + b = (int)(brightness * 255.0f + 0.5f); + break; + case 5: + r = (int)(brightness * 255.0f + 0.5f); + g = (int)(p * 255.0f + 0.5f); + b = (int)(q * 255.0f + 0.5f); + break; } + } - r += bright * 255; - g += bright * 255; - b += bright * 255; + r += bright_adjust * 255; + g += bright_adjust * 255; + b += bright_adjust * 255; - r = MAX(0, MIN(255, r)); - g = MAX(0, MIN(255, g)); - b = MAX(0, MIN(255, b)); + r = CLAMP(r, 0, 255); + g = CLAMP(g, 0, 255); + b = CLAMP(b, 0, 255); - argb = a; - argb = (argb << 8) + r; - argb = (argb << 8) + g; - argb = (argb << 8) + b; - data[id] = argb; - } + argb = a; + argb = (argb << 8) + r; + argb = (argb << 8) + g; + argb = (argb << 8) + b; + data[id] = argb; } }@@ -511,7 +493,7 @@ DATA32 *data = imlib_image_get_data();
adjust_asb(data, imlib_image_get_width(), imlib_image_get_height(), - alpha, + alpha / 100.0, saturation / 100.0, brightness / 100.0); imlib_image_put_back_data(data);
M
src/util/common.h
→
src/util/common.h
@@ -8,6 +8,7 @@ #define COMMON_H
#define WM_CLASS_TINT "panel" +#include <glib.h> #include <Imlib2.h> #include <pango/pangocairo.h> #include "area.h"@@ -17,6 +18,9 @@ #define YELLOW "\033[1;33m"
#define RED "\033[1;31m" #define BLUE "\033[1;34m" #define RESET "\033[0m" + +#define MAX3(a, b, c) MAX(MAX(a, b), c) +#define MIN3(a, b, c) MIN(MIN(a, b), c) // mouse actions typedef enum MouseAction {@@ -67,8 +71,20 @@
Imlib_Image load_image(const char *path, int cached); // Adjusts the alpha/saturation/brightness on an ARGB image. -// Parameters: alpha from 0 to 100, satur from 0 to 1, bright from 0 to 1. -void adjust_asb(DATA32 *data, int w, int h, int alpha, float satur, float bright); +// Parameters: +// * alpha_adjust: multiplicative: +// * 0 = full transparency +// * 1 = no adjustment +// * 2 = twice the current opacity +// * satur_adjust: additive: +// * -1 = full grayscale +// * 0 = no adjustment +// * 1 = full color +// * bright_adjust: additive: +// * -1 = black +// * 0 = no adjustment +// * 1 = white +void adjust_asb(DATA32 *data, int w, int h, float alpha_adjust, float satur_adjust, float bright_adjust); Imlib_Image adjust_icon(Imlib_Image original, int alpha, int saturation, int brightness); void create_heuristic_mask(DATA32 *data, int w, int h);