Eneboo - Documentación para desarrolladores
Tipos públicos | Métodos públicos | Métodos públicos estáticos | Funciones relacionadas
Referencia de la Clase QColor

The QColor class provides colors based on RGB or HSV values. Más...

#include <qcolor.h>

Lista de todos los miembros.

Tipos públicos

enum  Spec { Rgb, Hsv, Rgb, Hsv }
enum  { Dirt = 0x44495254, Invalid = 0x49000000 }
enum  Spec { Rgb, Hsv, Rgb, Hsv }
enum  { Dirt = 0x44495254, Invalid = 0x49000000 }

Métodos públicos

 QColor ()
 QColor (int r, int g, int b)
 QColor (int x, int y, int z, Spec)
 QColor (QRgb rgb, uint pixel=0xffffffff)
 QColor (const QString &name)
 QColor (const char *name)
 QColor (const QColor &)
QColoroperator= (const QColor &)
bool isValid () const
bool isDirty () const
QString name () const
void setNamedColor (const QString &name)
QRgb rgb () const
void setRgb (int r, int g, int b)
void setRgb (QRgb rgb)
void getRgb (int *r, int *g, int *b) const
void rgb (int *r, int *g, int *b) const
int red () const
int green () const
int blue () const
void setHsv (int h, int s, int v)
void getHsv (int *h, int *s, int *v) const
void hsv (int *h, int *s, int *v) const
void getHsv (int &h, int &s, int &v) const
QColor light (int f=150) const
QColor dark (int f=200) const
bool operator== (const QColor &c) const
bool operator!= (const QColor &c) const
uint alloc ()
uint pixel () const
 QColor ()
 QColor (int r, int g, int b)
 QColor (int x, int y, int z, Spec)
 QColor (QRgb rgb, uint pixel=0xffffffff)
 QColor (const QString &name)
 QColor (const char *name)
 QColor (const QColor &)
QColoroperator= (const QColor &)
bool isValid () const
bool isDirty () const
QString name () const
void setNamedColor (const QString &name)
QRgb rgb () const
void setRgb (int r, int g, int b)
void setRgb (QRgb rgb)
void getRgb (int *r, int *g, int *b) const
void rgb (int *r, int *g, int *b) const
int red () const
int green () const
int blue () const
void setHsv (int h, int s, int v)
void getHsv (int *h, int *s, int *v) const
void hsv (int *h, int *s, int *v) const
void getHsv (int &h, int &s, int &v) const
QColor light (int f=150) const
QColor dark (int f=200) const
bool operator== (const QColor &c) const
bool operator!= (const QColor &c) const
uint alloc ()
uint pixel () const

Métodos públicos estáticos

static int maxColors ()
static int numBitPlanes ()
static int enterAllocContext ()
static void leaveAllocContext ()
static int currentAllocContext ()
static void destroyAllocContext (int)
static void initialize ()
static void cleanup ()
static QStringList colorNames ()
static int maxColors ()
static int numBitPlanes ()
static int enterAllocContext ()
static void leaveAllocContext ()
static int currentAllocContext ()
static void destroyAllocContext (int)
static void initialize ()
static void cleanup ()
static QStringList colorNames ()

Funciones relacionadas

(Observar que estas no son funciones miembro.)

QDataStreamoperator<< (QDataStream &s, const QColor &c)
QDataStreamoperator>> (QDataStream &s, QColor &c)
int qRed (QRgb rgb)
int qGreen (QRgb rgb)
int qBlue (QRgb rgb)
int qAlpha (QRgb rgba)
QRgb qRgb (int r, int g, int b)
QRgb qRgba (int r, int g, int b, int a)
int qGray (int r, int g, int b)
int qGray

Descripción detallada

The QColor class provides colors based on RGB or HSV values.

A color is normally specified in terms of RGB (red, green and blue) components, but it is also possible to specify HSV (hue, saturation and value) or set a color name (the names are copied from from the X11 color database).

