Back to index

lightning-sunbird  0.9+nobinonly
nsColor.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsColor_h___
00039 #define nsColor_h___
00040 
00041 #include "gfxCore.h"
00042 
00043 class nsAString;
00044 class nsString;
00045 class nsCString;
00046 
00047 // A color is a 32 bit unsigned integer with four components: R, G, B
00048 // and A.
00049 typedef PRUint32 nscolor;
00050 
00051 // Make a color out of r,g,b values. This assumes that the r,g,b values are
00052 // properly constrained to 0-255. This also assumes that a is 255.
00053 #define NS_RGB(_r,_g,_b) \
00054   ((nscolor) ((255 << 24) | ((_b)<<16) | ((_g)<<8) | (_r)))
00055 
00056 // Make a color out of r,g,b,a values. This assumes that the r,g,b,a
00057 // values are properly constrained to 0-255.
00058 #define NS_RGBA(_r,_g,_b,_a) \
00059   ((nscolor) (((_a) << 24) | ((_b)<<16) | ((_g)<<8) | (_r)))
00060 
00061 // Extract color components from nscolor
00062 #define NS_GET_R(_rgba) ((PRUint8) ((_rgba) & 0xff))
00063 #define NS_GET_G(_rgba) ((PRUint8) (((_rgba) >> 8) & 0xff))
00064 #define NS_GET_B(_rgba) ((PRUint8) (((_rgba) >> 16) & 0xff))
00065 #define NS_GET_A(_rgba) ((PRUint8) (((_rgba) >> 24) & 0xff))
00066 
00067 // Fast approximate division by 255. It has the property that
00068 // for all 0 <= n <= 255*255, FAST_DIVIDE_BY_255(n) == n/255.
00069 // But it only uses two adds and two shifts instead of an 
00070 // integer division (which is expensive on many processors).
00071 //
00072 // equivalent to target=v/255
00073 #define FAST_DIVIDE_BY_255(target,v)               \
00074   PR_BEGIN_MACRO                                   \
00075     unsigned tmp_ = v;                             \
00076     target = ((tmp_ << 8) + tmp_ + 255) >> 16;     \
00077   PR_END_MACRO
00078 
00079 // Blending macro
00080 //
00081 // equivalent to target=(bg*(255-alpha)+fg*alpha)/255
00082 #define MOZ_BLEND(target, bg, fg, alpha) \
00083         FAST_DIVIDE_BY_255(target, (bg)*(255-(alpha)) + (fg)*(alpha))
00084 
00085 // Translate a hex string to a color. Return true if it parses ok,
00086 // otherwise return false.
00087 // This accepts only 3, 6 or 9 digits
00088 extern "C" NS_GFX_(PRBool) NS_HexToRGB(const nsString& aBuf, nscolor* aResult);
00089 extern "C" NS_GFX_(PRBool) NS_ASCIIHexToRGB(const nsCString& aBuf,
00090                                             nscolor* aResult);
00091 
00092 // Compose one NS_RGB color onto another. The result is what you get
00093 // if you draw aBG onto RGBA(0,0,0,0) and then aFG on top of that,
00094 // with operator OVER.
00095 extern "C" NS_GFX_(nscolor) NS_ComposeColors(nscolor aBG, nscolor aFG);
00096 
00097 // Translate a hex string to a color. Return true if it parses ok,
00098 // otherwise return false.
00099 // This version accepts 1 to 9 digits (missing digits are 0)
00100 extern "C" NS_GFX_(PRBool) NS_LooseHexToRGB(const nsString& aBuf, nscolor* aResult);
00101 
00102 // Translate a color to a hex string and prepend a '#'.
00103 // The returned string is always 7 characters including a '#' character.
00104 extern "C" NS_GFX_(void) NS_RGBToHex(nscolor aColor, nsAString& aResult);
00105 extern "C" NS_GFX_(void) NS_RGBToASCIIHex(nscolor aColor,
00106                                           nsCString& aResult);
00107 
00108 // Translate a color name to a color. Return true if it parses ok,
00109 // otherwise return false.
00110 extern "C" NS_GFX_(PRBool) NS_ColorNameToRGB(const nsAString& aBuf, nscolor* aResult);
00111 
00112 // Special method to brighten a Color and have it shift to white when
00113 // fully saturated.
00114 extern "C" NS_GFX_(nscolor) NS_BrightenColor(nscolor inColor);
00115 
00116 // Special method to darken a Color and have it shift to black when
00117 // darkest component underflows
00118 extern "C" NS_GFX_(nscolor) NS_DarkenColor(nscolor inColor);
00119 
00120 // function to convert from HSL color space to RGB color space
00121 // the float parameters are all expected to be in the range 0-1
00122 extern "C" NS_GFX_(nscolor) NS_HSL2RGB(float h, float s, float l);
00123 
00124 #endif /* nsColor_h___ */