Results 1 to 9 of 9
  1. #1
    mmbob's Avatar
    Join Date
    Dec 2009
    Gender
    male
    Location
    ja
    Posts
    655
    Reputation
    70
    Thanks
    1,156
    My Mood
    Bitchy

    Dynamic Color Spectrum

    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);

  2. The Following 15 Users Say Thank You to mmbob For This Useful Post:

    -Stephen (01-22-2011),-xGhost- (01-22-2011),ac1d_buRn (01-22-2011),[MPGH]AVGN (01-22-2011),CodeDemon (01-22-2011),Crash (01-22-2011),Flash (01-22-2011),GodHack2 (01-22-2011),markoj (01-22-2011),NOOB (01-22-2011),S0aD (04-19-2011),Sh3hw4z (01-22-2011),topblast (01-24-2011),whatup777 (01-22-2011),_Fk127_ (01-22-2011)

  3. #2
    CodeDemon's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    vagina
    Posts
    1,074
    Reputation
    50
    Thanks
    936
    My Mood
    Fine
    Nice work bob! mmbob = win

  4. The Following User Says Thank You to CodeDemon For This Useful Post:

    Sh3hw4z (01-22-2011)

  5. #3
    Flash's Avatar
    Join Date
    Jul 2010
    Gender
    male
    Location
    Slovenia
    Posts
    7,691
    Reputation
    561
    Thanks
    1,866
    My Mood
    Fine
    Good job on this!


    Former Middleman
    07-07-2011 - 09-13-2011





    Skype: mpgh.flash
    MSN: mpgh.flash@msn.com


    “I don't care if you're black, white, straight, bisexual, gay, lesbian, short, tall, fat, skinny, rich or poor. If you're nice to me, I'll be nice to you. Simple as that.”
    - Eminem

  6. The Following User Says Thank You to Flash For This Useful Post:

    Sh3hw4z (01-22-2011)

  7. #4
    GodHack2's Avatar
    Join Date
    May 2010
    Gender
    male
    Posts
    645
    Reputation
    38
    Thanks
    762
    My Mood
    Amused
    coders are on the color picker spree





    beat this bitches ^^^^^^^

    Current Stats : Bored :/


    Respect list :
    Crash !
    Gordon'
    Markoj

  8. The Following User Says Thank You to GodHack2 For This Useful Post:

    Sh3hw4z (01-22-2011)

  9. #5
    whatup777's Avatar
    Join Date
    May 2010
    Gender
    male
    Location
    CA Source Code Section
    Posts
    4,030
    Reputation
    147
    Thanks
    351
    My Mood
    Dead
    FUCKING WIN. +Rep
    Quotes I live by.


    A foolish person learns from his mistakes, I wise person learns from others.
    Quote Originally Posted by AVGN View Post



    mhm

    i live in texas

    i was at the grocery store with my son. He saw a mexican guy, and he said "Look daddy! a mower man!"

    he's 4 yrs old

  10. The Following User Says Thank You to whatup777 For This Useful Post:

    Sh3hw4z (01-22-2011)

  11. #6
    _Fk127_'s Avatar
    Join Date
    Nov 2010
    Gender
    male
    Posts
    724
    Reputation
    16
    Thanks
    208
    My Mood
    Bitchy
    Nice job! love it



    Put this image in your signature if you support HTML5 development!

  12. The Following User Says Thank You to _Fk127_ For This Useful Post:

    Sh3hw4z (01-22-2011)

  13. #7
    -xGhost-'s Avatar
    Join Date
    Oct 2010
    Gender
    male
    Location
    C:\Sytem32
    Posts
    347
    Reputation
    9
    Thanks
    57
    My Mood
    Relaxed
    This is great, Thanks (:



  14. The Following User Says Thank You to -xGhost- For This Useful Post:

    Sh3hw4z (01-22-2011)

  15. #8
    ac1d_buRn's Avatar
    Join Date
    Aug 2009
    Gender
    female
    Location
    CA Source Section
    Posts
    3,406
    Reputation
    157
    Thanks
    3,991
    My Mood
    Flirty
    haha looks way better than my sprite
    gj and thanks for contributing.

  16. The Following User Says Thank You to ac1d_buRn For This Useful Post:

    Sh3hw4z (01-22-2011)

  17. #9
    whit's Avatar
    Join Date
    Jan 2010
    Gender
    male
    Posts
    7,170
    Reputation
    490
    Thanks
    2,252
    Very nice..
    Good job...

  18. The Following User Says Thank You to whit For This Useful Post:

    Sh3hw4z (01-22-2011)