Solved

getting red, green and blue values using commondialog.showcolor

Posted on 2002-04-06
17
664 Views
Last Modified: 2008-02-01
Hello,
     
     I am relatively new to VB and I have 1 question related to VB 6.0
     
     I want to know how can I get Red, Green and Blue Values using CommonDialog Control's ShowColor. I don't want to get multiplied RGB value as returned by CommonDialog.Color but each value separately as RED, GREEN, and BLUE and also HSB values separately.
     
     If it is not possible then is it possible that I can determine how that rgb value was calculated(using which three values).

     If even this isn't possible than is there any way to draw these rainbow like color strips on a form and then use mouse tracking to get rgb values? As is done in Color Dialogs Custom Option. I think this option is most difficult.
0
Comment
Question by:Sukhwinder Singh
  • 8
  • 4
  • 2
  • +2
17 Comments
 
LVL 18

Expert Comment

by:Crash2100
ID: 6923241
Convert an RGB value to a long, or a long to RGB
http://www.visualbasiccode.com/asp/showsn.asp?theID=311

A Colorpicker that uses WebSafe Colors
http://www.freevbcode.com/ShowCode.Asp?ID=945
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6923437
DIM Red as Long
DIM Green as Long
DIM Blue as Long

Dim Color as long

Red = (Color And &HFF0000)/&H10000
Green = (Color And &HFF00)/&H100
Blue = Color And &HFF

Hope this helps, inthedark....
0
 
LVL 100

Expert Comment

by:mlmcc
ID: 6923449
listening
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6923465
To stay inthedark, you have got several colors...
:)
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6923629
DIM Red as Long
DIM Green as Long
DIM Blue as Long

Dim Color as long

Sub GetColors(Color as Long,Red As Long,Green as Long,Blue s Long)

    Red = (Color And &HFF0000)/&H10000
    Green = (Color And &HFF00)/&H100
    Blue = Color And &HFF

End Sub
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6923630
The previous sub only works for real colors.  You can tell the difference as non real colors as negative.

0
 
LVL 17

Expert Comment

by:inthedark
ID: 6923634
So in your code

If Color >=0 then
   GetColors Color, Red, Green, Blue
End If

Unless you have any better ideas Richie_Simonetti?
0
 
LVL 16

Expert Comment