In addition to the RGB value, a QColor also has a pixel value and a validity. The pixel value is used by the underlying window system to refer to a color. It can be thought of as an index into the display hardware's color table.

The validity (isValid()) indicates whether the color is legal at all. For example, a RGB color with RGB values out of range is illegal. For performance reasons, QColor mostly disregards illegal colors. The result of using an invalid color is unspecified and will usually be surprising.

There are 19 predefined QColor objects: white, black, red, darkRed, green, darkGreen, blue, darkBlue, cyan, darkCyan, magenta, darkMagenta, yellow, darkYellow, gray, darkGray, lightGray, color0 and color1, accessible as members of the Qt namespace (ie. Qt::red).

qt-colors.png Qt Colors

The colors color0 (zero pixel value) and color1 (non-zero pixel value) are special colors for drawing in bitmaps. Painting with color0 sets the bitmap bits to 0 (transparent, i.e. background), and painting with color1 sets the bits to 1 (opaque, i.e. foreground).

The QColor class has an efficient, dynamic color allocation strategy. A color is normally allocated the first time it is used (lazy allocation), that is, whenever the pixel() function is called. The following steps are taken to allocate a color. If, at any point, a suitable color is found then the appropriate pixel value is returned and the subsequent steps are not taken:

1 Is the pixel value valid? If it is, just return it; otherwise, allocate a pixel value. Check an internal hash table to see if we allocated an equal RGB value earlier. If we did, set the corresponding pixel value for the color and return it. Try to allocate the RGB value. If we succeed, we get a pixel value that we save in the internal table with the RGB value. Return the pixel value. The color could not be allocated. Find the closest matching color, save it in the internal table, and return it.

A color can be set by passing setNamedColor() an RGB string like "#112233", or a color name, e.g. "blue". The names are taken from X11's rgb.txt database but can also be used under Windows. To get a lighter or darker color use light() and dark() respectively. Colors can also be set using setRgb() and setHsv(). The color components can be accessed in one go with rgb() and hsv(), or individually with red(), green() and blue().

Use maxColors() and numBitPlanes() to determine the maximum number of colors and the number of bit planes supported by the underlying window system,

If you need to allocate many colors temporarily, for example in an image viewer application, enterAllocContext(), leaveAllocContext() and destroyAllocContext() will prove useful.


Documentación de las enumeraciones miembro de la clase

anonymous enum
Valores de enumeraciones:
Dirt 
Invalid 
anonymous enum
Valores de enumeraciones:
Dirt 
Invalid 
Valores de enumeraciones:
Rgb 
Hsv 
Rgb 
Hsv 

The type of color specified, either RGB or HSV, e.g. in the {QColor::QColor( x, y, z, colorSpec)} constructor.

Rgb Hsv

Valores de enumeraciones:
Rgb 
Hsv 
Rgb 
Hsv 

Documentación del constructor y destructor

QColor::QColor ( ) [inline]

Constructs an invalid color with the RGB value (0, 0, 0). An invalid color is a color that is not properly set up for the underlying window system.

The alpha value of an invalid color is unspecified.

Ver también:
isValid()
QColor::QColor ( int  r,
int  g,
int  b 
) [inline]

Constructs a color with the RGB value r, g, b, in the same way as setRgb().

The color is left invalid if any or the arguments are illegal.

Ver también:
setRgb()
QColor::QColor ( int  x,
int  y,
int  z,
Spec  colorSpec 
)

Constructs a color with the RGB or HSV value x, y, z.

The arguments are an RGB value if colorSpec is QColor::Rgb. x (red), y (green), and z (blue). All of them must be in the range 0-255.

The arguments are an HSV value if colorSpec is QColor::Hsv. x (hue) must be -1 for achromatic colors and 0-359 for chromatic colors; y (saturation) and z (value) must both be in the range 0-255.

