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!

Reverse RGB() ?

Status
Not open for further replies.

volfreak

Programmer
Nov 6, 2000
42
0
0
US
I have a grid and want the users to be able to set the dynamic fore and back color. I can get the color dialog box (GETCOLOR()) but it returns a single numeric value while the dynamic requires the character string in RGB format.

I know I can take the RGB() and convert the 3 individual values to the single numeric (for fore color and back color - why one set of properties is numeric and the other RGB character is beyond me). However, I cannot figure out how to take the single numeric value provided by GETCOLOR() and have it converted to the 3 values in RGB format.

Anyone have any ideas on how to do this? And why there is only one conversion direction RGB->##? The 'why' is not as important as the 'how'.

Thanks much in advance for any and all help.
 
Hi volfreak

The result of RGB() is a number between 0 and 16,777,215 (which is 256^3 minus one).

The formula used to compute the result is
nBlueValue * (256^2) + nGreenValue * 256 + nRedValue

HTH
Ed
Please let me know if the sugestion(s) I provide are helpful to you.
Sometimes you're the windshield... Sometimes you're the bug.
smallbug.gif
 
volfreak

LOCAL r, g, b

red = 0
green = 0
blue = 0

getRGB(getColor(),@red,@green,@blue)

? red
? green
? blue

FUNC getRGB(colour,red,green,blue)
PRIVATE p,i

p = (256 ^ 2)
i = INT(colour / p)
blue = i
colour = colour - (i * p)
p = (256 ^ 1)
i = INT(colour / p)
green = i
colour = colour - (i * p)
red = INT(colour)

HTH

Chris [pc2]
 
Actually, the DynamicBack/ForeColor properties don't need to be in RGB() format. You can specify the single number and it will work just fine.

Here is the program I wrote that does this. I use this to convert the results of the API call GetSysColor to values I can understand.
[tt]
lparameters nInColor,nRed,nGreen,nBlue,lOutput
* Parameters 2-4 should be passed BY REFERENCE
LOCAL nColor
nColor=nInColor
nBlue=INT(nColor/65536)
nColor=nColor%65536
nGreen=INT(nColor/256)
nRed=nColor%256
IF type("lOutput")='L' and lOutput
?"RED: "
??nRed
?"GREEN: "
??nGreen
?"BLUE: "
??nBlue
ENDIF
RETURN
[/tt]
To call the function, you need to initialize 3 variables to pass to it. The 3 variables can be individual variables, 3 elements of an array, or 3 properties of an object. The optional 5th parameter, if .T., will output the colors to the screen. Here are examples:
[tt]
STORE 0 TO lnRed,lnGreen,lnBlue
FromRGB(8723642,@lnRed,@lnGreen,@lnBlue,.T.)

DIMENSION aColors[3]
FromRGB(8723642,@aColors[1],@aColors[2],@aColors[3])

oCol=CreateObject("RGBClass")
FromRGB(8723642,@oCol.red,@oCol.Green,@oCol.Blue)

DEFINE CLASS RGBClass AS Custom
red=0
green=0
blue=0
ENDDEFINE
[/tt]
Hope that helps,

Ian
 
Thanks for all of the suggestions. I figured there was a mathematical calculation for the conversion and was hoping someone else had already done that. Thanks much!!!!

Thanks also for the code snippets. When looking at the on-line help it shows cColor (or something similar) for the dynamicfore/backcolor properties. I didn't see a reference to a numeric value. That's good to know that the method will accept the numeric color value.

Cheers,
 
The reason it's listed as cColor (the c indicating that it's a CHARACTER expression) is because the value for DynamicBackColor has to be a string. This string gets evaluated every time the control refreshes, but it must ultimately resolve into a single numeric number representing a color.

The RGB() function generates that number. If you already have that number, though, it would be kinda pointless to run a function to break it into RGB values just to feed it back into the RGB() function.

Good luck!

Ian
 
In the words or the famous Homer, "DOH!" Yes, Simpson not of the Iliad & Odyssey fame. ;-)

Ding Ding that makes sense. Thanks for 'setting me straight'. ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top