TGC Codebase Backup



RGB / RGBA Colour and Other Commands by Hamish McHaggis

14th Sep 2005 14:44
Summary

The code to convert RGBA colours to a single value, and to extract the RGBA values from the single value.



Description

The code to convert RGBA colours to a single value, and to extract the RGBA values from the single value. Uses simple bitwise operations to compile four bytes into a dword (and back).



Code
                                    ` This code was downloaded from The Game Creators
                                    ` It is reproduced here with full permission
                                    ` http://www.thegamecreators.com
                                    
                                    local colour as dword
local alpha as byte
local red as byte
local green as byte
local blue as byte

alpha = 255
red = 255
green = 128
blue = 4
colour = rgb(red,green,blue)

ink colour,0
print "DBPro Colour: ",colour
ink GetColour(alpha,red,green,blue),0
print "Calculated Colour: ",GetColour(alpha,red,green,blue)
ink rgb(255,255,255),0
print "Alpha: ",GetAlpha(colour)
print "Red: ",GetRed(colour)
print "Green: ",GetGreen(colour)
print "Blue: ",GetBlue(colour)
wait key
end

function GetColour(alpha as byte, red as byte, green as byte, blue as byte)
	local colour as dword
	colour = (alpha<<24)||(red<<16)||(green<<8)||(blue)
endfunction colour

function GetAlpha(colour as dword)
	local alpha as byte
	alpha = (colour&&0xFF000000)>>24
endfunction alpha

function GetRed(colour as dword)
	local red as byte
	red = (colour&&0x00FF0000)>>16
endfunction red

function GetGreen(colour as dword)
	local green as byte
	green = (colour&&0x0000FF00)>>8
endfunction green

function GetBlue(colour as dword)
	local blue as byte
	blue = colour&&0x000000FF
endfunction blue