]> Dogcows Code - chaz/tint2/blob - src/util/common.c
lower cpu use with icon. replace HUE by ALPHA on icon (see task_icon_asb).
[chaz/tint2] / src / util / common.c
1 /**************************************************************************
2 *
3 * Tint2 : common windows function
4 *
5 * Copyright (C) 2007 Pål Staurland (staura@gmail.com)
6 * Modified (C) 2008 thierry lorthiois (lorthiois@bbsoft.fr)
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License version 2
10 * as published by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 **************************************************************************/
20
21 #include <X11/Xlib.h>
22 #include <X11/Xutil.h>
23 #include <X11/Xatom.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28
29 #include "common.h"
30
31
32
33 void adjust_asb(DATA32 *data, int w, int h, int alpha, float satur, float bright)
34 {
35 unsigned int x, y;
36 unsigned int a, r, g, b, argb;
37 unsigned long id;
38 int cmax, cmin;
39 float h2, f, p, q, t;
40 float hue, saturation, brightness;
41 float redc, greenc, bluec;
42
43 for(y = 0; y < h; y++) {
44 for(id = y * w, x = 0; x < w; x++, id++) {
45 argb = data[id];
46 a = (argb >> 24) & 0xff;
47 // transparent => nothing to do.
48 if (a == 0) continue;
49 r = (argb >> 16) & 0xff;
50 g = (argb >> 8) & 0xff;
51 b = (argb) & 0xff;
52
53 // convert RGB to HSB
54 cmax = (r > g) ? r : g;
55 if (b > cmax) cmax = b;
56 cmin = (r < g) ? r : g;
57 if (b < cmin) cmin = b;
58 brightness = ((float)cmax) / 255.0f;
59 if (cmax != 0)
60 saturation = ((float)(cmax - cmin)) / ((float)cmax);
61 else
62 saturation = 0;
63 if (saturation == 0)
64 hue = 0;
65 else {
66 redc = ((float)(cmax - r)) / ((float)(cmax - cmin));
67 greenc = ((float)(cmax - g)) / ((float)(cmax - cmin));
68 bluec = ((float)(cmax - b)) / ((float)(cmax - cmin));
69 if (r == cmax)
70 hue = bluec - greenc;
71 else if (g == cmax)
72 hue = 2.0f + redc - bluec;
73 else
74 hue = 4.0f + greenc - redc;
75 hue = hue / 6.0f;
76 if (hue < 0)
77 hue = hue + 1.0f;
78 }
79
80 // adjust
81 saturation += satur;
82 if (saturation < 0.0) saturation = 0.0;
83 if (saturation > 1.0) saturation = 1.0;
84 brightness += bright;
85 if (brightness < 0.0) brightness = 0.0;
86 if (brightness > 1.0) brightness = 1.0;
87 if (alpha != 100)
88 a = (a * alpha)/100;
89
90 // convert HSB to RGB
91 if (saturation == 0) {
92 r = g = b = (int)(brightness * 255.0f + 0.5f);
93 } else {
94 h2 = (hue - (int)hue) * 6.0f;
95 f = h2 - (int)(h2);
96 p = brightness * (1.0f - saturation);
97 q = brightness * (1.0f - saturation * f);
98 t = brightness * (1.0f - (saturation * (1.0f - f)));
99 switch ((int) h2) {
100 case 0:
101 r = (int)(brightness * 255.0f + 0.5f);
102 g = (int)(t * 255.0f + 0.5f);
103 b = (int)(p * 255.0f + 0.5f);
104 break;
105 case 1:
106 r = (int)(q * 255.0f + 0.5f);
107 g = (int)(brightness * 255.0f + 0.5f);
108 b = (int)(p * 255.0f + 0.5f);
109 break;
110 case 2:
111 r = (int)(p * 255.0f + 0.5f);
112 g = (int)(brightness * 255.0f + 0.5f);
113 b = (int)(t * 255.0f + 0.5f);
114 break;
115 case 3:
116 r = (int)(p * 255.0f + 0.5f);
117 g = (int)(q * 255.0f + 0.5f);
118 b = (int)(brightness * 255.0f + 0.5f);
119 break;
120 case 4:
121 r = (int)(t * 255.0f + 0.5f);
122 g = (int)(p * 255.0f + 0.5f);
123 b = (int)(brightness * 255.0f + 0.5f);
124 break;
125 case 5:
126 r = (int)(brightness * 255.0f + 0.5f);
127 g = (int)(p * 255.0f + 0.5f);
128 b = (int)(q * 255.0f + 0.5f);
129 break;
130 }
131 }
132
133 argb = a;
134 argb = (argb << 8) + r;
135 argb = (argb << 8) + g;
136 argb = (argb << 8) + b;
137 data[id] = argb;
138 }
139 }
140 }
141
This page took 0.041197 seconds and 5 git commands to generate.