Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > .NET Interface and Graphics > [VB2010] - can i convert colors values to integer?


Reply
 
Thread Tools Display Modes
  #1  
Old 03-31-2012, 12:49 PM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,278
Default [VB2010] - can i convert colors values to integer?


can i convert colors values to integer?
Reply With Quote
  #2  
Old 03-31-2012, 03:03 PM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 565
Default

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
__________________
Visit my site

Last edited by 4x2y; 03-31-2012 at 03:22 PM.
Reply With Quote
  #3  
Old 03-31-2012, 03:27 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,911
Default

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.
Reply With Quote
  #4  
Old 03-31-2012, 03:33 PM
4x2y 4x2y is offline
Contributor
 
Join Date: Sep 2005
Posts: 565
Default

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.
__________________
Visit my site

Last edited by 4x2y; 03-31-2012 at 03:53 PM.
Reply With Quote
  #5  
Old 03-31-2012, 07:23 PM
passel's Avatar
passel passel is offline
Sinecure Expert

Super Moderator
* Guru *
 
Join Date: Jun 2003
Location: Upstate New York, usa
Posts: 7,911
Default

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.
Reply With Quote
  #6  
Old 04-01-2012, 04:30 AM
Cambalinho_83's Avatar
Cambalinho_83 Cambalinho_83 is offline
Senior Contributor
 
Join Date: Feb 2008
Location: somewhere in space
Posts: 1,278
Default

Quote:
Originally Posted by passel View Post
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
Reply With Quote
  #7  
Old 04-02-2012, 07:48 PM
jcouture100 jcouture100 is offline
Newcomer
 
Join Date: Jun 2011
Posts: 1
Default

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
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->