Can you please advise me on how to convert hex to RGB in ASP Classic/VBScript. I have tried to search all over the Internet and tried out many suggested solutions but none point to what I want to achieve.
I have tried the following functions but none of them work: Convert hex color string to RGB color
After converting to RBB, I wish to set the text color based on the background color. So basically my background color code is in hex.
First, you convert your hex code into decimal using "&h" representation and parsing the result. After that, it's a matter of doing basic bitwise operators to extract the RGB values from the number.
Dim hexval : hexval = "fdfeff"
Dim rgbval : rgbval = CLng("&h" & hexval)
Dim r : r = (rgbval And &hff0000&) / 65536
Dim g : g = (rgbval And &h00ff00&) / 256
Dim b : b = (rgbval And &h0000ff&)
wscript.echo Join(Array(hexval, rgbval, r, g, b), vbcrlf)
This produces the following output:
fefeff
16645887
253
254
255
This is a little long and I am sure the base 16 function could be more efficient but it works.
' Convert Hex to RGB
Function ConvertHexToRBG(theHexColor)
Color = Replace(theHexColor, "#", "")
Red = (Mid(Color, 1, 2))
Green = (Mid(Color, 3, 2))
Blue = (Mid(Color, 5, 2))
ConvertHexToRBG = "RGB("&ConvertHexToInt(red)&","&ConvertHexToInt(green)&","&ConvertHexToInt(blue)&",1)"
End Function
Function ConvertHexToInt(theStr)
If theStr <> "" Then
SELECT Case Left(theStr,1)
Case "F"
T = 15
Case "E"
T = 14
Case "D"
T = 13
Case "C"
T = 12
Case "B"
T = 11
Case "A"
T = 10
Case "9"
T = 9
Case "8"
T = 8
Case "7"
T = 7
Case "6"
T = 6
Case "5"
T = 5
Case "4"
T = 4
Case "3"
T = 3
Case "2"
T = 2
Case "1"
T = 1
CASE "0"
T = 0
Case Else
T = Left(theStr,1)
End SELECT
SELECT Case Right(theStr,1)
Case "F"
D = 15
Case "E"
D = 14
Case "D"
D = 13
Case "C"
D = 12
Case "B"
D = 11
Case "A"
D = 10
Case "9"
D = 9
Case "8"
D = 8
Case "7"
D = 7
Case "6"
D = 6
Case "5"
D = 5
Case "4"
D = 4
Case "3"
D = 3
Case "2"
D = 2
Case "1"
D = 1
CASE "0"
D = 0
Case Else
D = Right(theStr,1)
End SELECT
ConvertHexToInt = CInt(T*16)+CInt(D)
Else
ConvertHexToInt = theStr
End If
Related
I need your help with the coordinates. What I would like to happen is to print an "X" after the given coordinates. Example: The given coordinates for x-axis is 2 and y-axis is 2
the output will be:
So basically, 2 "#" on the top and 2 "#" on the left, then it will print the letter "X"
Dim d As String = ""
For i = 0 To NumericUpDownX.Value
For j = 0 To NumericUpDownY.Value
d = d & "#"
Next
d = d & vbNewLine
Next
output.Text = d
I was able to print the # but I can't seem to figure out how to put the "X" there.
I'd do it like this with the String constructor and PadLeft:
Dim d As New System.Text.StringBuilder
For y = 0 To NumericUpDownY.Value
If y < NumericUpDownY.Value Then
d.AppendLine(New String("#", NumericUpDownX.Value + 1))
Else
d.AppendLine("X".PadLeft(NumericUpDownX.Value + 1, "#"))
End If
Next
output.Text = d.ToString
If you want something more inline with what you were originally doing, then:
Dim d As String = ""
For y = 0 To NumericUpDownY.Value
For x = 0 To NumericUpDownX.Value
If y = NumericUpDownY.Value AndAlso x = NumericUpDownX.Value Then
d = d & "X"
Else
d = d & "#"
End If
Next
d = d & vbCrLf
Next
output.Text = d
I am in need of a function, say, int2string, that can convert a given integer into its string representation. If possible, int2string may take a second argument referring to the base of the representation. By default, this second argument is 10.
To tostring_int. It's available in prelude/SATS/tostring.sats and you can overload tostring or something if you'd like :)
This works - although it may not be the best style. Please feel free to refine this answer.
extern
fun
int2string(i: int, b: int): string
(* ****** ****** *)
implement
int2string(i, b) = let
val () = assertloc(i >= 0)
fun dig2str(i:int): string =
if i = 0 then "0"
else if i = 1 then "1"
else if i = 2 then "2"
else if i = 3 then "3"
else if i = 4 then "4"
else if i = 5 then "5"
else if i = 6 then "6"
else if i = 7 then "7"
else if i = 8 then "8"
else if i = 9 then "9"
else "" // can add A, B, C,...
fun helper(i: int, res: string): string =
if i > 0 then helper(i / b, dig2str(i % b) + res)
else res
in
helper(i, "")
end
Public Function MyMod(a As Double, b As Double) As Double
MyMod = a - Int(a / b) * b
End Function
This code doesn't work as it doesn't correctly show the remainder do be able to then calculate HEX.
Correct : 10009335357561071 / 16 = 62558345984756.69
VB6 MyMod returns 0 instead of a valid remainder.
I have been unable to figure out how to convert such a large value into a hex string?
I was able to code it myself. Because of the vb6 limitations of the size of a number, I had to go about it in different ways. I needed this to be able to covert VERY LARGE WHOLE numbers to Binary and Hexadecimal.
This this code, there are three functions you can use.
1) Decimal 2 Hex
2) Binary to Hex
3) Decimal 2 Binary
The code works and gives me CORRECT returns for the VERY large numbers.
Public Function Dec2Hex(Dec As String) As String
Dec2Hex = Binary2Hex(Dec2Bin(Dec))
End Function
Public Function Binary2Hex(Binary As String, Optional Pos As Long = 0) As String
Dim tic As Long
Dim Sz As Long
Dim x As Long
Dim z As Long
Dim AT As Long
Dim Hx As Long
Dim HxB As String
Dim xstart As Long
Dim xstop As Long
HxB = vbNullString
If InStrB(Binary, " ") <> 0 Then Binary = Replace(Binary, " ", "")
Sz = Len(Binary)
xstart = Sz
xstop = xstart - 3
Do
AT = 0
Hx = 0
If xstop < 1 Then xstop = 1
For x = xstart To xstop Step -1
AT = AT + 1
If AscB(Mid$(Binary, x, 1)) = 49 Then
Select Case AT
Case 1: Hx = Hx + 1
Case 2: Hx = Hx + 2
Case 3: Hx = Hx + 4
Case 4: Hx = Hx + 8
End Select
End If
Next x
HxB = Digit2Hex(CStr(Hx)) + HxB
If x <= 1 Then Exit Do
xstart = x
xstop = xstart - 3
Loop
Binary2Hex = HxB
End Function
Private Function Digit2Hex(digit As String) As String
Select Case digit
Case "0": Digit2Hex = "0"
Case "1": Digit2Hex = "1"
Case "2": Digit2Hex = "2"
Case "3": Digit2Hex = "3"
Case "4": Digit2Hex = "4"
Case "5": Digit2Hex = "5"
Case "6": Digit2Hex = "6"
Case "7": Digit2Hex = "7"
Case "8": Digit2Hex = "8"
Case "9": Digit2Hex = "9"
Case "10": Digit2Hex = "A"
Case "11": Digit2Hex = "B"
Case "12": Digit2Hex = "C"
Case "13": Digit2Hex = "D"
Case "14": Digit2Hex = "E"
Case "15": Digit2Hex = "F"
Case Else: Digit2Hex = vbNullString
End Select
End Function
Public Function Dec2Bin(Dec As String) As String
Dim Bin As String
Dim Var As Variant
Dim p As Long
Dim Tmp As String
Bin = vbNullString
Tmp = Dec
Do
Bin = IIf(isEven(Tmp), "0", "1") + Bin
Var = CDec(Tmp)
Var = Var / 2
Tmp = CStr(Var)
p = InStr(Tmp, ".")
If p > 0 Then Tmp = Mid(Tmp, 1, p - 1)
If Len(Tmp) = 1 Then
If CLng(Tmp) = 0 Then Exit Do
End If
Loop
Dec2Bin = Bin
End Function
Public Function isEven(Dec As String) As Boolean
Dim OE As Long
Dim myDec As Variant
OE = CLng(Right$(CStr(Dec), 1))
isEven = (OE = 0 Or OE = 2 Or OE = 4 Or OE = 6 Or OE = 8)
End Function
The only convenient data type in VB6 that can accurately represent 10009335357561071 is the Variant's Decimal subtype. Both Double and Currency native types lack the precision required.
There is also the matter of handling signed values and for that matter how many bytes of precision are desired, whether leading zeros should be suppressed, and probably others.
It is very hard to conceive of a need for this in a real application.
Even if we presume you are doing something "especially special" or if some instructor has given you this problem as an aid to general understanding...
... there just isn't much you can do with this without a BigNum library of some sort, or possibly using Decimal with some care though it only gains you a few more digits of precision.
Here is a working sample (using Fix), that is not mine, credit to http://visualbasic.ittoolbox.com/groups/technical-functional/visualbasic-l/vb60-hex-function-overflow-error-2744358.
Private Function MyHex(ByVal TempDec As Double) As String
Dim TNo As Integer
MyHex = ""
Do
TNo = TempDec - (Fix(TempDec / 16) * 16)
If TNo > 9 Then
MyHex = Chr(55 + TNo) & MyHex
Else
MyHex = TNo & MyHex
End If
TempDec = Fix(TempDec / 16)
Loop Until (TempDec = 0)
End Function
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Private Function Dec2Hex(ByVal strDec As Variant) As String
Dim mybyte(0 To 19) As Byte
Dim lp As Long
CopyMemory mybyte(0), ByVal VarPtr(CDec(strDec)), 16
' Quick reorganise so we can then just step through the entire thing in one loop
For lp = 7 To 4 Step -1
mybyte(12 + lp) = mybyte(lp)
Next
' Build the hex string
For lp = 19 To 8 Step -1
If (Not Len(Dec2Hex) And mybyte(lp) <> 0) Or Len(Dec2Hex) Then
'Dec2Hex = Dec2Hex & Format(hex(mybyte(lp)), IIf(Len(Dec2Hex), "00", "0"))
Dec2Hex = Dec2Hex & IIf(Len(Dec2Hex), Right$("0" & hex(mybyte(lp)), 2), hex(mybyte(lp)))
End If
Next
End Function
How would I make a *.bmp image using 1 bit per pixel using VB6? Does an example project exist for something like this?
'# # Image Data Info : #
'# # Each black dot are represented as binary 1(high)#
'# # and white are represented as binary 0(low) in #
'# # form of hexadecimal character. #
'# # Example : (for this example assume the image width is 8)#
'# # Data : 7E817E #
'# # Binary data : 7=0111, E=1110, 8=1000, 1=0001 #
'# # 7=0111, E=1110 #
'# # Image data : px1 px2 px3 px4 px5 px6 px7 px8 #
'# # px1 w b b b b b b w #
'# # px2 b w w w w w w b #
'# # px3 w b b b b b b w #
'# # #
'# # w = white, b = black, px = pixel #
Details:
You may use the following code, please note that:
the image width must be a multiple of 8;
the rows start from the bottom;
If the requirements are not good for you, the code can be fixed accordingly.
Option Explicit
Private Type BITMAPFILEHEADER
bfType As String * 2
bfSize As Long
bfReserved1 As Integer
bfReserved2 As Integer
bfOffBits As Long
End Type
Private Type BITMAPINFOHEADER
biSize As Long
biWidth As Long
biHeight As Long
biPlanes As Integer
biBitCount As Integer
biCompression As Long
biSizeImage As Long
biXPelsPerMeter As Long
biYPelsPerMeter As Long
biClrUsed As Long
biClrImportant As Long
End Type
Private Type RGBQUAD
rgbBlue As Byte
rgbGreen As Byte
rgbRed As Byte
rgbReserved As Byte
End Type
Private Type BITMAPINFO
bmiHeader As BITMAPINFOHEADER
bmiColors(1) As RGBQUAD
End Type
Public Function strToBmp(str As String, w As Integer, h As Integer, filename As String) As Boolean
Dim bmfh As BITMAPFILEHEADER
Dim bmi As BITMAPINFO
Dim r As Boolean
Dim ff As Integer
Dim i As Integer
Dim x As Integer
Dim rl As Integer
Dim rw As Integer
Dim s As String
Dim b As Byte
rw = ((w + 31) \ 32 + 3) And &HFFFFFFFC
With bmfh
.bfType = "BM"
.bfSize = Len(bmfh) + Len(bmi) + rw * h
.bfOffBits = Len(bmfh) + Len(bmi)
End With
With bmi.bmiHeader
.biSize = Len(bmi.bmiHeader)
.biWidth = w
.biHeight = h
.biPlanes = 1
.biBitCount = 1
.biCompression = 0
.biSizeImage = rw * h
.biXPelsPerMeter = 72
.biYPelsPerMeter = 72
.biClrUsed = 0
.biClrImportant = 0
End With
With bmi.bmiColors(0)
.rgbRed = 255
.rgbGreen = 255
.rgbBlue = 255
End With
On Error Resume Next
Call Kill(filename)
On Error GoTo e2
ff = FreeFile()
Open filename For Binary Access Write As #ff
On Error GoTo e1
Put #ff, , bmfh
Put #ff, , bmi
For i = 1 To Len(str) Step 2
b = CByte("&H" & Mid(str, i, 2))
Put #ff, , b
rl = rl + 1
x = x + 8
If x = w Then
b = 0
Do While rl < rw
Put #ff, , b
rl = rl + 1
Loop
x = 0
rl = 0
End If
Next i
r = True
e1:
Close ff
e2:
strToBmp = r
End Function
Public Sub test()
Call strToBmp("7E817E", 8, 3, "out.bmp")
End Sub
This is the resulting image:
Please also note that Microsoft Paint seems to have a bug which affects monochromatic images resulting in the scrambling of some pixels.
How to write DRYer code for this in a model:
a = 10
b = 6
if a == b
a = 20
else
a
end
Basically, a remains a = 10 when a != b.
a = 10
b = 6
a = 20 if a == b
If this is in a method and you want the last value of a to be returned:
a = 10
b = 6
a == b ? a = 20 : a
Here's the third one :
You can also use short circuit operator and
a = 10
b = 6
a == b and a = 20