Android Question Background color based in text

rkxo

Active Member
Licensed User
Longtime User
hi,
is there any function or library to generate a rgb background color based on a text/string?
thanks

Exemple.
"abc"-->rgb(12,23,44)
"aab"-->rgb(15,45,23)
"ccc"-->rgb(45,34,23)
 

klaus

Expert
Licensed User
Longtime User
What exactly do you want to do?
What is the relation between the text and the colors?
If "abc" just means rgb(12,23,44) and there is no relation between the characters and the RGB values.
A Map could be the solution with "abc" as the key and rgb(12,23,44) the value.
 
Upvote 0

rkxo

Active Member
Licensed User
Longtime User
I would like by the text or the sum of the characters of a text or another algorithm to generate an integer that could transform it into color.

For example I have a list of cities:

paris -> p = 1 a = 2 r = 3 i = 4 s = 5

italy -> i = 4 t = 24 a = 1 .... = sum

1 + 2 + 3 + 4 + 5 = 15

this city would have the rgb (15, x, x)

to create the background color according to a header

thanks
 
Upvote 0

rkxo

Active Member
Licensed User
Longtime User
in VB.Net convert...
B4X:
Public Function ToColorFromTitleCaseLetters(ByVal fullName As String) As Color
        If fullName.IsNullOrEmpty() Then
            Return Color.Blue
        End If

        Try
            Dim result As String = String.Concat(Regex.Matches(fullName, "[A-Z]").OfType(Of Match)().Select(Function(match) match.Value))

            Dim hash As Integer = 1
            If result.Length > 1 Then
                hash = (Convert.ToInt32(result.Chars(0)) - 64) * 100 + Convert.ToInt32(result.Chars(1)) - 64


                'https://stackoverflow.com/questions/14204827/ms-chart-for-net-predefined-palettes-color-list
                Dim colors() As String = { "008000", "0000FF", "800080", "800080", "FF00FF", "008080", "FFFF00", "808080", "00FFFF", "000080", "800000", "FF3939", "7F7F00", "C0C0C0", "FF6347", "FFE4B5", "33023", "B8860B", "C04000", "6B8E23", "CD853F", "C0C000", "228B22", "D2691E", "808000", "20B2AA", "F4A460", "00C000", "8FBC8B", "B22222", "843A05", "C00000" }

                Dim max As Integer = ((AscW("Z"c) - 64) * 100 + AscW("Z"c) - 64)

                Dim index As Decimal = (CDec(hash) / CDec(max)) * colors.Length
                Return Color.FromHex(colors(CInt(Math.Truncate(index))))
            End If
        Catch
        End Try

        Return Color.Blue
End Function

anybody can adapt to b4a?

thanks
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    For Each s As String In Array("aaa", "bbb", "ccc", "aad")
        LogColor(s, StringToColor(s))
    Next
End Sub


Private Sub StringToColor (s As String) As Int
    Return Bit.Or(0xff000000, CalcHash(s))
End Sub


Private Sub CalcHash (key As String) As Int
    'djb2 hash algorithm
    Dim h As Int = 5381
    For i = 0 To key.Length - 1
        h = Bit.ShiftLeft(h, 5) + h + Asc(key.CharAt(i))
    Next
    Return h
End Sub
 
Upvote 0

rkxo

Active Member
Licensed User
Longtime User
Other question. why crash?.
B4X:
'background color
    lvPueblo.Color=StringToColor(lvPueblo.Text)
'textcolor
        lvPueblo.TextColor=IdealTextColor(StringToColor(lvPueblo.Text))

Sub GetARGB(Color As Int) As Int()
    Dim res(4) As Int
    res(0) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff000000), 24)
    res(1) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff0000), 16)
    res(2) = Bit.UnsignedShiftRight(Bit.And(Color, 0xff00), 8)
    res(3) = Bit.And(Color, 0xff)
    Return res
End Sub
Sub  IdealTextColor(bg As Int) As Int
    Dim nThreshold As Int = 105
    Dim IntRGB As ARGBColor= GetARGB(bg)
    Dim bgDelta As Int = ((IntRGB.r * 0.299) + (IntRGB.g * 0.587) + (IntRGB.b * 0.114)) '->Crash here
Dim foreColor As Int
    If(255 - bgDelta < nThreshold) Then
        foreColor=Colors.black
        Else
        foreColor=Colors.white
    End If
    Return foreColor
End Sub

java.lang.RuntimeException: Field: r not found in: [I
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
Because GetARGB(bg) returns an array of Ints and not an ARGBColor object.
Use this:
B4X:
Sub  IdealTextColor(bg As Int) As Int
    Dim nThreshold As Int = 105
    Dim IntRGB() = GetARGB(bg)
    Dim bgDelta As Int = ((IntRGB(1) * 0.299) + (IntRGB(2) * 0.587) + (IntRGB(3) * 0.114)) '->Crash here
    Dim foreColor As Int
    If(255 - bgDelta < nThreshold) Then
        foreColor=Colors.black
        Else
        foreColor=Colors.white
    End If
    Return foreColor
End Sub
 
Upvote 0
Top