Ver también:
setRgb(), setHsv()
QColor::QColor ( QRgb  rgb,
uint  pixel = 0xffffffff 
)

Constructs a color with the RGB value rgb and a custom pixel value pixel.

If pixel == 0xffffffff (the default), then the color uses the RGB value in a standard way. If pixel is something else, then the pixel value is set directly to pixel, skipping the normal allocation procedure.

QColor::QColor ( const QString name)

Constructs a named color in the same way as setNamedColor() using name name.

The color is left invalid if name cannot be parsed.

Ver también:
setNamedColor()
QColor::QColor ( const char *  name)

Constructs a named color in the same way as setNamedColor() using name name.

The color is left invalid if name cannot be parsed.

Ver también:
setNamedColor()
QColor::QColor ( const QColor c)

Constructs a color that is a copy of c.

QColor::QColor ( )
QColor::QColor ( int  r,
int  g,
int  b 
)
QColor::QColor ( int  x,
int  y,
int  z,
Spec   
)
QColor::QColor ( QRgb  rgb,
uint  pixel = 0xffffffff 
)
QColor::QColor ( const QString name)
QColor::QColor ( const char *  name)
QColor::QColor ( const QColor )

Documentación de las funciones miembro

uint QColor::alloc ( )

Allocates the RGB color and returns the pixel value.

Allocating a color means to obtain a pixel value from the RGB specification. The pixel value is an index into the global color table, but should be considered an arbitrary platform-dependent value.

The pixel() function calls alloc() if necessary, so in general you don't need to call this function.

Ver también:
setLazyAlloc(), enterAllocContext()

Allocates the RGB color and returns the pixel value.

Allocating a color means to obtain a pixel value from the RGB specification. The pixel value is an index into the global color table, but should be considered an arbitrary platform-dependent value.

The pixel() function calls alloc() if necessary, so in general you don't need to call this function.

Ver también:
enterAllocContext()
uint QColor::alloc ( )
int QColor::blue ( ) const [inline]

Returns the B (blue) component of the RGB value.

int QColor::blue ( ) const
void QColor::cleanup ( ) [static]

Internal clean up required for QColor. This function is called from the QApplication destructor.

Ver también:
initialize()
static void QColor::cleanup ( ) [static]
QStringList QColor::colorNames ( ) [static]

Returns a QStringList containing the color names Qt knows about.

static QStringList QColor::colorNames ( ) [static]
int QColor::currentAllocContext ( ) [static]

Returns the current color allocation context.

The default context is 0.

Ver también:
enterAllocContext(), leaveAllocContext()
static int QColor::currentAllocContext ( ) [static]
QColor QColor::dark ( int  factor = 200) const

Returns a darker (or lighter) color, but does not change this object.

Returns a darker color if factor is greater than 100. Setting factor to 300 returns a color that has one-third the brightness.

Returns a lighter color if factor is less than 100. We recommend using lighter() for this purpose. If factor is 0 or negative, the return value is unspecified.

(This function converts the current RGB color to HSV, divides V by factor and converts back to RGB.)

Ver también:
light()
QColor QColor::dark ( int  f = 200) const
void QColor::destroyAllocContext ( int  context) [static]

Destroys a color allocation context, context.

This function deallocates all colors that were allocated in the specified context. If context == -1, it frees up all colors that the application has allocated. If context == -2, it frees up all colors that the application has allocated, except those in the default context.

The function does nothing for true color displays.

Ver también:
enterAllocContext(), alloc()
static void QColor::destroyAllocContext ( int  ) [static]
int QColor::enterAllocContext ( ) [static]

Enters a color allocation context and returns a nonzero unique identifier.

Color allocation contexts are useful for programs that need to allocate many colors and throw them away later, like image viewers. The allocation context functions work for true color displays as well as colormap display, except that QColor::destroyAllocContext() does nothing for true color.

