]> Dogcows Code - chaz/tint2/blob - src/util/common.c
fixed segfault
[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_hsb(DATA32 *data, int w, int h, float hu, 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 r = (argb >> 16) & 0xff;
48 g = (argb >> 8) & 0xff;
49 b = (argb) & 0xff;
50
51 // convert RGB to HSB
52 cmax = (r > g) ? r : g;
53 if (b > cmax) cmax = b;
54 cmin = (r < g) ? r : g;
55 if (b < cmin) cmin = b;
56 brightness = ((float)cmax) / 255.0f;
57 if (cmax != 0)
58 saturation = ((float)(cmax - cmin)) / ((float)cmax);
59 else
60 saturation = 0;
61 if (saturation == 0)
62 hue = 0;
63 else {
64 redc = ((float)(cmax - r)) / ((float)(cmax - cmin));
65 greenc = ((float)(cmax - g)) / ((float)(cmax - cmin));
66 bluec = ((float)(cmax - b)) / ((float)(cmax - cmin));
67 if (r == cmax)
68 hue = bluec - greenc;
69 else if (g == cmax)
70 hue = 2.0f + redc - bluec;
71 else
72 hue = 4.0f + greenc - redc;
73 hue = hue / 6.0f;
74 if (hue < 0)
75 hue = hue + 1.0f;
76 }
77
78 // adjust
79 saturation += satur;
80 if (saturation < 0.0) saturation = 0.0;
81 if (saturation > 1.0) saturation = 1.0;
82 brightness += bright;
83 if (brightness < 0.0) brightness = 0.0;
84 if (brightness > 1.0) brightness = 1.0;
85 hue += hu;
86 if (hue < 0.0) hue = 0.0;
87 if (hue > 1.0) hue = 1.0;
88
89 // convert HSB to RGB
90 if (saturation == 0) {
91 r = g = b = (int)(brightness * 255.0f + 0.5f);
92 } else {
93 h2 = (hue - (int)hue) * 6.0f;
94 f = h2 - (int)(h2);
95 p = brightness * (1.0f - saturation);
96 q = brightness * (1.0f - saturation * f);
97 t = brightness * (1.0f - (saturation * (1.0f - f)));
98 switch ((int) h2) {
99 case 0:
100 r = (int)(brightness * 255.0f + 0.5f);
101 g = (int)(t * 255.0f + 0.5f);
102 b = (int)(p * 255.0f + 0.5f);
103 break;
104 case 1:
105 r = (int)(q * 255.0f + 0.5f);
106 g = (int)(brightness * 255.0f + 0.5f);
107 b = (int)(p * 255.0f + 0.5f);
108 break;
109 case 2:
110 r = (int)(p * 255.0f + 0.5f);
111 g = (int)(brightness * 255.0f + 0.5f);
112 b = (int)(t * 255.0f + 0.5f);
113 break;
114 case 3:
115 r = (int)(p * 255.0f + 0.5f);
116 g = (int)(q * 255.0f + 0.5f);
117 b = (int)(brightness * 255.0f + 0.5f);
118 break;
119 case 4:
120 r = (int)(t * 255.0f + 0.5f);
121 g = (int)(p * 255.0f + 0.5f);
122 b = (int)(brightness * 255.0f + 0.5f);
123 break;
124 case 5:
125 r = (int)(brightness * 255.0f + 0.5f);
126 g = (int)(p * 255.0f + 0.5f);
127 b = (int)(q * 255.0f + 0.5f);
128 break;
129 }
130 }
131
132 argb = a;
133 argb = (argb << 8) + r;
134 argb = (argb << 8) + g;
135 argb = (argb << 8) + b;
136 data[id] = argb;
137 }
138 }
139 }
140
141
This page took 0.042796 seconds and 5 git commands to generate.