michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef nsColor_h___ michael@0: #define nsColor_h___ michael@0: michael@0: #include // for size_t michael@0: #include // for uint8_t, uint32_t michael@0: #include "gfxCore.h" // for NS_GFX_ michael@0: #include "nscore.h" // for nsAString michael@0: michael@0: class nsAString; michael@0: class nsString; michael@0: class nsCString; michael@0: michael@0: // A color is a 32 bit unsigned integer with four components: R, G, B michael@0: // and A. michael@0: typedef uint32_t nscolor; michael@0: michael@0: // Make a color out of r,g,b values. This assumes that the r,g,b values are michael@0: // properly constrained to 0-255. This also assumes that a is 255. michael@0: #define NS_RGB(_r,_g,_b) \ michael@0: ((nscolor) ((255 << 24) | ((_b)<<16) | ((_g)<<8) | (_r))) michael@0: michael@0: // Make a color out of r,g,b,a values. This assumes that the r,g,b,a michael@0: // values are properly constrained to 0-255. michael@0: #define NS_RGBA(_r,_g,_b,_a) \ michael@0: ((nscolor) (((_a) << 24) | ((_b)<<16) | ((_g)<<8) | (_r))) michael@0: michael@0: // Extract color components from nscolor michael@0: #define NS_GET_R(_rgba) ((uint8_t) ((_rgba) & 0xff)) michael@0: #define NS_GET_G(_rgba) ((uint8_t) (((_rgba) >> 8) & 0xff)) michael@0: #define NS_GET_B(_rgba) ((uint8_t) (((_rgba) >> 16) & 0xff)) michael@0: #define NS_GET_A(_rgba) ((uint8_t) (((_rgba) >> 24) & 0xff)) michael@0: michael@0: // Fast approximate division by 255. It has the property that michael@0: // for all 0 <= n <= 255*255, FAST_DIVIDE_BY_255(n) == n/255. michael@0: // But it only uses two adds and two shifts instead of an michael@0: // integer division (which is expensive on many processors). michael@0: // michael@0: // equivalent to target=v/255 michael@0: #define FAST_DIVIDE_BY_255(target,v) \ michael@0: PR_BEGIN_MACRO \ michael@0: unsigned tmp_ = v; \ michael@0: target = ((tmp_ << 8) + tmp_ + 255) >> 16; \ michael@0: PR_END_MACRO michael@0: michael@0: // Translate a hex string to a color. Return true if it parses ok, michael@0: // otherwise return false. michael@0: // This accepts only 3 or 6 digits michael@0: NS_GFX_(bool) NS_HexToRGB(const nsAString& aBuf, nscolor* aResult); michael@0: michael@0: // Compose one NS_RGB color onto another. The result is what michael@0: // you get if you draw aFG on top of aBG with operator OVER. michael@0: NS_GFX_(nscolor) NS_ComposeColors(nscolor aBG, nscolor aFG); michael@0: michael@0: // Translate a hex string to a color. Return true if it parses ok, michael@0: // otherwise return false. michael@0: // This version accepts 1 to 9 digits (missing digits are 0) michael@0: NS_GFX_(bool) NS_LooseHexToRGB(const nsString& aBuf, nscolor* aResult); michael@0: michael@0: // There is no function to translate a color to a hex string, because michael@0: // the hex-string syntax does not support transparency. michael@0: michael@0: // Translate a color name to a color. Return true if it parses ok, michael@0: // otherwise return false. michael@0: NS_GFX_(bool) NS_ColorNameToRGB(const nsAString& aBuf, nscolor* aResult); michael@0: michael@0: // Returns an array of all possible color names, and sets michael@0: // *aSizeArray to the size of that array. Do NOT call |free()| on this array. michael@0: NS_GFX_(const char * const *) NS_AllColorNames(size_t *aSizeArray); michael@0: michael@0: // function to convert from HSL color space to RGB color space michael@0: // the float parameters are all expected to be in the range 0-1 michael@0: NS_GFX_(nscolor) NS_HSL2RGB(float h, float s, float l); michael@0: michael@0: // Return a color name for the given nscolor. If there is no color michael@0: // name for it, returns null. If there are multiple possible color michael@0: // names for the given color, the first one in nsColorNameList.h michael@0: // (which is generally the first one in alphabetical order) will be michael@0: // returned. michael@0: NS_GFX_(const char*) NS_RGBToColorName(nscolor aColor); michael@0: michael@0: #endif /* nsColor_h___ */