Example:

    QPixmap loadPixmap( QString fileName )
    {
 static int alloc_context = 0;
 if ( alloc_context )
     QColor::destroyAllocContext( alloc_context );
 alloc_context = QColor::enterAllocContext();
 QPixmap pm( fileName );
 QColor::leaveAllocContext();
 return pm;
    }

The example code loads a pixmap from file. It frees up all colors that were allocated the last time loadPixmap() was called.

The initial/default context is 0. Qt keeps a list of colors associated with their allocation contexts. You can call destroyAllocContext() to get rid of all colors that were allocated in a specific context.

Calling enterAllocContext() enters an allocation context. The allocation context lasts until you call leaveAllocContext(). QColor has an internal stack of allocation contexts. Each call to enterAllocContex() must have a corresponding leaveAllocContext().

      // context 0 active
    int c1 = QColor::enterAllocContext(); // enter context c1
      // context c1 active
    int c2 = QColor::enterAllocContext(); // enter context c2
      // context c2 active
    QColor::leaveAllocContext();  // leave context c2
      // context c1 active
    QColor::leaveAllocContext();  // leave context c1
      // context 0 active
      // Now, free all colors that were allocated in context c2
    QColor::destroyAllocContext( c2 );

You may also want to set the application's color specification. See QApplication::setColorSpec() for more information.

Ver también:
leaveAllocContext(), currentAllocContext(), destroyAllocContext(), QApplication::setColorSpec()

Enters a color allocation context and returns a non-zero unique identifier.

Color allocation contexts are useful for programs that need to allocate many colors and throw them away later, like image viewers. The allocation context functions work for true color displays as well as for colormap displays, except that QColor::destroyAllocContext() does nothing for true color.

Example:

    QPixmap loadPixmap( QString fileName )
    {
        static int alloc_context = 0;
        if ( alloc_context )
            QColor::destroyAllocContext( alloc_context );
        alloc_context = QColor::enterAllocContext();
        QPixmap pm( fileName );
        QColor::leaveAllocContext();
        return pm;
    }

The example code loads a pixmap from file. It frees up all colors that were allocated the last time loadPixmap() was called.

The initial/default context is 0. Qt keeps a list of colors associated with their allocation contexts. You can call destroyAllocContext() to get rid of all colors that were allocated in a specific context.

Calling enterAllocContext() enters an allocation context. The allocation context lasts until you call leaveAllocContext(). QColor has an internal stack of allocation contexts. Each call to enterAllocContex() must have a corresponding leaveAllocContext().

        // context 0 active
    int c1 = QColor::enterAllocContext();    // enter context c1
        // context c1 active
    int c2 = QColor::enterAllocContext();    // enter context c2
        // context c2 active
    QColor::leaveAllocContext();             // leave context c2
        // context c1 active
    QColor::leaveAllocContext();             // leave context c1
        // context 0 active
        // Now, free all colors that were allocated in context c2
    QColor::destroyAllocContext( c2 );

You may also want to set the application's color specification. See QApplication::setColorSpec() for more information.

Ver también:
leaveAllocContext(), currentAllocContext(), destroyAllocContext(), QApplication::setColorSpec()
static int QColor::enterAllocContext ( ) [static]
void QColor::getHsv ( int h,
int s,
int v 
) const [inline]
void QColor::getHsv ( int h,
int s,
int v 
) const [inline]
void QColor::getHsv ( int h,
int s,
int v 
) const [inline]

Returns the current RGB value as HSV. The contents of the h, s and v pointers are set to the HSV values. If any of the three pointers are null, the function does nothing.

The hue (which h points to) is set to -1 if the color is achromatic.

Atención:
Colors are stored internally as RGB values, so getHSv() may return slightly different values to those set by setHsv().
Ver también:
setHsv(), rgb()
void QColor::getHsv ( int h,
int s,
int v 
) const [inline]
void QColor::getRgb ( int r,
int g,
int b 
) const [inline]
void QColor::getRgb ( int r,
int g,
int b 
) const [inline]

Sets the contents pointed to by r, g and b to the red, green and blue components of the RGB value respectively. The value range for a component is 0..255.

Ver también:
rgb(), setRgb(), getHsv()
int QColor::green ( ) const
int QColor::green ( ) const [inline]

Returns the G (green) component of the RGB value.

void QColor::hsv ( int h,
int s,
int v 
) const

Use getHsv() instead.

void QColor::hsv ( int h,
int s,
int v 
) const
static void QColor::initialize ( ) [static]
void QColor::initialize ( ) [static]

Internal initialization required for QColor. This function is called from the QApplication constructor.

Ver también:
cleanup()
bool QColor::isDirty ( ) const
bool QColor::isDirty ( ) const
bool QColor::isValid ( ) const
bool QColor::isValid ( void  ) const [inline]

Returns FALSE if the color is invalid, i.e. it was constructed using the default constructor; otherwise returns TRUE.

static void QColor::leaveAllocContext ( ) [static]
void QColor::leaveAllocContext ( ) [static]

Leaves a color allocation context.

See enterAllocContext() for a detailed explanation.

Ver también:
enterAllocContext(), currentAllocContext()
QColor QColor::light ( int  factor = 150) const

Returns a lighter (or darker) color, but does not change this object.

Returns a lighter color if factor is greater than 100. Setting factor to 150 returns a color that is 50% brighter.

Returns a darker color if factor is less than 100. We recommend using dark() for this purpose. If factor is 0 or negative, the return value is unspecified.

(This function converts the current RGB color to HSV, multiplies V by factor, and converts the result back to RGB.)

Ver también:
dark()
QColor QColor::light ( int  f = 150) const
int QColor::maxColors ( ) [static]

Returns the maximum number of colors supported by the underlying window system.

Returns the maximum number of colors supported by the underlying window system if the window system uses a palette.

Otherwise returns -1. Use numBitPlanes() to calculate the available colors in that case.

static int QColor::maxColors ( ) [static]
QString QColor::name ( ) const

Returns the name of the color in the format "#RRGGBB", i.e. a "#" character followed by three two-digit hexadecimal numbers.

Ver también:
setNamedColor()
QString QColor::name ( ) const
int QColor::numBitPlanes ( ) [static]

Returns the number of color bit planes for the underlying window system.

The returned values is equal to the default pixmap depth;

Ver también:
QPixmap::defaultDepth()

Returns the number of color bit planes for the underlying window system.

The returned value is equal to the default pixmap depth.

Ver también:
QPixmap::defaultDepth()
static int QColor::numBitPlanes ( ) [static]
bool QColor::operator!= ( const QColor c) const [inline]

Returns TRUE if this color has a different RGB value from c; otherwise returns FALSE.

bool QColor::operator!= ( const QColor c) const
QColor& QColor::operator= ( const QColor )
QColor & QColor::operator= ( const QColor c)

Assigns a copy of the color c and returns a reference to this color.

bool QColor::operator== ( const QColor c) const [inline]

Returns TRUE if this color has the same RGB value as c; otherwise returns FALSE.

bool QColor::operator== ( const QColor c) const
uint QColor::pixel ( ) const
uint QColor::pixel ( ) const

Returns the pixel value.

This value is used by the underlying window system to refer to a color. It can be thought of as an index into the display hardware's color table, but the value is an arbitrary 32-bit value.

Ver también:
alloc()
int QColor::red ( ) const
int QColor::red ( ) const [inline]

Returns the R (red) component of the RGB value.

QRgb QColor::rgb ( ) const [inline]

Returns the RGB value.

The return type QRgb is equivalent to unsigned int.

For an invalid color, the alpha value of the returned color is unspecified.

