1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2
/* ***** BEGIN LICENSE BLOCK *****
3
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
5
* The contents of this file are subject to the Netscape Public License
6
* Version 1.1 (the "License"); you may not use this file except in
7
* compliance with the License. You may obtain a copy of the License at
8
* http://www.mozilla.org/NPL/
10
* Software distributed under the License is distributed on an "AS IS" basis,
11
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12
* for the specific language governing rights and limitations under the
15
* The Original Code is mozilla.org code.
17
* The Initial Developer of the Original Code is
18
* Netscape Communications Corporation.
19
* Portions created by the Initial Developer are Copyright (C) 1998
20
* the Initial Developer. All Rights Reserved.
25
* Alternatively, the contents of this file may be used under the terms of
26
* either the GNU General Public License Version 2 or later (the "GPL"), or
27
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28
* in which case the provisions of the GPL or the LGPL are applicable instead
29
* of those above. If you wish to allow use of your version of this file only
30
* under the terms of either the GPL or the LGPL, and not to allow others to
31
* use your version of this file under the terms of the NPL, indicate your
32
* decision by deleting the provisions above and replace them with the notice
33
* and other provisions required by the GPL or the LGPL. If you do not delete
34
* the provisions above, a recipient may use your version of this file under
35
* the terms of any one of the NPL, the GPL or the LGPL.
37
* ***** END LICENSE BLOCK ***** */
39
#include "nsCSSColorUtils.h"
42
// Weird color computing code stolen from winfe which was stolen
43
// from the xfe which was written originally by Eric Bina. So there.
45
#define RED_LUMINOSITY 30
46
#define GREEN_LUMINOSITY 59
47
#define BLUE_LUMINOSITY 11
48
#define INTENSITY_FACTOR 25
49
#define LIGHT_FACTOR 0
50
#define LUMINOSITY_FACTOR 75
53
#define COLOR_DARK_THRESHOLD 51
54
#define COLOR_LIGHT_THRESHOLD 204
56
#define COLOR_LITE_BS_FACTOR 45
57
#define COLOR_LITE_TS_FACTOR 70
59
#define COLOR_DARK_BS_FACTOR 30
60
#define COLOR_DARK_TS_FACTOR 50
62
#define LIGHT_GRAY NS_RGB(192, 192, 192)
63
#define DARK_GRAY NS_RGB(96, 96, 96)
64
#define WHITE NS_RGB(255, 255, 255)
65
#define BLACK NS_RGB(0, 0, 0)
67
#define MAX_BRIGHTNESS 254
68
#define MAX_DARKNESS 0
70
void NS_Get3DColors(nscolor aResult[2], nscolor aBackgroundColor)
72
int rb = NS_GET_R(aBackgroundColor);
73
int gb = NS_GET_G(aBackgroundColor);
74
int bb = NS_GET_B(aBackgroundColor);
76
int brightness = NS_GetBrightness(rb,gb,bb);
79
if (brightness < COLOR_DARK_THRESHOLD) {
80
f0 = COLOR_DARK_BS_FACTOR;
81
f1 = COLOR_DARK_TS_FACTOR;
82
} else if (brightness > COLOR_LIGHT_THRESHOLD) {
83
f0 = COLOR_LITE_BS_FACTOR;
84
f1 = COLOR_LITE_TS_FACTOR;
86
f0 = COLOR_DARK_BS_FACTOR +
88
(COLOR_LITE_BS_FACTOR - COLOR_DARK_BS_FACTOR) / MAX_COLOR);
89
f1 = COLOR_DARK_TS_FACTOR +
91
(COLOR_LITE_TS_FACTOR - COLOR_DARK_TS_FACTOR) / MAX_COLOR);
94
int r = rb - (f0 * rb / 100);
95
int g = gb - (f0 * gb / 100);
96
int b = bb - (f0 * bb / 100);
97
aResult[0] = NS_RGB(r, g, b);
98
if ((r == rb) && (g == gb) && (b == bb)) {
99
aResult[0] = (aBackgroundColor == BLACK) ? DARK_GRAY : BLACK;
102
r = rb + (f1 * (MAX_COLOR - rb) / 100);
103
if (r > 255) r = 255;
104
g = gb + (f1 * (MAX_COLOR - gb) / 100);
105
if (g > 255) g = 255;
106
b = bb + (f1 * (MAX_COLOR - bb) / 100);
107
if (b > 255) b = 255;
108
aResult[1] = NS_RGB(r, g, b);
109
if ((r == rb) && (g == gb) && (b == bb)) {
110
aResult[1] = (aBackgroundColor == WHITE) ? LIGHT_GRAY : WHITE;
114
void NS_GetSpecial3DColors(nscolor aResult[2],
115
nscolor aBackgroundColor,
116
nscolor aBorderColor)
122
PRUint8 rb = NS_GET_R(aBorderColor);
123
PRUint8 gb = NS_GET_G(aBorderColor);
124
PRUint8 bb = NS_GET_B(aBorderColor);
126
// This needs to be optimized.
127
// Calculating background brightness again and again is
128
// a waste of time!!!. Just calculate it only once.
131
PRUint8 red = NS_GET_R(aBackgroundColor);
132
PRUint8 green = NS_GET_G(aBackgroundColor);
133
PRUint8 blue = NS_GET_B(aBackgroundColor);
135
PRUint8 elementBrightness = NS_GetBrightness(rb,gb,bb);
136
PRUint8 backgroundBrightness = NS_GetBrightness(red, green, blue);
139
if (backgroundBrightness < COLOR_DARK_THRESHOLD) {
140
f0 = COLOR_DARK_BS_FACTOR;
141
f1 = COLOR_DARK_TS_FACTOR;
142
if(elementBrightness == MAX_DARKNESS)
144
rb = NS_GET_R(DARK_GRAY);
145
gb = NS_GET_G(DARK_GRAY);
146
bb = NS_GET_B(DARK_GRAY);
148
}else if (backgroundBrightness > COLOR_LIGHT_THRESHOLD) {
149
f0 = COLOR_LITE_BS_FACTOR;
150
f1 = COLOR_LITE_TS_FACTOR;
151
if(elementBrightness == MAX_BRIGHTNESS)
153
rb = NS_GET_R(LIGHT_GRAY);
154
gb = NS_GET_G(LIGHT_GRAY);
155
bb = NS_GET_B(LIGHT_GRAY);
158
f0 = COLOR_DARK_BS_FACTOR +
159
(backgroundBrightness *
160
(COLOR_LITE_BS_FACTOR - COLOR_DARK_BS_FACTOR) / MAX_COLOR);
161
f1 = COLOR_DARK_TS_FACTOR +
162
(backgroundBrightness *
163
(COLOR_LITE_TS_FACTOR - COLOR_DARK_TS_FACTOR) / MAX_COLOR);
167
r = rb - (f0 * rb / 100);
168
g = gb - (f0 * gb / 100);
169
b = bb - (f0 * bb / 100);
170
aResult[0] = NS_RGB(r, g, b);
172
r = rb + (f1 * (MAX_COLOR - rb) / 100);
173
g = gb + (f1 * (MAX_COLOR - gb) / 100);
174
b = bb + (f1 * (MAX_COLOR - bb) / 100);
175
aResult[1] = NS_RGB(r, g, b);
178
int NS_GetBrightness(PRUint8 aRed, PRUint8 aGreen, PRUint8 aBlue)
181
PRUint8 intensity = (aRed + aGreen + aBlue) / 3;
184
((RED_LUMINOSITY * aRed) / 100) +
185
((GREEN_LUMINOSITY * aGreen) / 100) +
186
((BLUE_LUMINOSITY * aBlue) / 100);
188
return ((intensity * INTENSITY_FACTOR) +
189
(luminosity * LUMINOSITY_FACTOR)) / 100;
192
// Function to convert RGB color space into the HSV colorspace
193
// Hue is the primary color defined from 0 to 359 degrees
194
// Saturation is defined from 0 to 255. The higher the number.. the deeper the color
195
// Value is the brightness of the color. 0 is black, 255 is white.
197
NS_RGB2HSV(nscolor aColor,PRUint16 &aHue,PRUint16 &aSat,PRUint16 &aValue)
200
PRInt16 delta,min,max,r1,b1,g1;
203
r = NS_GET_R(aColor);
204
g = NS_GET_G(aColor);
205
b = NS_GET_B(aColor);
222
// value or brightness will always be the max of all the colors(RGB)
225
aSat = (max!=0)?((delta*255)/max):0;
234
hue=(float)(g1-b1)/(float)delta;
235
} else if (g1==max) {
236
hue= 2.0f+(float)(b1-r1)/(float)delta;
238
hue = 4.0f+(float)(r1-g1)/(float)delta;
251
aHue = (PRUint16)hue;
254
// Function to convert HSV color space into the RGB colorspace
255
// Hue is the primary color defined from 0 to 359 degrees
256
// Saturation is defined from 0 to 255. The higher the number.. the deeper the color
257
// Value is the brightness of the color. 0 is black, 255 is white.
259
NS_HSV2RGB(nscolor &aColor,PRUint16 aHue,PRUint16 aSat,PRUint16 aValue)
261
PRUint16 r=0,g=0,b=0;
266
// achromatic color, no hue is defined
271
// hue in in degrees around the color wheel defined from
277
// we break the color wheel into 6 areas.. these
278
// areas define how the saturation and value define the color.
279
// reds behave differently than the blues
280
h = (double)aHue / 60.0;
281
i = (PRUint16) floor(h);
283
percent = ((double)aValue/255.0); // this needs to be a value from 0 to 1, so a percentage
284
// can be calculated of the saturation.
285
p = (PRUint16)(percent*(255-aSat));
286
q = (PRUint16)(percent*(255-(aSat*f)));
287
t = (PRUint16)(percent*(255-(aSat*(1.0-f))));
289
// i is guarenteed to never be larger than 5.
291
case 0: r = aValue; g = t; b = p;break;
292
case 1: r = q; g = aValue; b = p;break;
293
case 2: r = p; g = aValue; b = t;break;
294
case 3: r = p; g = q; b = aValue;break;
295
case 4: r = t; g = p; b = aValue;break;
296
case 5: r = aValue; g = p; b = q;break;
299
aColor = NS_RGB(r,g,b);