Android Question [B4X] HexToColor Error

Lucas Eduardo

Active Member
Licensed User
Hello, i'm trying to use this function HexToColor from this post https://www.b4x.com/android/forum/threads/b4x-hextocolor-and-colortohex.114300/
but it's giving me this error below:

B4X:
Error occurred on line: 1070 (NovoPedido)
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
    at java.lang.reflect.Array.get(Array.java:189)
    at anywheresoftware.b4a.shell.ArraysUtils.getElement(ArraysUtils.java:76)
    at anywheresoftware.b4a.shell.Shell.getArrayElement(Shell.java:584)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:383)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:255)
    at java.lang.reflect.Method.invoke(Native Method)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:144)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.shell.DebugResumableSub$RemoteResumableSub.resume(DebugResumableSub.java:22)
    at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:267)
    at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:137)
    at anywheresoftware.b4a.BA.raiseEvent(BA.java:193)
    at anywheresoftware.b4a.keywords.Common$14.run(Common.java:1760)
    at android.os.Handler.handleCallback(Handler.java:794)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:176)
    at android.app.ActivityThread.main(ActivityThread.java:6662)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)

The error it's on this line:
B4X:
Return ints(0)

An example of hex that i'm trying to convert: #00FFFF

Can someone help me?

Thank you.
 

DonManfred

Expert
Licensed User
Longtime User
An example of hex that i'm trying to convert: #00FFFF
not in the code you are using. Based on the error the array has a length of 0.
Also note that you maybe need to parse a hex like #FF00FFFF (ARGB)

B4X:
Log(HexToColor("#FF00FFFF"))
works
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
I added code that will prefix the input hex with "FF" if only 6 hex digits are provided to the routine:

B4X:
Sub HexToColor(InHex As String) As Int
    Dim bc As ByteConverter
    If InHex.StartsWith("#") Then
        InHex = InHex.SubString(1)
    Else If InHex.StartsWith("0x") Then
        InHex = InHex.SubString(2)
    End If

    If InHex.Length = 6 Then
        InHex = "FF" & InHex
    End If

    Dim ints() As Int = bc.IntsFromBytes(bc.HexToBytes(InHex))
    Return ints(0)
End Sub
 
Upvote 0
Top