天天看点

一些色彩值的转换

从Game Development Wiki看到的,虽然很内容很基础,但是我还是贴下:

//----------------------------------------------------------------------------------------------------------------------------------------

32 bit DWORD/Long to separate R,G,B,A channels :  These functions will convert a 32bit DWORD/Long value to the R,G,B,A channels it's made of. This allows you to work on individual channels, instead of the entire color. This format is widely used in Microsoft Windows' GDI, where it is known as the COLORREF type. Keep in mind that most graphics libraries already provide functions that do this.

void DWORD_To_RGBA(DWORD color, BYTE& r, BYTE& g, BYTE& b, BYTE& a)

{

  r = (color & 0xFF000000) >> 24;

  g = (color & 0x00FF0000) >> 16;

  b = (color & 0x0000FF00) >> 8;

  a = (color & 0x000000FF);

}

//----------------------------------------------------------------------------------------------------------------------------------------

R,G,B,A channels to 32 bit DWORD/Long :This code will combine the given RGBA channels to one DWORD/Long. The returning value can be used in DirectX. Note that DirectX provides its own functions for this, as well

void RGBA_To_DWORD(DWORD& color, BYTE r, BYTE g, BYTE b, BYTE a)

{

  color = (r << 24) | (g << 16) | (b << 8) | a;

}

//----------------------------------------------------------------------------------------------------------------------------------------

//Gamma correction : Gamma correction is increasing or decreasing the brightness of a color. A higher gamma value will mean brighter colors, while a lower gamma value will result in darker colors. The code could be put in it's own macro or function.

The code below uses the color in a range of 0-255 per channel. Make sure that you actually clamp the value to 0-255 after changing the gamma value, or it could lead to undesirable results.

float fGammaCorrection = 2.5f;

fR = pow( color.cR, fGammaCorrection );

fG = pow( color.cG, fGammaCorrection );

fB = pow( color.cB, fGammaCorrection );

//----------------------------------------------------------------------------------------------------------------------------------------

//Interpolating colors :Interpolating between two colors, using a simple variable to control the interpolation amount. Note that DirectX8 and newer has their own D3DXColor* functions for all kinds of color operations.

float fAmount;

fR = Color1.r + fAmount * (Color2.r - Color1.r);

fG = Color1.g + fAmount * (Color2.g - Color1.g);

fB = Color1.b + fAmount * (Color2.b - Color1.b);

fA = Color1.a + fAmount * (Color2.a - Color1.a);

//----------------------------------------------------------------------------------------------------------------------------------------

//RGB and BGR conversions

DWORD rgb_to_bgr(DWORD rgb)

{

  int b, g, r;

  b = rgb & 0xFF;

  g = (rgb >> 8) & 0xFF;

  r = rgb >> 16;

  return (b << 16) | (g << 8) | r;

}

DWORD bgr_to_rgb(DWORD bgr)

{

    int r, g, b;

    r = bgr & 0xFF;

    g = (bgr >> 8) & 0xFF;

    b = bgr >> 16;

    return (r << 16) | (g << 8) | b;

}