]> Dogcows Code - chaz/tint2/blob - src/util/common.c
31c2d2eb2d67cafc67d39777bde97a41f9da5672
[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(unsigned int *data, int w, int h, float hu, float satur, float bright)
34 {
35 unsigned int *pt = data;
36 int x, y;
37 unsigned int a, r, g, b, argb;
38 int cmax, cmin;
39 float h2, f, p, q, t;
40 float hue, saturation, brightness;
41 float redc, greenc, bluec;
42
43
44 for(y = 0; y < h; y++) {
45 for(x = 0; x < w; x++) {
46 argb = pt[y * h + x];
47 a = (argb >> 24) & 0xff;
48 r = (argb >> 16) & 0xff;
49 g = (argb >> 8) & 0xff;
50 b = (argb) & 0xff;
51
52 // convert RGB to HSB
53 cmax = (r > g) ? r : g;
54 if (b > cmax) cmax = b;
55 cmin = (r < g) ? r : g;
56 if (b < cmin) cmin = b;
57 brightness = ((float)cmax) / 255.0f;
58 if (cmax != 0)
59 saturation = ((float)(cmax - cmin)) / ((float)cmax);
60 else
61 saturation = 0;
62 if (saturation == 0)
63 hue = 0;
64 else {
65 redc = ((float)(cmax - r)) / ((float)(cmax - cmin));
66 greenc = ((float)(cmax - g)) / ((float)(cmax - cmin));
67 bluec = ((float)(cmax - b)) / ((float)(cmax - cmin));
68 if (r == cmax)
69 hue = bluec - greenc;
70 else if (g == cmax)
71 hue = 2.0f + redc - bluec;
72 else
73 hue = 4.0f + greenc - redc;
74 hue = hue / 6.0f;
75 if (hue < 0)
76 hue = hue + 1.0f;
77 }
78
79 // adjust
80 saturation += satur;
81 if (saturation < 0.0) saturation = 0.0;
82 if (saturation > 1.0) saturation = 1.0;
83 brightness += bright;
84 if (brightness < 0.0) brightness = 0.0;
85 if (brightness > 1.0) brightness = 1.0;
86 hue += hu;
87 if (hue < 0.0) hue = 0.0;
88 if (hue > 1.0) hue = 1.0;
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 pt[y * h + x] = argb;
138 }
139 }
140 }
141
142
This page took 0.038767 seconds and 3 git commands to generate.