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/. */
9 #include <stddef.h> // for size_t
10 #include <stdint.h> // for uint8_t, uint32_t
11 #include "gfxCore.h" // for NS_GFX_
12 #include "nscore.h" // for nsAString
18 // A color is a 32 bit unsigned integer with four components: R, G, B
20 typedef uint32_t nscolor
;
22 // Make a color out of r,g,b values. This assumes that the r,g,b values are
23 // properly constrained to 0-255. This also assumes that a is 255.
24 #define NS_RGB(_r,_g,_b) \
25 ((nscolor) ((255 << 24) | ((_b)<<16) | ((_g)<<8) | (_r)))
27 // Make a color out of r,g,b,a values. This assumes that the r,g,b,a
28 // values are properly constrained to 0-255.
29 #define NS_RGBA(_r,_g,_b,_a) \
30 ((nscolor) (((_a) << 24) | ((_b)<<16) | ((_g)<<8) | (_r)))
32 // Extract color components from nscolor
33 #define NS_GET_R(_rgba) ((uint8_t) ((_rgba) & 0xff))
34 #define NS_GET_G(_rgba) ((uint8_t) (((_rgba) >> 8) & 0xff))
35 #define NS_GET_B(_rgba) ((uint8_t) (((_rgba) >> 16) & 0xff))
36 #define NS_GET_A(_rgba) ((uint8_t) (((_rgba) >> 24) & 0xff))
38 // Fast approximate division by 255. It has the property that
39 // for all 0 <= n <= 255*255, FAST_DIVIDE_BY_255(n) == n/255.
40 // But it only uses two adds and two shifts instead of an
41 // integer division (which is expensive on many processors).
43 // equivalent to target=v/255
44 #define FAST_DIVIDE_BY_255(target,v) \
47 target = ((tmp_ << 8) + tmp_ + 255) >> 16; \
50 // Translate a hex string to a color. Return true if it parses ok,
51 // otherwise return false.
52 // This accepts only 3 or 6 digits
53 NS_GFX_(bool) NS_HexToRGB(const nsAString
& aBuf
, nscolor
* aResult
);
55 // Compose one NS_RGB color onto another. The result is what
56 // you get if you draw aFG on top of aBG with operator OVER.
57 NS_GFX_(nscolor
) NS_ComposeColors(nscolor aBG
, nscolor aFG
);
59 // Translate a hex string to a color. Return true if it parses ok,
60 // otherwise return false.
61 // This version accepts 1 to 9 digits (missing digits are 0)
62 NS_GFX_(bool) NS_LooseHexToRGB(const nsString
& aBuf
, nscolor
* aResult
);
64 // There is no function to translate a color to a hex string, because
65 // the hex-string syntax does not support transparency.
67 // Translate a color name to a color. Return true if it parses ok,
68 // otherwise return false.
69 NS_GFX_(bool) NS_ColorNameToRGB(const nsAString
& aBuf
, nscolor
* aResult
);
71 // Returns an array of all possible color names, and sets
72 // *aSizeArray to the size of that array. Do NOT call |free()| on this array.
73 NS_GFX_(const char * const *) NS_AllColorNames(size_t *aSizeArray
);
75 // function to convert from HSL color space to RGB color space
76 // the float parameters are all expected to be in the range 0-1
77 NS_GFX_(nscolor
) NS_HSL2RGB(float h
, float s
, float l
);
79 // Return a color name for the given nscolor. If there is no color
80 // name for it, returns null. If there are multiple possible color
81 // names for the given color, the first one in nsColorNameList.h
82 // (which is generally the first one in alphabetical order) will be
84 NS_GFX_(const char*) NS_RGBToColorName(nscolor aColor
);
86 #endif /* nsColor_h___ */