Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Converting RGB to hexadecimal (VB.net)  (Read 24211 times)

0 Members and 1 Guest are viewing this topic.

gamerx365

    Topic Starter


    Adviser
  • Radda Radda!
  • Thanked: 1
    • Yes
    • Yes
  • Experience: Experienced
  • OS: Windows 10
Converting RGB to hexadecimal (VB.net)
« on: December 17, 2008, 07:19:14 PM »
This is the code I have:

Code: [Select]
        Dim r1 As String = Hex(r)
        Dim g1 As String = Hex(g)
        Dim b1 As String = Hex(b)
        col5 = r1 & g1 & b1

where r, g, and b have values between 0 and 255.

When I use this code I'm getting numbers with as few as 3 digits. I understand that this is because this converts each number to hex separately but can anyone offer a simple alternative? Thanx.

Sidewinder



    Guru

    Thanked: 139
  • Experience: Familiar
  • OS: Windows 10
Re: Converting RGB to hexadecimal (VB.net)
« Reply #1 on: December 18, 2008, 04:51:12 AM »
Following along with your method, this may help:

Code: [Select]
dim r as byte = 10
dim g as byte = 100
dim b as byte = 200
col5 = hex(r & g & b)

The above code will produce whatever shade of Shrek 9A1DE8 is, but is this really the result you're looking for?

Quote
Application methods and properties that accept a color specification expect that specification to be a number representing an RGB color value. An RGB color value specifies the relative intensity of red, green, and blue to cause a specific color to be displayed.

Source: Visual Basic Language Reference

 8)
The true sign of intelligence is not knowledge but imagination.

-- Albert Einstein

gamerx365

    Topic Starter


    Adviser
  • Radda Radda!
  • Thanked: 1
    • Yes
    • Yes
  • Experience: Experienced
  • OS: Windows 10
Re: Converting RGB to hexadecimal (VB.net)
« Reply #2 on: December 18, 2008, 02:25:15 PM »
I suppose thats what i want... I'm not quite sure why it wouldn't be. I just wanted to make it so that you can use rgb or hex code to achieve the same, or almost exact same color. My program generates rgb but hex is easier to use(copy and paste numerous times).

thanx.

gamerx365

    Topic Starter


    Adviser
  • Radda Radda!
  • Thanked: 1
    • Yes
    • Yes
  • Experience: Experienced
  • OS: Windows 10
Re: Converting RGB to hexadecimal (VB.net)
« Reply #3 on: December 18, 2008, 02:31:28 PM »
oh and also im not sure what method I would use to do this, but using streamwriter im trying to save to a text file, but the text I'm saving needs to have quotes in it. maybe an escape key? or even something simpler?

gamerx365

    Topic Starter


    Adviser
  • Radda Radda!
  • Thanked: 1
    • Yes
    • Yes
  • Experience: Experienced
  • OS: Windows 10
Re: Converting RGB to hexadecimal (VB.net)
« Reply #4 on: December 18, 2008, 03:03:52 PM »
yeah.I don't think that code is right actually. If i use 0,0,0 i just get 0 as a result. if I use 1,1,1 i get 6f. and if I get a hex that looks real, I plug it into something and get a color that way off.

Dias de verano

  • Guest
Re: Converting RGB to hexadecimal (VB.net)
« Reply #5 on: December 18, 2008, 04:39:32 PM »
Consider the color where the red/green/blue values are decimal numbers: red=36, green=104, blue=160 (a greyish-blue color). The decimal numbers 36, 104 and 160 are equivalent to the hexadecimal numbers 24, 68 and A0 respectively. The hex triplet is obtained by concatenating the 6 hexadecimal digits together, 2468A0 in this example.

Note that if any one of the three color values is less than 16 (decimal) or 10 (hex), it must be represented with a leading zero so that the triplet always has exactly six digits. For example, the decimal triplet 4, 8, 16 would be represented by the hex digits 04, 08, 10, forming the hex triplet 040810.

gamerx365

    Topic Starter


    Adviser
  • Radda Radda!
  • Thanked: 1
    • Yes
    • Yes
  • Experience: Experienced
  • OS: Windows 10
Re: Converting RGB to hexadecimal (VB.net)
« Reply #6 on: December 18, 2008, 05:26:15 PM »
so I have to write an if statement to detect whether the hex value is less than 10 and put a zero in front of it? sounds alright. I'll give it a try.

thanx.

gamerx365

    Topic Starter


    Adviser
  • Radda Radda!
  • Thanked: 1
    • Yes
    • Yes
  • Experience: Experienced
  • OS: Windows 10
Re: Converting RGB to hexadecimal (VB.net)
« Reply #7 on: December 18, 2008, 05:38:35 PM »
Alright. Thank you dias, i believe I got it to work.

But instead of checking the hex I checked the rgb value to be sure it was above 16.

Also I still need to know how to output quotes to a file. If anyone could help with that quick.

Thanx to all.

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Converting RGB to hexadecimal (VB.net)
« Reply #8 on: December 18, 2008, 08:50:47 PM »
Quotes are outputted by placing double quotes in your string. For example, this would output a single double-quote:

""""

As for the hex format; this is a excerpt from my Colour handling class (which I'm in the process of completely rewriting to support more colour models, such as YUV and CIELAB)

Code: [Select]
Public Property Get WebFormat() As String
Attribute WebFormat.VB_Description = "Sets/returns a string representation of this colour."
'retrieve the web format form our R,G and B.
Dim I As Long
ReDim hexes(1 To 3) As String
hexes(1) = Hex$(mvarRGB.Red)
hexes(2) = Hex$(mvarRGB.Green)
hexes(3) = Hex$(mvarRGB.Blue)
For I = 1 To 3
    If Len(hexes(I)) = 1 Then
    'append a leading zero.
    hexes(I) = "0" & hexes(I)
    End If
Next I
WebFormat = "#" & JoinStr(hexes(), "")
End Function


'Joinstr routine.
Private Function JoinStr(ByRef Arrjoin() As String, Optional ByVal Delimiter As String = ",")
    Dim I As Long
    Dim StrBuild As String
   
    'Strbuild = LBound(Arrjoin) + 1
    On Error GoTo getoutofhere
    For I = LBound(Arrjoin) To UBound(Arrjoin)
        StrBuild = StrBuild & Delimiter & Arrjoin(I)
    Next I
    JoinStr = StrBuild
   
   
Exit Function

the webformat property Get would need to be changed to a Function, and you might want to remove the leading "#". It outputs the string in a format that will work when placed in a HTML attribute such as BGCOLOR or FORECOLOR, etc.


Code: [Select]
dim r as byte = 10
dim g as byte = 100
dim b as byte = 200
col5 = hex(r & g & b)

The above code will produce whatever shade of Shrek 9A1DE8 is, but is this really the result you're looking for?

Woah. Hate to call you out there sidewinder- but that is almost completely wrong- although, you simply forgot to bitshift the RGB values. For example:

Code: [Select]
Hex$(RGB(r,g,b))
which almost works, but we have the same problem with leading zeroes as before...
I was trying to dereference Null Pointers before it was cool.

gamerx365

    Topic Starter


    Adviser
  • Radda Radda!
  • Thanked: 1
    • Yes
    • Yes
  • Experience: Experienced
  • OS: Windows 10
Re: Converting RGB to hexadecimal (VB.net)
« Reply #9 on: December 19, 2008, 09:51:41 AM »
thanks. it works great. everythings working so far. thanks for the help.