DirectX Greyscale that works

BlitzMax Forums/BlitzMax Beginners Area/DirectX Greyscale that works

This :
Strict

Graphics 800,600,0

Local t:timage = LoadImage("weirdclouds.jpg")

SetBlend(alphablend)


SetMono(True,$0036B612,$777777) 

DrawImageRect(t,0,0,400,300)
SetMono(False)
DrawImageRect(t,400,0,400,300)
Flip

WaitMouse()



'If you want more physically correct luminance (the eye is more receptive to some colour 
'components than others), the REC709 coefficients (For average PC monitor) are r=0.2125, 
'g=0.7154, b=0.0721. Converted To the TEXTUREFACTOR constant (multiply by 255 And convert To Hex) 
'it's: 0x0036B612 [since you have to round some of the coefficients, you lose some precision - 
'really the r,g,b parts of the constant should add up To 255 so I'd an extra 1 to the blue.

' Source Color Default is the Red Channel.


Function SetMono(state:Byte=True,dwFactor:Int = $0036B612,SourceCol:Int=$555555)
	If State
		SetColor((SourceCol Shr 16) & $FF,(SourceCol Shr 8) & $FF,SourceCol & $FF)
		PrimaryDevice.device.SetRenderState(D3DRS_TEXTUREFACTOR, dwFactor)
  		PrimaryDevice.device.SetTextureStageState(1, D3DTSS_COLOROP,D3DTOP_DOTPRODUCT3)
  		PrimaryDevice.device.SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE) 
  		PrimaryDevice.device.SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_TFACTOR)  
	Else
		PrimaryDevice.device.SetRenderState(D3DRS_TEXTUREFACTOR, $FFFFFFFF ) 
  		PrimaryDevice.device.SetTextureStageState(1,D3DTSS_COLOROP,D3DTOP_MODULATE)  
		PrimaryDevice.device.SetTextureStageState(1,D3DTSS_COLORARG1,D3DTA_TEXTURE)
		PrimaryDevice.device.SetTextureStageState(1,D3DTSS_COLORARG2,D3DTA_DIFFUSE)
		SetColor 255,255,255
	EndIf
End Function

Gives :