Line data Source code
1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* This Source Code Form is subject to the terms of the Mozilla Public
3 : * License, v. 2.0. If a copy of the MPL was not distributed with this
4 : * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 :
6 : #ifndef nsColor_h___
7 : #define nsColor_h___
8 :
9 : #include <stddef.h> // for size_t
10 : #include <stdint.h> // for uint8_t, uint32_t
11 : #include "nscore.h" // for nsAString
12 : #include "nsCoord.h" // for NSToIntRound
13 :
14 : class nsAString;
15 : class nsString;
16 :
17 : // A color is a 32 bit unsigned integer with four components: R, G, B
18 : // and A.
19 : typedef uint32_t nscolor;
20 :
21 : // Make a color out of r,g,b values. This assumes that the r,g,b values are
22 : // properly constrained to 0-255. This also assumes that a is 255.
23 : #define NS_RGB(_r,_g,_b) \
24 : ((nscolor) ((255 << 24) | ((_b)<<16) | ((_g)<<8) | (_r)))
25 :
26 : // Make a color out of r,g,b,a values. This assumes that the r,g,b,a
27 : // values are properly constrained to 0-255.
28 : #define NS_RGBA(_r,_g,_b,_a) \
29 : ((nscolor) (((_a) << 24) | ((_b)<<16) | ((_g)<<8) | (_r)))
30 :
31 : // Extract color components from nscolor
32 : #define NS_GET_R(_rgba) ((uint8_t) ((_rgba) & 0xff))
33 : #define NS_GET_G(_rgba) ((uint8_t) (((_rgba) >> 8) & 0xff))
34 : #define NS_GET_B(_rgba) ((uint8_t) (((_rgba) >> 16) & 0xff))
35 : #define NS_GET_A(_rgba) ((uint8_t) (((_rgba) >> 24) & 0xff))
36 :
37 : namespace mozilla {
38 :
39 : template<typename T>
40 177 : inline uint8_t ClampColor(T aColor)
41 : {
42 177 : if (aColor >= 255) {
43 93 : return 255;
44 : }
45 84 : if (aColor <= 0) {
46 12 : return 0;
47 : }
48 72 : return NSToIntRound(aColor);
49 : }
50 :
51 : } // namespace mozilla
52 :
53 : // Fast approximate division by 255. It has the property that
54 : // for all 0 <= n <= 255*255, FAST_DIVIDE_BY_255(n) == n/255.
55 : // But it only uses two adds and two shifts instead of an
56 : // integer division (which is expensive on many processors).
57 : //
58 : // equivalent to target=v/255
59 : #define FAST_DIVIDE_BY_255(target,v) \
60 : PR_BEGIN_MACRO \
61 : unsigned tmp_ = v; \
62 : target = ((tmp_ << 8) + tmp_ + 255) >> 16; \
63 : PR_END_MACRO
64 :
65 : enum class nsHexColorType : uint8_t {
66 : NoAlpha, // 3 or 6 digit hex colors only
67 : AllowAlpha, // 3, 4, 6, or 8 digit hex colors
68 : };
69 :
70 : // Translate a hex string to a color. Return true if it parses ok,
71 : // otherwise return false.
72 : // This accepts the number of digits specified by aType.
73 : bool
74 : NS_HexToRGBA(const nsAString& aBuf, nsHexColorType aType, nscolor* aResult);
75 :
76 : // Compose one NS_RGB color onto another. The result is what
77 : // you get if you draw aFG on top of aBG with operator OVER.
78 : nscolor NS_ComposeColors(nscolor aBG, nscolor aFG);
79 :
80 : namespace mozilla {
81 :
82 0 : inline uint32_t RoundingDivideBy255(uint32_t n)
83 : {
84 : // There is an approximate alternative: ((n << 8) + n + 32896) >> 16
85 : // But that is actually slower than this simple expression on a modern
86 : // machine with a modern compiler.
87 0 : return (n + 127) / 255;
88 : }
89 :
90 : // Blend one RGBA color with another based on a given ratio.
91 : // It is a linear interpolation on each channel with alpha premultipled.
92 : nscolor LinearBlendColors(nscolor aBg, nscolor aFg, uint_fast8_t aFgRatio);
93 :
94 : } // namespace mozilla
95 :
96 : // Translate a hex string to a color. Return true if it parses ok,
97 : // otherwise return false.
98 : // This version accepts 1 to 9 digits (missing digits are 0)
99 : bool NS_LooseHexToRGB(const nsString& aBuf, nscolor* aResult);
100 :
101 : // There is no function to translate a color to a hex string, because
102 : // the hex-string syntax does not support transparency.
103 :
104 : // Translate a color name to a color. Return true if it parses ok,
105 : // otherwise return false.
106 : bool NS_ColorNameToRGB(const nsAString& aBuf, nscolor* aResult);
107 :
108 : // Returns an array of all possible color names, and sets
109 : // *aSizeArray to the size of that array. Do NOT call |free()| on this array.
110 : const char * const * NS_AllColorNames(size_t *aSizeArray);
111 :
112 : // function to convert from HSL color space to RGB color space
113 : // the float parameters are all expected to be in the range 0-1
114 : nscolor NS_HSL2RGB(float h, float s, float l);
115 :
116 : // Return a color name for the given nscolor. If there is no color
117 : // name for it, returns null. If there are multiple possible color
118 : // names for the given color, the first one in nsColorNameList.h
119 : // (which is generally the first one in alphabetical order) will be
120 : // returned.
121 : const char* NS_RGBToColorName(nscolor aColor);
122 :
123 : #endif /* nsColor_h___ */
|