~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/layout/html/style/src/nsCSSColorUtils.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
4
 *
 
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/
 
9
 *
 
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
 
13
 * License.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *
 
24
 *
 
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.
 
36
 *
 
37
 * ***** END LICENSE BLOCK ***** */
 
38
 
 
39
#include "nsCSSColorUtils.h"
 
40
#include <math.h>
 
41
 
 
42
// Weird color computing code stolen from winfe which was stolen
 
43
// from the xfe which was written originally by Eric Bina. So there.
 
44
 
 
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
 
51
 
 
52
#define MAX_COLOR             255
 
53
#define COLOR_DARK_THRESHOLD  51
 
54
#define COLOR_LIGHT_THRESHOLD 204
 
55
 
 
56
#define COLOR_LITE_BS_FACTOR 45
 
57
#define COLOR_LITE_TS_FACTOR 70
 
58
 
 
59
#define COLOR_DARK_BS_FACTOR 30
 
60
#define COLOR_DARK_TS_FACTOR 50
 
61
 
 
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)
 
66
 
 
67
#define MAX_BRIGHTNESS  254
 
68
#define MAX_DARKNESS     0
 
69
 
 
70
void NS_Get3DColors(nscolor aResult[2], nscolor aBackgroundColor)
 
71
{
 
72
  int rb = NS_GET_R(aBackgroundColor);
 
73
  int gb = NS_GET_G(aBackgroundColor);
 
74
  int bb = NS_GET_B(aBackgroundColor);
 
75
  
 
76
  int brightness = NS_GetBrightness(rb,gb,bb);
 
77
 
 
78
  int f0, f1;
 
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;
 
85
  } else {
 
86
    f0 = COLOR_DARK_BS_FACTOR +
 
87
      (brightness *
 
88
       (COLOR_LITE_BS_FACTOR - COLOR_DARK_BS_FACTOR) / MAX_COLOR);
 
89
    f1 = COLOR_DARK_TS_FACTOR +
 
90
      (brightness *
 
91
       (COLOR_LITE_TS_FACTOR - COLOR_DARK_TS_FACTOR) / MAX_COLOR);
 
92
  }
 
93
 
 
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;
 
100
  }
 
101
 
 
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;
 
111
  }
 
112
}
 
113
 
 
114
void NS_GetSpecial3DColors(nscolor aResult[2],
 
115
                                                                                           nscolor aBackgroundColor,
 
116
                                                                                           nscolor aBorderColor)
 
117
{
 
118
 
 
119
  PRUint8 f0, f1;
 
120
  PRUint8 r, g, b;
 
121
 
 
122
  PRUint8 rb = NS_GET_R(aBorderColor);
 
123
  PRUint8 gb = NS_GET_G(aBorderColor);
 
124
  PRUint8 bb = NS_GET_B(aBorderColor);
 
125
 
 
126
  // This needs to be optimized.
 
127
  // Calculating background brightness again and again is 
 
128
  // a waste of time!!!. Just calculate it only once.
 
129
  // .....somehow!!!
 
130
 
 
131
  PRUint8 red = NS_GET_R(aBackgroundColor);
 
132
  PRUint8 green = NS_GET_G(aBackgroundColor);
 
133
  PRUint8 blue = NS_GET_B(aBackgroundColor);
 
134
  
 
135
  PRUint8 elementBrightness = NS_GetBrightness(rb,gb,bb);
 
136
  PRUint8 backgroundBrightness = NS_GetBrightness(red, green, blue);
 
137
 
 
138
 
 
139
  if (backgroundBrightness < COLOR_DARK_THRESHOLD) {
 
140
    f0 = COLOR_DARK_BS_FACTOR;
 
141
    f1 = COLOR_DARK_TS_FACTOR;
 
142
        if(elementBrightness == MAX_DARKNESS)
 
143
        {
 
144
       rb = NS_GET_R(DARK_GRAY);
 
145
       gb = NS_GET_G(DARK_GRAY);
 
146
       bb = NS_GET_B(DARK_GRAY);
 
147
        }
 
148
  }else if (backgroundBrightness > COLOR_LIGHT_THRESHOLD) {
 
149
    f0 = COLOR_LITE_BS_FACTOR;
 
150
    f1 = COLOR_LITE_TS_FACTOR;
 
151
        if(elementBrightness == MAX_BRIGHTNESS)
 
152
        {
 
153
       rb = NS_GET_R(LIGHT_GRAY);
 
154
       gb = NS_GET_G(LIGHT_GRAY);
 
155
       bb = NS_GET_B(LIGHT_GRAY);
 
156
        }
 
157
  }else {
 
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);
 
164
  }
 