Ver también:
setRgb(), hsv(), qRed(), qBlue(), qGreen(), isValid()
void QColor::rgb ( int r,
int g,
int b 
) const

Use getRgb() instead

QRgb QColor::rgb ( ) const
void QColor::rgb ( int r,
int g,
int b 
) const
void QColor::setHsv ( int  h,
int  s,
int  v 
)

Sets a HSV color value. h is the hue, s is the saturation and v is the value of the HSV color.

If s or v are not in the range 0-255, or h is < -1, the color is not changed.

Atención:
Colors are stored internally as RGB values, so getHSv() may return slightly different values to those set by setHsv().
Ver también:
hsv(), setRgb()
void QColor::setHsv ( int  h,
int  s,
int  v 
)
void QColor::setNamedColor ( const QString name)

Sets the RGB value to name, which may be in one of these formats: #RGB (each of R, G and B is a single hex digit) #RRGGBB #RRRGGGBBB #RRRRGGGGBBBB A name from the X color database (rgb.txt) (e.g. "steelblue" or "gainsboro"). These color names also work under Windows.

The color is invalid if name cannot be parsed.

void QColor::setNamedColor ( const QString name)
void QColor::setRgb ( QRgb  rgb)

Esta es una función miembro sobrecargada que se suministra por conveniencia. Difiere de la anterior función solamente en los argumentos que acepta. Sets the RGB value to rgb.

The type QRgb is equivalent to unsigned int.

Ver también:
rgb(), setHsv()
void QColor::setRgb ( QRgb  rgb)
void QColor::setRgb ( int  r,
int  g,
int  b 
)

Sets the RGB value to r, g, b. The arguments, r, g and b must all be in the range 0..255. If any of them are outside the legal range, the color is not changed.

Ver también:
rgb(), setHsv()
void QColor::setRgb ( int  r,
int  g,
int  b 
)

Documentación de las funciones relacionadas y clases amigas

QDataStream & operator<< ( QDataStream s,
const QColor c 
) [related]

Writes a color object, c to the stream, s.

Ver también:
Format of the QDataStream operators
QDataStream & operator>> ( QDataStream s,
QColor c 
) [related]

Reads a color object, c, from the stream, s.

Ver también:
Format of the QDataStream operators
int qAlpha ( QRgb  rgba) [related]

Returns the alpha component of the RGBA quadruplet rgba.

int qBlue ( QRgb  rgb) [related]

Returns the blue component of the RGB triplet rgb.

Ver también:
qRgb(), QColor::blue()
int qGray ( int  r,
int  g,
int  b 
) [related]

Returns a gray value 0..255 from the (r, g, b) triplet.

The gray value is calculated using the formula (r*11 + g*16 + b*5)/32.

int qGray [related]

Returns a gray value 0..255 from the given rgb colour.

int qGreen ( QRgb  rgb) [related]

Returns the green component of the RGB triplet rgb.

Ver también:
qRgb(), QColor::green()
int qRed ( QRgb  rgb) [related]

Returns the red component of the RGB triplet rgb.

Ver también:
qRgb(), QColor::red()
QRgb qRgb ( int  r,
int  g,
int  b 
) [related]

Returns the RGB triplet (r,g,b).

The return type QRgb is equivalent to unsigned int.

Ver también:
qRgba(), qRed(), qGreen(), qBlue()
QRgb qRgba ( int  r,
int  g,
int  b,
int  a 
) [related]

Returns the RGBA quadruplet (r,g,b,a).

The return type QRgba is equivalent to unsigned int.

Ver también:
qRgb(), qRed(), qGreen(), qBlue()

Documentación de los datos miembro

struct { ... } ::D32 QColor::d32
struct { ... } ::D8 QColor::d8

La documentación para esta clase fue generada a partir de los siguientes ficheros:
 Todo Clases Namespaces Archivos Funciones Variables 'typedefs' Enumeraciones Valores de enumeraciones Propiedades Amigas 'defines'