gfx/src/nsColor.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/src/nsColor.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,86 @@
     1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#ifndef nsColor_h___
    1.10 +#define nsColor_h___
    1.11 +
    1.12 +#include <stddef.h>                     // for size_t
    1.13 +#include <stdint.h>                     // for uint8_t, uint32_t
    1.14 +#include "gfxCore.h"                    // for NS_GFX_
    1.15 +#include "nscore.h"                     // for nsAString
    1.16 +
    1.17 +class nsAString;
    1.18 +class nsString;
    1.19 +class nsCString;
    1.20 +
    1.21 +// A color is a 32 bit unsigned integer with four components: R, G, B
    1.22 +// and A.
    1.23 +typedef uint32_t nscolor;
    1.24 +
    1.25 +// Make a color out of r,g,b values. This assumes that the r,g,b values are
    1.26 +// properly constrained to 0-255. This also assumes that a is 255.
    1.27 +#define NS_RGB(_r,_g,_b) \
    1.28 +  ((nscolor) ((255 << 24) | ((_b)<<16) | ((_g)<<8) | (_r)))
    1.29 +
    1.30 +// Make a color out of r,g,b,a values. This assumes that the r,g,b,a
    1.31 +// values are properly constrained to 0-255.
    1.32 +#define NS_RGBA(_r,_g,_b,_a) \
    1.33 +  ((nscolor) (((_a) << 24) | ((_b)<<16) | ((_g)<<8) | (_r)))
    1.34 +
    1.35 +// Extract color components from nscolor
    1.36 +#define NS_GET_R(_rgba) ((uint8_t) ((_rgba) & 0xff))
    1.37 +#define NS_GET_G(_rgba) ((uint8_t) (((_rgba) >> 8) & 0xff))
    1.38 +#define NS_GET_B(_rgba) ((uint8_t) (((_rgba) >> 16) & 0xff))
    1.39 +#define NS_GET_A(_rgba) ((uint8_t) (((_rgba) >> 24) & 0xff))
    1.40 +
    1.41 +// Fast approximate division by 255. It has the property that
    1.42 +// for all 0 <= n <= 255*255, FAST_DIVIDE_BY_255(n) == n/255.
    1.43 +// But it only uses two adds and two shifts instead of an 
    1.44 +// integer division (which is expensive on many processors).
    1.45 +//
    1.46 +// equivalent to target=v/255
    1.47 +#define FAST_DIVIDE_BY_255(target,v)               \
    1.48 +  PR_BEGIN_MACRO                                   \
    1.49 +    unsigned tmp_ = v;                             \
    1.50 +    target = ((tmp_ << 8) + tmp_ + 255) >> 16;     \
    1.51 +  PR_END_MACRO
    1.52 +
    1.53 +// Translate a hex string to a color. Return true if it parses ok,
    1.54 +// otherwise return false.
    1.55 +// This accepts only 3 or 6 digits
    1.56 +NS_GFX_(bool) NS_HexToRGB(const nsAString& aBuf, nscolor* aResult);
    1.57 +
    1.58 +// Compose one NS_RGB color onto another. The result is what
    1.59 +// you get if you draw aFG on top of aBG with operator OVER.
    1.60 +NS_GFX_(nscolor) NS_ComposeColors(nscolor aBG, nscolor aFG);
    1.61 +
    1.62 +// Translate a hex string to a color. Return true if it parses ok,
    1.63 +// otherwise return false.
    1.64 +// This version accepts 1 to 9 digits (missing digits are 0)
    1.65 +NS_GFX_(bool) NS_LooseHexToRGB(const nsString& aBuf, nscolor* aResult);
    1.66 +
    1.67 +// There is no function to translate a color to a hex string, because
    1.68 +// the hex-string syntax does not support transparency.
    1.69 +
    1.70 +// Translate a color name to a color. Return true if it parses ok,
    1.71 +// otherwise return false.
    1.72 +NS_GFX_(bool) NS_ColorNameToRGB(const nsAString& aBuf, nscolor* aResult);
    1.73 +
    1.74 +// Returns an array of all possible color names, and sets
    1.75 +// *aSizeArray to the size of that array. Do NOT call |free()| on this array.
    1.76 +NS_GFX_(const char * const *) NS_AllColorNames(size_t *aSizeArray);
    1.77 +
    1.78 +// function to convert from HSL color space to RGB color space
    1.79 +// the float parameters are all expected to be in the range 0-1
    1.80 +NS_GFX_(nscolor) NS_HSL2RGB(float h, float s, float l);
    1.81 +
    1.82 +// Return a color name for the given nscolor.  If there is no color
    1.83 +// name for it, returns null.  If there are multiple possible color
    1.84 +// names for the given color, the first one in nsColorNameList.h
    1.85 +// (which is generally the first one in alphabetical order) will be
    1.86 +// returned.
    1.87 +NS_GFX_(const char*) NS_RGBToColorName(nscolor aColor);
    1.88 +
    1.89 +#endif /* nsColor_h___ */

mercurial