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