1.

Solve : Converting RGB to hexadecimal (VB.net)?

Answer»

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.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


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.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?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.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.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.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.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.

Quote from: Sidewinder on December 18, 2008, 04:51:12 AM

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...thanks. it works great. everythings working so far. thanks for the help.


Discussion

No Comment Found