Falkon Develop
Cross-platform Qt-based web browser
colors.cpp
Go to the documentation of this file.
1/*
2 * Bespin library for Qt style, KWin decoration and everything else
3 * Copyright 2007-2012 by Thomas Lübking <thomas.luebking@gmail.com>
4 *
5 * This library is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Library General Public License version 2
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Library General Public License for more details
12 *
13 * You should have received a copy of the GNU Library General Public
14 * License along with this program; if not, write to the
15 * Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18
19#include "colors.h"
20#define CLAMP(x,l,u) (x) < (l) ? (l) :\
21 (x) > (u) ? (u) :\
22 (x)
23#include <QWidget>
24#include <QApplication>
25
26// using namespace Bespin;
27
28const QColor &
29Colors::bg(const QPalette &pal, const QWidget* w)
30{
31 QPalette::ColorRole role;
32 if (!w) {
33 role = QPalette::Window;
34 }
35 else if (w->parentWidget()) {
36 role = w->parentWidget()->backgroundRole();
37 }
38 else {
39 role = w->backgroundRole();
40 }
41
42// if (pal.brush(role).style() > 1)
43 return pal.color(role);
44// return QApplication::palette().color(role);
45}
46
47int
48Colors::contrast(const QColor &a, const QColor &b)
49{
50 int ar, ag, ab, br, bg, bb;
51 a.getRgb(&ar, &ag, &ab);
52 b.getRgb(&br, &bg, &bb);
53
54 int diff = 299 * (ar - br) + 587 * (ag - bg) + 114 * (ab - bb);
55 diff = (diff < 0) ? -diff : 90 * diff / 100;
56 int perc = diff / 2550;
57
58 diff = qMax(ar, br) + qMax(ag, bg) + qMax(ab, bb)
59 - (qMin(ar, br) + qMin(ag, bg) + qMin(ab, bb));
60
61 perc += diff / 765;
62 perc /= 2;
63
64 return perc;
65}
66
67QPalette::ColorRole
68Colors::counterRole(QPalette::ColorRole role)
69{
70 switch (role) {
71 case QPalette::ButtonText: //8
72 return QPalette::Button;
73 case QPalette::WindowText: //0
74 return QPalette::Window;
75 case QPalette::HighlightedText: //13
76 return QPalette::Highlight;
77 case QPalette::Window: //10
78 return QPalette::WindowText;
79 case QPalette::Base: //9
80 return QPalette::Text;
81 case QPalette::Text: //6
82 return QPalette::Base;
83 case QPalette::Highlight: //12
84 return QPalette::HighlightedText;
85 case QPalette::Button: //1
86 return QPalette::ButtonText;
87 default:
88 return QPalette::Window;
89 }
90}
91
92bool
93Colors::counterRole(QPalette::ColorRole &from, QPalette::ColorRole &to, QPalette::ColorRole defFrom,
94 QPalette::ColorRole defTo)
95{
96 switch (from) {
97 case QPalette::WindowText: //0
98 to = QPalette::Window;
99 break;
100 case QPalette::Window: //10
101 to = QPalette::WindowText;
102 break;
103 case QPalette::Base: //9
104 to = QPalette::Text;
105 break;
106 case QPalette::Text: //6
107 to = QPalette::Base;
108 break;
109 case QPalette::Button: //1
110 to = QPalette::ButtonText;
111 break;
112 case QPalette::ButtonText: //8
113 to = QPalette::Button;
114 break;
115 case QPalette::Highlight: //12
116 to = QPalette::HighlightedText;
117 break;
118 case QPalette::HighlightedText: //13
119 to = QPalette::Highlight;
120 break;
121 default:
122 from = defFrom;
123 to = defTo;
124 return false;
125 }
126 return true;
127}
128
129QColor
130Colors::emphasize(const QColor &c, int value)
131{
132 int h, s, v, a;
133 QColor ret;
134 c.getHsv(&h, &s, &v, &a);
135 if (v < 75 + value) {
136 ret.setHsv(h, s, CLAMP(85 + value, 85, 255), a);
137 return ret;
138 }
139 if (v > 200) {
140 if (s > 30) {
141 h -= 5;
142 if (h < 0) {
143 h = 360 + h;
144 }
145 s = (s << 3) / 9;
146 v += value;
147 ret.setHsv(h, CLAMP(s, 30, 255), CLAMP(v, 0, 255), a);
148 return ret;
149 }
150 if (v > 230) {
151 ret.setHsv(h, s, CLAMP(v - value, 0, 255), a);
152 return ret;
153 }
154 }
155 if (v > 128) {
156 ret.setHsv(h, s, CLAMP(v + value, 0, 255), a);
157 }
158 else {
159 ret.setHsv(h, s, CLAMP(v - value, 0, 255), a);
160 }
161 return ret;
162}
163
164bool
165Colors::haveContrast(const QColor &a, const QColor &b)
166{
167 int ar, ag, ab, br, bg, bb;
168 a.getRgb(&ar, &ag, &ab);
169 b.getRgb(&br, &bg, &bb);
170
171 int diff = (299 * (ar - br) + 587 * (ag - bg) + 114 * (ab - bb));
172
173 if (qAbs(diff) < 91001) {
174 return false;
175 }
176
177 diff = qMax(ar, br) + qMax(ag, bg) + qMax(ab, bb)
178 - (qMin(ar, br) + qMin(ag, bg) + qMin(ab, bb));
179
180 return (diff > 300);
181}
182
183QColor
184Colors::light(const QColor &c, int value)
185{
186 int h, s, v, a;
187 c.getHsv(&h, &s, &v, &a);
188 QColor ret;
189 if (v < 255 - value) {
190 ret.setHsv(h, s, CLAMP(v + value, 0, 255), a); //value could be negative
191 return ret;
192 }
193 // psychovisual uplightning, i.e. shift hue and lower saturation
194 if (s > 30) {
195 h -= (value * 5 / 20);
196 if (h < 0) {
197 h = 400 + h;
198 }
199 s = CLAMP((s << 3) / 9, 30, 255);
200 ret.setHsv(h, s, 255, a);
201 return ret;
202 }
203 else { // hue shifting has no sense, half saturation (btw, white won't get brighter :)
204 ret.setHsv(h, s >> 1, 255, a);
205 }
206 return ret;
207}
208
209QColor
210Colors::mid(const QColor &c1, const QColor &c2, int w1, int w2)
211{
212 int sum = (w1 + w2);
213 if (!sum) {
214 return Qt::black;
215 }
216
217 int r, g, b, a;
218#if 0
219 QColor c1 = oc1;
220 b = value(c1);
221 if (b < 70) {
222 c1.getHsv(&r, &g, &b, &a);
223 c1.setHsv(r, g, 70, a);
224 }
225#endif
226 r = (w1 * c1.red() + w2 * c2.red()) / sum;
227 r = CLAMP(r, 0, 255);
228 g = (w1 * c1.green() + w2 * c2.green()) / sum;
229 g = CLAMP(g, 0, 255);
230 b = (w1 * c1.blue() + w2 * c2.blue()) / sum;
231 b = CLAMP(b, 0, 255);
232 a = (w1 * c1.alpha() + w2 * c2.alpha()) / sum;
233 a = CLAMP(a, 0, 255);
234 return QColor(r, g, b, a);
235}
236
237int
238Colors::value(const QColor &c)
239{
240 int v = c.red();
241 if (c.green() > v) {
242 v = c.green();
243 }
244 if (c.blue() > v) {
245 v = c.blue();
246 }
247 return v;
248}
#define CLAMP(x, l, u)
Definition: colors.cpp:20
QColor mid(const QColor &oc1, const QColor &c2, int w1=1, int w2=1)
Definition: colors.cpp:210
int value(const QColor &c)
Definition: colors.cpp:238
int contrast(const QColor &a, const QColor &b)
Definition: colors.cpp:48
QPalette::ColorRole counterRole(QPalette::ColorRole role)
Definition: colors.cpp:68
QColor light(const QColor &c, int value)
Definition: colors.cpp:184
const QColor & bg(const QPalette &pal, const QWidget *w)
Definition: colors.cpp:29
bool haveContrast(const QColor &a, const QColor &b)
Definition: colors.cpp:165
QColor emphasize(const QColor &c, int value=10)
Definition: colors.cpp:130