Instead of using a sprite, why not just create it at runtime . This also has a much faster GetColor system than using HDCs and what not.
D3DColorSpectrum.h:
Code:
#include <d3d9.h>
class D3DColorSpectrum
{
LPDIRECT3DDEVICE9 pDevice;
LPDIRECT3DTEXTURE9 pTex;
int Width;
int Height;
D3DCOLOR HSLToRGB(float H, float S, float L);
public:
D3DColorSpectrum();
~D3DColorSpectrum();
HRESULT Init(LPDIRECT3DDEVICE9 _pDevice, int _Width, int _Height);
void Destroy();
LPDIRECT3DTEXTURE9 GetTex();
D3DCOLOR GetColor(int X, int Y);
};
D3DColorSpectrum.cpp:
Code:
#include "D3DColorSpectrum.h"
D3DColorSpectrum::D3DColorSpectrum() : pDevice(0), pTex(0), Width(1), Height(1)
{}
D3DColorSpectrum::~D3DColorSpectrum()
{
Destroy();
}
D3DCOLOR D3DColorSpectrum::HSLToRGB(float H, float S, float L)
{
float Q;
if (L < 0.5f)
Q = L * (S + 1.0f);
else
Q = L + S - (L * S);
float P = 2 * L - Q;
float RGBs[3];
RGBs[0] = H + (1.0f / 3.0f);
RGBs[1] = H;
RGBs[2] = H - (1.0f / 3.0f);
for (int i = 0; i < 3; ++i)
{
if (RGBs[i] < 0)
RGBs[i] += 1.0f;
if (RGBs[i] > 1)
RGBs[i] -= 1.0f;
if (RGBs[i] < (1.0f / 6.0f))
RGBs[i] = P + ((Q - P) * 6 * RGBs[i]);
else if (RGBs[i] < 0.5f)
RGBs[i] = Q;
else if (RGBs[i] < (2.0f / 3.0f))
RGBs[i] = P + ((Q - P) * 6 * ((2.0f / 3.0f) - RGBs[i]));
else
RGBs[i] = P;
}
return D3DCOLOR_XRGB(int(RGBs[0] * 255.0f), int(RGBs[1] * 255.0f), int(RGBs[2] * 255.0f));
}
HRESULT D3DColorSpectrum::Init(LPDIRECT3DDEVICE9 _pDevice, int _Width, int _Height)
{
pDevice = _pDevice;
Width = _Width;
Height = _Height;
bool Bits32 = true;
HRESULT hr;
hr = pDevice->CreateTexture(Width, Height, 1, 0, D3DFMT_X8R8G8B8, D3DPOOL_MANAGED, &pTex, 0);
if (FAILED(hr))
{
Bits32 = false;
hr = pDevice->CreateTexture(Width, Height, 1, 0, D3DFMT_X4R4G4B4, D3DPOOL_MANAGED, &pTex, 0);
if (FAILED(hr))
return hr;
}
D3DLOCKED_RECT lrc;
hr = pTex->LockRect(0, &lrc, 0, 0);
if (FAILED(hr))
{
pTex->Release();
pTex = 0;
return hr;
}
float H = 0;
float S = 0.99f;
float L = 1.0f;
D3DCOLOR Color;
BYTE R;
BYTE G;
BYTE B;
DWORD* pColors32 = ((DWORD*) lrc.pBits) - 1;
WORD* pColors = ((WORD*) lrc.pBits) - 1;
for (int i = 0; i < Width; ++i)
{
for (int j = 0; j < Height; ++j)
{
Color = HSLToRGB(H, S, L);
if (Bits32)
{
pColors32++;
*pColors32 = Color;//(0xFF << 24) | (R << 16) | (G << 8) | (B << 0);
}
else
{
R = ((Color >> 16) & 0xFF) / 0x10;
G = ((Color >> 8) & 0xFF) / 0x10;
B = ((Color >> 0) & 0xFF) / 0x10;
pColors++;
*pColors = (0xFF << 12) | (R << 8) | (G << 4) | (B << 0);
}
H += (1.0f / Width);
}
L -= (1.0f / Height);
H = 0.0f;
}
pTex->UnlockRect(0);
return S_OK;
}
void D3DColorSpectrum::Destroy()
{
if (pTex != 0)
{
pTex->Release();
pTex = 0;
}
}
LPDIRECT3DTEXTURE9 D3DColorSpectrum::GetTex()
{
return pTex;
}
D3DCOLOR D3DColorSpectrum::GetColor(int X, int Y)
{
float H = X * (1.0f / Width);
float S = 0.99f;
float L = 1.0f - Y * (1.0f / Height);
return HSLToRGB(H, S, L);
}
Usage:
(In reset and where you first initialize everything)
Code:
ColorSpectrum.Init(pDevice, Width, Height);
(In your drawing code)
Code:
pSprite->Draw(ColorSpectrum.GetTex(), ...);
(In your color checking code)
Code:
POINT pt;
GetCursorPos(&pt);
D3DCOLOR ClickedColor = ColorSpectrum.GetColor(pt.x - SpectrumX, pt.y - SpectrumY);