165
  
 
166
  
 
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);
 
171
 
 
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);
 
176
}
 
177
 
 
178
int NS_GetBrightness(PRUint8 aRed, PRUint8 aGreen, PRUint8 aBlue)
 
179
{
 
180
 
 
181
  PRUint8 intensity = (aRed + aGreen + aBlue) / 3;
 
182
 
 
183
  PRUint8 luminosity =
 
184
    ((RED_LUMINOSITY * aRed) / 100) +
 
185
    ((GREEN_LUMINOSITY * aGreen) / 100) +
 
186
    ((BLUE_LUMINOSITY * aBlue) / 100);
 
187
 
 
188
  return ((intensity * INTENSITY_FACTOR) +
 
189
          (luminosity * LUMINOSITY_FACTOR)) / 100;
 
190
}
 
191
 
 
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.  
 
196
void
 
197
NS_RGB2HSV(nscolor aColor,PRUint16 &aHue,PRUint16 &aSat,PRUint16 &aValue)
 
198
{
 
199
PRUint8  r,g,b;
 
200
PRInt16  delta,min,max,r1,b1,g1;
 
201
float    hue;
 
202
 
 
203
  r = NS_GET_R(aColor);
 
204
  g = NS_GET_G(aColor);
 
205
  b = NS_GET_B(aColor);
 
206
 
 
207
  if (r>g) {
 
208
    max = r;
 
209
    min = g;
 
210
  } else {
 
211
    max = g;
 
212
    min = r;
 
213
  }
 
214
 
 
215
  if (b>max) {
 
216
    max = b;
 
217
  }
 
218
  if (b<min) {
 
219
    min = b;
 
220
  }
 
221
 
 
222
  // value or brightness will always be the max of all the colors(RGB)
 
223
  aValue = max;   
 
224
  delta = max-min;
 
225
  aSat = (max!=0)?((delta*255)/max):0;
 
226
  r1 = r;
 
227
  b1 = b;
 
228
  g1 = g;
 
229
 
 
230
  if (aSat==0) {
 
231
    hue = 1000;
 
232
  } else {
 
233
    if(r==max){
 
234
      hue=(float)(g1-b1)/(float)delta;
 
235
    } else if (g1==max) {
 
236
      hue= 2.0f+(float)(b1-r1)/(float)delta;
 
237
    } else { 
 
238
      hue = 4.0f+(float)(r1-g1)/(float)delta;
 
239
    }
 
240
  }
 
241
 
 
242
  if(hue<999) {
 
243
    hue*=60;
 
244
    if(hue<0){
 
245
      hue+=360;
 
246
    }
 
247
  } else {
 
248
    hue=0;
 
249
  }
 
250
 
 
251
  aHue = (PRUint16)hue;
 
252
}
 
253
 
 
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.  
 
258
void
 
259
NS_HSV2RGB(nscolor &aColor,PRUint16 aHue,PRUint16 aSat,PRUint16 aValue)
 
260
{
 
261
PRUint16  r=0,g=0,b=0;
 
262
PRUint16  i,p,q,t;
 
263
double    h,f,percent;
 
264
 
 
265
  if ( aSat == 0 ){
 
266
    // achromatic color, no hue is defined
 
267
    r = aValue;
 
268
    g = aValue;
 
269
    b = aValue;
 
270
  } else {
 
271
    // hue in in degrees around the color wheel defined from
 
272
    // 0 to 360 degrees.  
 
273
    if (aHue >= 360) {
 
274
      aHue = 0;
 
275
    }
 
276
 
 
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);
 
282
    f = h-(double)i;
 
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))));
 
288
 
 
289
    // i is guarenteed to never be larger than 5.
 
290
    switch(i){
 
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;
 
297
    }
 
298
  }
 
299
  aColor = NS_RGB(r,g,b);
 
300
}