by:Richie_Simonetti
ID: 6924017
Sorry, maybe i was not clear..
it was a bad joke regarding your handle and color stuff
(i meant, if you are "inthedark", you can't "see any color at all" .
By the way, i like your code, so i said that to ASKER.
Cheers
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 17

Expert Comment

by:inthedark
ID: 6924026
Ha, Ha, Richie_Simonetti, I am color blind....but in the UK at 12:00 on a Sunday my sense of humor converts to a need to go to the pub.
0
 

Author Comment

by:Sukhwinder Singh
ID: 6924273
Hello,
     
     Thanks Crash2100:
     
I tried solution provided at this url http://www.visualbasiccode.com/asp/showsn.asp?theID=311
 
That worked fine but Why I have to declare red, green and blue as long not integers.
Because result will only be between 0-255.

When I use code like this bufferoverflow error occurs when assigning values to intRed only.

Dim intRed As Integer
Dim intGreen As Integer
Dim intBlue As Integer


intBlue = dlgColor.Color \ 65536
intGreen = (dlgColor.Color - intBlue * 65536) \ 256
intRed = CInt(dlgColor.Color - intBlue * 65536 - intGreen * 256) 'Buffer overflow results only here

Even when I changed intRed to a long overflow still results.


Thanks inthedark:

There is little problem:

Dim lngRed As Long
Dim lngGreen As Long
Dim lngBlue As Long
Dim lngRGBColor As Long


dlgColor.Flags = &H3
dlgColor.Color = txtColor.BackColor
dlgColor.ShowColor
txtColor.BackColor = dlgColor.Color
lngRGBColor = dlgColor.Color
GetColors lngRGBColor, lngRed, lngGreen, lngBlue


Public Sub GetColors(Color As Long, Red As Long, Green As Long, Blue As Long)

   Red = (Color And &HFF0000) / &H10000
   Green = (Color And &HFF00) / &H100
   Blue = Color And &HFF

End Sub

Value of green isn't correct. It returns values like 36013, 43320 etc. Is there any problem in this code? Red and Blue values are correct.

Also Can you please exaplain what you meant by real colors? And if its not real color then whats the solution? Also, is 'and' in (Color and &hff00) doing some kind of bit shifting?
0
 
LVL 18

Expert Comment

by:Crash2100
ID: 6924492
I'm not sure why, I keep comparing the code, but I can't see why this would make a difference.  Try this and for some reason it works fine when I put all the integer declarations on one single line.


Dim intBlue, intGreen, intRed As Integer

intBlue = dlgColor.Color \ 65536
intGreen = (dlgColor.Color - intBlue * 65536) \ 256
intRed = dlgColor.Color - intBlue * 65536 - intGreen * 256
0
 
LVL 17

Accepted Solution

by:
inthedark earned 75 total points
ID: 6924495
Longs work faster than integers.

Also you get overflows when you mix longs and integers in the same calc. 65536 is a long value.

There is a bug in VB &HFF00 is cast incorrectly by the compiler but strangly &HFF0000 is fine. But here is a work arround:


Public Sub GetColors(Color As Long, Red As Long, Green As Long, Blue As Long)

Dim Mask As Long
Mask = &HFF

Red = (Color And Mask * &H10000) / &H10000
Green = (Color And Mask * &H100) / &H100
Blue = Color And Mask


End Sub

0
 

Author Comment

by:Sukhwinder Singh
ID: 6926551
Mr. Crash2100 and Mr. inthedark thank you very much for your help.

Crash2100:

I have answer to your question that why it works when  you put all integer declarations on one single line. Because when you do something like this:

Dim intBlue, intGreen, intRed As Integer

first two i.e intBlue and intGreen are declared as Variants in VB6 and not in vb.net.

So when assignment is made its data type changes to long.

inthedark:

Yes now program worked but only problem was that
value of red is value of blue and value of blue is value of red.

It means it shouldn't be like this:

Dim Mask As Long
Mask = &HFF
   Red = (Color And Mask * &H10000) / &H10000
   Green = (Color And Mask * &H100) / &H100
   Blue = Color And Mask

But like this:

Dim Mask As Long
Mask = &HFF
   Blue = (Color And Mask * &H10000) / &H10000
   Green = (Color And Mask * &H100) / &H100
   Red = Color And Mask

One last request if you have time in future could you please explain the logic behind this function and what 'and' does?
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6927457
The following are bit operators that work on the binary bits of a Byte/Integer/Long.

"And", "Or", "XOR" & "Not"

These operators are explained in the VB reference.  The operators work as CPU instructions and so work very, very fast, way faster then any other math operators.

How so they work?

If you had a byte is is split into 8 binary bits:

00000000 = 0
00000001 = 1
00000010 = 2
00000011 = 3
etc.
11111111 = 255 or &HFF

So when you "And" two binary numbers the Machine will act as a filter.  It will compare the bits of both bytes and only let the bits through where the bits in both bytes are set.

Example:

A = 7 '  Bits 00000111 are set
B = 14 ' Bits 00001110 are set

C = A And B ' The only bits common to both are bits 2 & 3

So C will become = 00000110 = 6


Hope this helps....
0
 

Author Comment

by:Sukhwinder Singh
ID: 6929715
thank you very much sir
0
 

Author Comment

by:Sukhwinder Singh
ID: 6930287
I knew about how bits are shifted but I wanted to how and why doing something like this: (Color And &HFF0000)/&H10000 returns the value of Red.
1. Why have you taken only &H10000 (5digits) and not &H100000(6 digits) 2 each for red green and blue?
2. How do you know that doing this will return the value of red.
0
 
LVL 17

Expert Comment

by:inthedark
ID: 6944504
Sorry indianmunda, I've been away....
 
Yes this is correct.

  Blue = (Color And Mask * &H10000) / &H10000
  Green = (Color And Mask * &H100) / &H100
  Red = Color And Mask

I was used to another system where the colours are reversed (I think that in HTML colors are reversed too).


?hex(rgb(0,0,255)) ' proves your are right


>I knew about how bits are shifted but I wanted to how and >why doing something like this: (Color And
>&HFF0000)/&H10000 returns the value of Red.

If you are working in base 10 you can say like this:

AA = Int(1234/100)

In AA you get 12

In summary devide by 10 and you shift the digits to the right by one place.  Devide by 100 and you shift the bytes by 2 places.

Working in Hex is just the same.  If you devide by sixteen you perform a hexadecimal shift by one place.

16 = &H10


2) How do you know that doing this will return the value of red?

If you use RGB you can see where the colours are placed:

MSGBOX HEX(RGB(10,11,12))

10 (A in hex) is Red
11 (B in Hex) is Blue
12 (C in Hex) is Green

You should see:

"0C0B0A"

So you now know that Green is stored in the front two characters, blue in next and red in last.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA color chart bars 12 67
String manipulation in Visual Basic 7 45
using Access 8 53
passing parameter in sql procedure 9 50
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now