DirectX Color Documentation

Overview

DirectX uses a variety of color representations to accommodate different hardware capabilities and rendering techniques. Understanding these formats is essential for accurate visual output and performance optimization.

Color Formats

FormatBitsDescription
DXGI_FORMAT_R8G8B8A8_UNORM328‑bit unsigned normalized RGBA
DXGI_FORMAT_B8G8R8A8_UNORM328‑bit unsigned normalized BGRA (common in Windows)
DXGI_FORMAT_R10G10B10A2_UNORM3210‑bit RGB, 2‑bit Alpha
DXGI_FORMAT_R16G16B16A16_FLOAT6416‑bit floating‑point per channel
D3DCOLORVALUE128Four 32‑bit floats (XMFLOAT4)
#FF7F00

Color Spaces

DirectX distinguishes between sRGB (gamma‑corrected) and Linear color spaces. Most textures are stored in sRGB and are automatically converted to linear when sampled.

// Enable sRGB on a render target
DXGI_SWAP_CHAIN_DESC scDesc = {};
scDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM_SRGB;

Using Colors in the API

Colors can be passed directly to APIs as D3DCOLORVALUE, XMFLOAT4, or packed DWORD values.

// Packing a D3DCOLOR (ARGB)
DWORD PackColor(float a, float r, float g, float b)
{
    BYTE A = (BYTE)(a * 255.0f) & 0xFF;
    BYTE R = (BYTE)(r * 255.0f) & 0xFF;
    BYTE G = (BYTE)(g * 255.0f) & 0xFF;
    BYTE B = (BYTE)(b * 255.0f) & 0xFF;
    return (A << 24) | (R << 16) | (G << 8) | B;
}

Examples

Clear a render target with a custom color using Direct3D 11:

float clearColor[4] = { 1.0f, 0.5f, 0.0f, 1.0f }; // RGBA
context->ClearRenderTargetView(renderTargetView, clearColor);

Apply a constant buffer with a color value in HLSL:

// C++ side
struct CBColor { XMFLOAT4 color; };
CBColor cb = { XMFLOAT4(0.2f, 0.6f, 0.9f, 1.0f) };
context->UpdateSubresource(cbBuffer,0,nullptr,&cb,0,0);

// HLSL
cbuffer CBColor : register(b0) { float4 gColor; }
float4 PSMain() : SV_Target { return gColor; }