1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 /* functions that manipulate colors */
9 #include "nsCSSColorUtils.h"
13 // Weird color computing code stolen from winfe which was stolen
14 // from the xfe which was written originally by Eric Bina. So there.
16 #define RED_LUMINOSITY 299
17 #define GREEN_LUMINOSITY 587
18 #define BLUE_LUMINOSITY 114
19 #define INTENSITY_FACTOR 25
20 #define LUMINOSITY_FACTOR 75
22 void NS_GetSpecial3DColors(nscolor aResult
[2], nscolor aBorderColor
) {
23 const float kDarkerScale
= 2.0f
/ 3.0f
;
25 uint8_t r
= NS_GET_R(aBorderColor
);
26 uint8_t g
= NS_GET_G(aBorderColor
);
27 uint8_t b
= NS_GET_B(aBorderColor
);
28 uint8_t a
= NS_GET_A(aBorderColor
);
29 if (r
== 0 && g
== 0 && b
== 0) {
31 aResult
[0] = NS_RGBA(76, 76, 76, a
);
33 aResult
[1] = NS_RGBA(178, 178, 178, a
);
37 aResult
[0] = NS_RGBA(uint8_t(r
* kDarkerScale
), uint8_t(g
* kDarkerScale
),
38 uint8_t(b
* kDarkerScale
), a
);
39 aResult
[1] = aBorderColor
;
42 int NS_GetBrightness(uint8_t aRed
, uint8_t aGreen
, uint8_t aBlue
) {
43 uint8_t intensity
= (aRed
+ aGreen
+ aBlue
) / 3;
45 uint8_t luminosity
= NS_GetLuminosity(NS_RGB(aRed
, aGreen
, aBlue
)) / 1000;
47 return ((intensity
* INTENSITY_FACTOR
) + (luminosity
* LUMINOSITY_FACTOR
)) /
51 int32_t NS_GetLuminosity(nscolor aColor
) {
52 // When aColor is not opaque, the perceived luminosity will depend
53 // on what color(s) aColor is ultimately drawn on top of, which we
55 NS_ASSERTION(NS_GET_A(aColor
) == 255,
56 "impossible to compute luminosity of a non-opaque color");
58 return (NS_GET_R(aColor
) * RED_LUMINOSITY
+
59 NS_GET_G(aColor
) * GREEN_LUMINOSITY
+
60 NS_GET_B(aColor
) * BLUE_LUMINOSITY
);
63 // Function to convert RGB color space into the HSV colorspace
64 // Hue is the primary color defined from 0 to 359 degrees
65 // Saturation is defined from 0 to 255. The higher the number.. the deeper
66 // the color Value is the brightness of the color. 0 is black, 255 is white.
67 void NS_RGB2HSV(nscolor aColor
, uint16_t& aHue
, uint16_t& aSat
,
68 uint16_t& aValue
, uint8_t& aAlpha
) {
70 int16_t delta
, min
, max
, r1
, b1
, g1
;
92 // value or brightness will always be the max of all the colors(RGB)
95 aSat
= (max
!= 0) ? ((delta
* 255) / max
) : 0;
104 hue
= (float)(g1
- b1
) / (float)delta
;
105 } else if (g1
== max
) {
106 hue
= 2.0f
+ (float)(b1
- r1
) / (float)delta
;
108 hue
= 4.0f
+ (float)(r1
- g1
) / (float)delta
;
121 aHue
= (uint16_t)hue
;
123 aAlpha
= NS_GET_A(aColor
);
126 // Function to convert HSV color space into the RGB colorspace
127 // Hue is the primary color defined from 0 to 359 degrees
128 // Saturation is defined from 0 to 255. The higher the number.. the deeper
129 // the color Value is the brightness of the color. 0 is black, 255 is white.
130 void NS_HSV2RGB(nscolor
& aColor
, uint16_t aHue
, uint16_t aSat
, uint16_t aValue
,
132 uint16_t r
= 0, g
= 0, b
= 0;
134 double h
, f
, percent
;
137 // achromatic color, no hue is defined
142 // hue in in degrees around the color wheel defined from
148 // we break the color wheel into 6 areas.. these
149 // areas define how the saturation and value define the color.
150 // reds behave differently than the blues
151 h
= (double)aHue
/ 60.0;
152 i
= (uint16_t)floor(h
);
154 percent
= ((double)aValue
/
155 255.0); // this needs to be a value from 0 to 1, so a percentage
156 // can be calculated of the saturation.
157 p
= (uint16_t)(percent
* (255 - aSat
));
158 q
= (uint16_t)(percent
* (255 - (aSat
* f
)));
159 t
= (uint16_t)(percent
* (255 - (aSat
* (1.0 - f
))));
161 // i is guaranteed to never be larger than 5.
195 aColor
= NS_RGBA(r
, g
, b
, aAlpha
);
198 #undef RED_LUMINOSITY
199 #undef GREEN_LUMINOSITY
200 #undef BLUE_LUMINOSITY
201 #undef INTENSITY_FACTOR
202 #undef LUMINOSITY_FACTOR