 |
 |

03-31-2012, 12:49 PM
|
 |
Senior Contributor
|
|
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,177
|
|
[VB2010] - can i convert colors values to integer?
|
can i convert colors values to integer?
|
|

03-31-2012, 03:03 PM
|
|
Contributor
|
|
Join Date: Sep 2005
Posts: 565
|
|
Try this for color>integer
Code:
Public Function ConvertColorToInteger(ByVal cColor As System.Drawing.Color) As Integer
Return (cColor.B * 65536) + (cColor.G * 256) + cColor.R
End Function
And this for integer>color
Code:
Public Function ConvertIntegerToColor(ByVal intColor As Integer) As System.Drawing.Color
Return System.Drawing.Color.FromArgb((intColor \ 1) And 255, (intColor \ 256) And 255, (intColor \ 65536) And 255)
End Function
|
Last edited by 4x2y; 03-31-2012 at 03:22 PM.
|

03-31-2012, 03:27 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
|
Normally, in .Net you would include the Alpha value and use the .toARGB to convert a color to an integer.
Dim colr As Integer = Color.Green.ToArgb
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

03-31-2012, 03:33 PM
|
|
Contributor
|
|
Join Date: Sep 2005
Posts: 565
|
|
|
You cannot use the value returned by ToArgb to set color of color dialog box
My two functions are useful to use with standard color dialog box.
|
Last edited by 4x2y; 03-31-2012 at 03:53 PM.
|

03-31-2012, 07:23 PM
|
 |
Sinecure Expert
Super Moderator * Guru *
|
|
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,714
|
|
To be honest, I've never used the color dialog, so I don't what the "standard" color dialog box is.
And, Cambalinho_83 didn't mention the color dialog so I didn't consider it.
He did mention VB2010 though, and in the list of controls in the toolbox under the Dialogs section is a dialog control called "ColorDialog".
Looking at that control, I don't know why you would want to use ToArgb to set a color of the color dialog box, since you don't use an integer to set the color, you use a color (System.Drawing.Color) to set the color.
You would use FromArgb to convert an integer to a color to set the color.
You would use ToArgb to convert the returned color to an integer.
As I said, I haven't used the color dialog so may not understand the issue.
But using the ColorDialog control in VB2010, the following works fine, the way I would expect.
Code:
Dim colr As Integer = &HFFFFFF00 'Set the color to Fully Opaque Yellow
ColorDialog1.Color = Color.FromArgb(colr) 'Set the color selected in the dialog to that color
ColorDialog1.ShowDialog() 'Show the dialog with the Yellow color selected
colr = ColorDialog1.Color.ToArgb 'Set the Integer colr to the color selected by the user in the dialog
Debug.Print(colr.ToString("X")) 'Print the hex value of the color returned.
|
__________________
There Is An Island Of Opportunity In The Middle of Every Difficulty.
Miss That, Though, And You're Pretty Much Doomed.
|

04-01-2012, 04:30 AM
|
 |
Senior Contributor
|
|
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,177
|
|
Quote:
Originally Posted by passel
To be honest, I've never used the color dialog, so I don't what the "standard" color dialog box is.
And, Cambalinho_83 didn't mention the color dialog so I didn't consider it.
He did mention VB2010 though, and in the list of controls in the toolbox under the Dialogs section is a dialog control called "ColorDialog".
Looking at that control, I don't know why you would want to use ToArgb to set a color of the color dialog box, since you don't use an integer to set the color, you use a color (System.Drawing.Color) to set the color.
You would use FromArgb to convert an integer to a color to set the color.
You would use ToArgb to convert the returned color to an integer.
As I said, I haven't used the color dialog so may not understand the issue.
But using the ColorDialog control in VB2010, the following works fine, the way I would expect.
Code:
Dim colr As Integer = &HFFFFFF00 'Set the color to Fully Opaque Yellow
ColorDialog1.Color = Color.FromArgb(colr) 'Set the color selected in the dialog to that color
ColorDialog1.ShowDialog() 'Show the dialog with the Yellow color selected
colr = ColorDialog1.Color.ToArgb 'Set the Integer colr to the color selected by the user in the dialog
Debug.Print(colr.ToString("X")) 'Print the hex value of the color returned.
|
thanks to both
|
|

04-02-2012, 07:48 PM
|
|
Newcomer
|
|
Join Date: Jun 2011
Posts: 1
|
|
How about this:
Code:
'Convert Color to Integer
Dim oColor As Color = Color.Blue
Dim iColor As Integer = ColorTranslator.ToWin32(oColor)
Debug.Print(iColor.ToString) 'Debug.Print to display value
'Convert integer to Color
iColor = 255 'Red
oColor = ColorTranslator.FromWin32(iColor)
Debug.Print(oColor.Name.ToString) 'Debug.Print to display value
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|
|
|
|
 |
|