Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Integer to RGB and Inversion of RGB values

Status
Not open for further replies.

darrellblackhawk

Programmer
Aug 30, 2002
846
0
0
US
Below you'll find two functions: IntToRGB and InvertIntRGB, which are relatively fast and may be of use.

They have no type or range checking, so passing valid values are essential.

Hope someone finds them useful.

Darrell


Code:
* Function(s) usage example
clear

local R,G,B, nInitialValue, cScrFontName, nScrFontSize
nInitialValue = 14215660

cScrFontName = _screen.fontname
nScrFontSize = _screen.fontsize
_screen.fontname = "courier new"
_screen.fontsize = 12

? Padr("Initial Value",25," "),trans(nInitialValue)
? Padr("RGB Value in hex",25," "),IntToRGB(nInitialValue,@R,@G,@B,.f.)
? Padr("Inverted Value in hex",25," "),InvertIntRGB(nInitialValue,@R,@G,@B,.f.)
?
? Padr("Initial Value",25," "),trans(nInitialValue)
? Padr("RGB Value in dec",25," "),IntToRGB(nInitialValue,@R,@G,@B,.t.)
? Padr("Inverted Value in dec",25," "),InvertIntRGB(nInitialValue,@R,@G,@B,.t.)
?
? Padr("Initial Value",25," "),trans(16777215)
? Padr("RGB Value in dec",25," "),IntToRGB(16777215,@R,@G,@B,.t.)
? Padr("Inverted Value in dec",25," "),InvertIntRGB(16777215,@R,@G,@B,.t.)

_screen.fontname = cScrFontName
_screen.fontsize = nScrFontSize

* Endof Usage:

* Function:	IntToRGB()
* Descript:	Convert an integer value to a RGB value
* Pass:		R,G,B (Output i.e. Pass in by reference)
*			They will contain the numeric (dec)
*			RGB values upon return
*
*			bDecString (Input)
*			If .t., the function will return a
*			comma separated, zero padded decimal
*			string.
*
* Note:		The real numeric values are in R,G,B upon exit
function IntToRGB
  lparam I, R, G, B, bDecString
  local cRGB
  * Parameters:
  * R,G,B passed in by reference
  * and will contain the RGB values
  * respectively upon return
  * These are output variables only
  
  * bDecString if .t., The function
  * will return a comma separated
  * zero padded decimal string, otherwise
  * a hex string will be returned

  cRGB = right(trans(I,"@0"),6)

  R = int(val("0x"+left(cRGB,2)))
  G = int(val("0x"+substr(cRGB,3,2)))
  B = int(val("0x"+substr(cRGB,5,2)))

  return iif(bDecString, ;
    padl(trans(R),3,"0")+","+ ;
    padl(trans(G),3,"0")+","+ ;
    padl(trans(B),3,"0"), ;
    cRGB)
endfunc

* Function:	InvertIntRGB()
* Descript:	Inverts an integer RGB value
* Pass:		R,G,B (Output i.e. Pass in by reference)
*			They will contain the numeric (dec)
*			RGB values upon return
*
*			bDecString (Input)
*			If .t., the function will return a
*			comma separated, zero padded decimal
*			string.
*
* Note:		The real numeric values are in R,G,B upon exit
*
* 			Useful to perform masking and other
*			paint functions
function InvertIntRGB
  lparam I, R, G, B, bDecString
  local cRGB

  =IntToRGB(I,@R,@G,@B)
  R = bitxor(R,255)
  G = bitxor(G,255)
  B = bitxor(B,255)

  cRGB = ;
    right(trans(R,"@0"),2) + ;
    right(trans(G,"@0"),2) + ;
    right(trans(B,"@0"),2)

  return iif(bDecString, ;
    padl(trans(R),3,"0")+","+ ;
    padl(trans(G),3,"0")+","+ ;
    padl(trans(B),3,"0"), ;
    cRGB)

endfunc
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top