Android Question Color.Invert funtion from ??? to B4A

Cableguy

Expert
Licensed User
Hi guys,

I found this (the website stated as VB6) function that I need converted to B4A, can anyone help please?

B4X:
function invertHex(hexnum){
  if(hexnum.length != 6) {
    alert("Hex color must be six hex numbers in length.");
    return false;
  }
  
  hexnum = hexnum.toUpperCase();
  var splitnum = hexnum.split("");
  var resultnum = "";
  var simplenum = "FEDCBA9876".split("");
  var complexnum = new Array();
  complexnum.A = "5";
  complexnum.B = "4";
  complexnum.C = "3";
  complexnum.D = "2";
  complexnum.E = "1";
  complexnum.F = "0";
  
  for(i=0; i<6; i++){
    if(!isNaN(splitnum[i])) {
      resultnum += simplenum[splitnum[i]];
    } else if(complexnum[splitnum[i]]){
      resultnum += complexnum[splitnum[i]];
    } else {
      alert("Hex colors must only include hex numbers 0-9, and A-F");
      return false;
    }
  }
  
  return resultnum;
}
 
Last edited:

RandomCoder

Well-Known Member
Licensed User
That's not VB6 pal, its more like C or C+?
Not sure I'm good enough to decrypt that any longer, it's been many years since I studied C and never been forced to use it since.
 

Cableguy

Expert
Licensed User
It look kinda strange to me but the website marked it as VB6, so I went along... I'll correct the title
 

RandomCoder

Well-Known Member
Licensed User
Give me a min, I'm working on it ;)
Got this far up to now....
B4X:
Sub invertHex(hexnum As String)
    If hexnum.Length <> 6 Then
        ToastMessageShow("Hex color must be six hex numbers in length.", False)
        Return
    End If
   
    hexnum = hexnum.ToUpperCase
    Dim splitnum(6) As String
    For i = 0 To 6
        splitnum(i) = hexnum.CharAt(i)
    Next
    Dim resultnum As String = ""
    Dim simplestring As String = "FEDCBA9876"
    Dim simplenum(10) As String
    For i = 0 To 9
        splitnum(i) = simplestring.CharAt(i)
    Next
    Dim complexnum() As String
   
End Sub
But next is the tricky bit, trying to interpret the array complexnum :eek:
 

Cableguy

Expert
Licensed User
I plan to use this in conjuction with your project, in order to show text in an iverted color of the background so that it remains readable
 

RandomCoder

Well-Known Member
Licensed User
Completely untested but I think it is correct. Not sure if it will error because I'm returning nothing if a faulty value is supplied?
I'm also not 100% that I've interpretted the complexnum array correctly as I've not seen that done like that before, the dot notation threw me slightly but if I've understood the function of the program correctly then you throw in a 6 character hex colour code and it spits out an integer value?
B4X:
Sub invertHex(hexnum As String) As Int
    If hexnum.Length <> 6 Then
        ToastMessageShow("Hex color must be six hex numbers in length.", False)
        Return
    End If
   
    hexnum = hexnum.ToUpperCase
    Dim splitnum(6) As String
    For i = 0 To 5
        splitnum(i) = hexnum.CharAt(i)
    Next
    Dim resultnum As String = ""
    Dim simplestring As String = "FEDCBA9876"
    Dim simplenum(10) As String
    For i = 0 To 9
        splitnum(i) = simplestring.CharAt(i)
    Next
    Dim complexnum As Map
    complexnum.Put(0, 5)
    complexnum.Put(1, 4)
    complexnum.Put(2, 3)
    complexnum.Put(3, 2)
    complexnum.Put(4, 1)
    complexnum.Put(5, 0)

    For i = 0 To <6 Step 1
        If IsNumber(splitnum(i)) Then    ' !isNaN = Not(is Not A Number) double negative!!!
            resultnum = resultnum + splitnum(i)
        Else If complexnum.ContainsKey(splitnum(i)) Then
            resultnum = resultnum + complexnum.Get(splitnum(i))
        Else
            ToastMessageShow("Hex colors must only include hex numbers 0-9, and A-F", False)
        End If
        Return
    Next
   
    Return resultnum
End Sub
 

RandomCoder

Well-Known Member
Licensed User
You'll have to let me know if I've passed or failed miserably :rolleyes:
 

Cableguy

Expert
Licensed User
Sorry RC, no luck... I was just playing with it... I also googled for how to invert colors, and I got a "formula" that says that I should subtract rgb(255,255,255) from a given color to get its negative... but I'm not too sure about that either
 

RandomCoder

Well-Known Member
Licensed User
Did the conversion I wrote error or did it just not do what you expected it to do?
 

Cableguy

Expert
Licensed User
It doesn't error but I don't get the expected result. Did a bit more search and found a "get contrasting color" code snippet that works somewhat better that I thought.
 

LucaMs

Expert
Licensed User
It doesn't error but I don't get the expected result. Did a bit more search and found a "get contrasting color" code snippet that works somewhat better that I thought.
from that page:

------------
Your question is unclear; no colors in RGB are "negative colors".

You could invert an image, as though it was a film negative. Is that what you meant?

If you wanted to invert an image that has just one pixel of color 0xFF00FF, the calculation is to subtract from white, 0xFFFFFF.

$negative_result_color = 0xFFFFFF - 0xFF00FF
------------

note that 0xFFFFFF - YourColor is not exact because it does not consider the Alpha:

Colors.ARGB(Alpha, Red, Green, Blue)

It can work if you "preserve" that alpha value (you can see: http://www.b4x.com/android/forum/threads/getting-the-contrast-color-for-a-label-etc.26026/)
 

Cableguy

Expert
Licensed User
Hi Luca, that exactly the snippet I used, it sets the text color black or white according to the view background color to make the text always readable.
 
Top