string to color

mcorbeel

Active Member
Licensed User
Longtime User
I'm trying to convert a string into a color, but I don't find the right way. Tried with a INT as string and a HEX as string but always get error. (The value is returned by a POST-GET from server so it is always a string).

Dim C as string "6291456" 'value as INT
activity.color = C
-> Error

Dim C as string = "0xfff0f8ff" 'value as HEX
activity.color = C
-> Error

What string format should I use so that I can convert it to a color?
 

thedesolatesoul

Expert
Licensed User
Longtime User
I'm trying to convert a string into a color, but I don't find the right way. Tried with a INT as string and a HEX as string but always get error. (The value is returned by a POST-GET from server so it is always a string).

Dim C as string "6291456" 'value as INT
activity.color = C
-> Error

Dim C as string = "0xfff0f8ff" 'value as HEX
activity.color = C
-> Error

What string format should I use so that I can convert it to a color?

Try casting it:
B4X:
Dim C as string = "6291456"
Dim d as Int = c
activity.color = d

EDIT: I was too slow!
 
Upvote 0

mcorbeel

Active Member
Licensed User
Longtime User
Note that your code is equivalent to:
B4X:
Dim C as string = "6291456"
activity.color = C

The problem is that this value is not a valid color (it might be a transparent color).

This gives no error, but my background color stays black, even though the color 6291711 should give a light blue color. The value 6291711 comes from a webserver with VB.net code that translates the Hex RGB color FF000060 into 6291711
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
I think the "Invalid Int" problem is that ParseInt regards the number as positive unless it is signed so your hex string represents a positive number that is too large to be accommodated by a signed integer.

"FF000060" should be a negative number when represented as a signed integer, 6291711 is 0x6000FF.

I'm struggling to find a neat way of doing what you want, the best I've come up with is this
B4X:
Sub Activity_Resume
   Dim C As String = "ff000060"
   Dim b() As Byte
   Dim bconv As ByteConverter
   b = bconv.HexToBytes(C)   
   Dim col As Int
   col =1 
   For i = 0 To b.Length -1
      col = Bit.ShiftLeft(col, 8) + Bit.AND(b(i), 255)
   Next   
   Activity.Color = col
End Sub
 
Upvote 0

mcorbeel

Active Member
Licensed User
Longtime User
I think the "Invalid Int" problem is that ParseInt regards the number as positive unless it is signed so your hex string represents a positive number that is too large to be accommodated by a signed integer.

"FF000060" should be a negative number when represented as a signed integer, 6291711 is 0x6000FF.

I'm struggling to find a neat way of doing what you want, the best I've come up with is this
B4X:
Sub Activity_Resume
   Dim C As String = "ff000060"
   Dim b() As Byte
   Dim bconv As ByteConverter
   b = bconv.HexToBytes(C)   
   Dim col As Int
   col =1 
   For i = 0 To b.Length -1
      col = Bit.ShiftLeft(col, 8) + Bit.AND(b(i), 255)
   Next   
   Activity.Color = col
End Sub

A bit heavy, but at least this works! Thanks alot!!!
 
Upvote 0

MLDev

Active Member
Licensed User
Longtime User
I think the "Invalid Int" problem is that ParseInt regards the number as positive unless it is signed so your hex string represents a positive number that is too large to be accommodated by a signed integer.

"FF000060" should be a negative number when represented as a signed integer, 6291711 is 0x6000FF.

I'm struggling to find a neat way of doing what you want, the best I've come up with is this
B4X:
Sub Activity_Resume
   Dim C As String = "ff000060"
   Dim b() As Byte
   Dim bconv As ByteConverter
   b = bconv.HexToBytes(C)   
   Dim col As Int
   col =1 
   For i = 0 To b.Length -1
      col = Bit.ShiftLeft(col, 8) + Bit.AND(b(i), 255)
   Next   
   Activity.Color = col
End Sub

The color FF000060 is encoded as ARGB. Here's another way to do it:

B4X:
Sub Activity_Resume
   Dim C As String = "ff000060"
   Dim b() As Byte
   Dim bconv As ByteConverter
   b = bconv.HexToBytes(C)   
   Activity.Color = Colors.ARGB(b(0),b(1),b(2),b(3))
End Sub
 
Upvote 0
Top