Common Dialog

Koga73
10-30-2004, 07:37 PM
'Pallete
Private Sub cmdColor_Click()
cmndlgColor.ShowColor
End Sub


Ok, after they choose a color from the pallete, is there a way that i can get its r,g,b colors after they click the color palletes OK button?

anon
10-30-2004, 07:46 PM
Ok, after they choose a color from the pallete, is there a way that i can get its r,g,b colors after they click the color palletes OK button?
This might be helpful:
http://www.vb-helper.com/howto_common_dialog_color.html

btw - This question has been asked before on the forum (if you did a search):
http://www.visualbasicforum.com/showthread.php?t=132965&goto=nextnewest

Koga73
10-30-2004, 08:37 PM
I read the first thing, and couldnt get it 2 work, heres what i got so far


'Pallete
Private Sub cmdColor_Click()
Dim intColors(1 To 3) As Integer

cmndlgColor.DialogTitle = "Choose A Color!"
cmndlgColor.ShowColor
picColor.BackColor = cmndlgColor.Color

txtRed.Text = LTrim$(intColors(1))
txtGreen.Text = LTrim$(intColors(2))
txtBlue.Text = LTrim$(intColors(3))
End Sub


I tried the thing he did and it didnt work so i deleted it, can some1 help me out and explain what his does, or if any1 knows a different way.

anon
10-30-2004, 08:52 PM
I tried the thing he did and it didnt work so i deleted it, can some1 help me out and explain what his does, or if any1 knows a different way.
Okay, I'm not understanding what you aren't understanding, but here's an excerpt from another web page:

The ShowColor method is used to display the Colour dialog box. The Color property is used to determine which colour was selected.

cdlColour.ShowColor
picPallette.FillColor = cdlColour.Color

Alternatively, you may wish to break out the long number into its "Red", "Green" and "Blue" parts.

Dim Red As Long, Green As Long, Blue As Long
cdlColour.ShowColor
Red = cdlColour.Color And &HFF
Green = (cdlColour.Color \ &H100) And &HFF
Blue = cdlColour.Color \ &H10000

from this page:
http://juicystudio.com/tutorial/vb/cdl.asp

Basically all you are doing (as it says above) is parsing (breaking apart mathematically) a Long variable into the "parts" that represent the bytes for red, green, and blue values (0 - 255 for each). Note: that you could also use:

Dim Red As Integer, Green As Integer, Blue As Integer
cdlColour.ShowColor
Red = cdlColour.Color And &HFF
Green = (cdlColour.Color \ &H100) And &HFF
Blue = cdlColour.Color \ &H10000
txtRed.Text = Red
txtGreen.Text = Green
txtBlue.Text = Blue


The LTrim$ just returns a copy of a string without leading spaces. It doesn't split up the bytes. I will look for a better reference...

...okay, here we go, a little more explanation of the Long to RGB byte conversion:

"...where 'Color' is the long returned by either Point or GetPixel, and R, G, and B are the red, green, and blue values, respectively. So now, we have just changed a 4 byte long variable into 3 individual bytes - neat, eh? You're probably going "how on earth does this work?!" - here's the deal. To a human, it's easy to see how 4 bytes can be divided up into 4 separate numbers: blank byte, red byte, green byte, blue byte - for a total of 4 bytes, right? But to a computer, 4 bytes is seen as 1 big number from -2,147,483,648 to 2,147,483,647, not 4 separate, smaller numbers. So, we have to tell it to make 16777215 into 0, 255, 255, 255, or 16777214 to 0, 254, 255, 255, and so on. Actually, the 4 smaller bytes are arranged like so into the long: empty byte, blue byte, green byte, red byte; and obviously, this reverse placement changes how we extract the numbers. Before I can explain the specifics of how RGB extraction works, you first need to understand some basics of binary encoding (I know - it sounds hard, but really it's easy!).

Binary encoding is the heart of how computers store information. Basically, it breaks up numbers into series of zeroes and ones - because your hard drive can't store the number '17,' but it can store '00010001' - the binary equivalent of 17. Think of it this way - zero is '00000000,' one is '00000001,' two is '00000010,' three is '00000011,' four is '00000100' - you get the picture. It's a pretty easy procedure in theory, right?"

So back to graphics - GetPixel or Point gives you 4 bytes (or 36 bits) of data. Your job is to change this from one set of 36 bits into 4 sets of 8 bits. Here's how we'll do it:

Because the red value is last (remember: blank byte, blue byte, green byte, red byte) we're gonna get it first, using the wonderful Mod function (to which 'And 255' is equivalent - trust me here). Basically, 'Mod' takes the remainder of the number if it were to be divided by 256, which also happens to be the data encoded in the last byte of the 4 byte long. You're probably wondering how on earth this works...good question. I don't have the time or the particular desire to write a lesson on binary encoding (there are many documents about it already on the net, besides), so if you want to understand the specifics, I strongly encourage you to look into it. If you're just going to trust me on it, that's okay too but you'll never be able to apply the concept yourself - and that means you're not a true programmer. Anyhow, from there we shift the number 8 bits to the right (by dividing it by 2^8 or 256 - another binary encoding technique called 'binary shifting' that is very worth reading up on) and do the same thing to get green, and then another 8 bytes again to get blue. This probably doesn't make a whole lot of sense, but it's a start. When (if ever <sigh>) I get some free time I'll write this up better, but in the meantime I would again highly recommend reading up on how binary encoding works, since it is at the heart of this entire operation (and many others!).

from this page:
http://tannerhelland.tripod.com/VBGraphicsTutorial2.htm

Hopefully that makes thing clearer when you go down to the binary level...

Oh, and if you want something to actually show you what the Long variable (representing the Color value returned by the Common Dialog) looks like in binary (zeros and ones), there's a "Function LongToBinary" on this page that will do the conversion:
http://www.vb-helper.com/howto_dec_hex_oct_bin.html

Koga73
10-30-2004, 11:13 PM
Thnx, i already know binary and hex and all, i needed 2 learn it for school :\, i just didnt get how it divided up the hex color code into decimal numbers, thnx. And btw, the reason i used the ltrim$ is just cause im so used 2 using it with qbasic, cause otherwise it makes ur strings look all ****ty like its a bunch of seprate strings rather then 1 whole string.
print "hello(" + Hello$ + ")"in qbasic would display
Hello( value )
so
print "hello(" + ltrim$(rtrim$(hello$)) + ")"
hello(value)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum