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

Discussion in 'Android Questions' started by Cableguy, Jun 1, 2015.

  1. Cableguy

    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?

    Code:
    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: Jun 1, 2015
  2. RandomCoder

    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.
     
    thedesolatesoul likes this.
  3. Cableguy

    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
     
  4. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Give me a min, I'm working on it ;)
    Got this far up to now....
    Code:
    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(6As String
        
    For i = 0 To 6
            splitnum(i) = hexnum.CharAt(i)
        
    Next
        
    Dim resultnum As String = ""
        
    Dim simplestring As String = "FEDCBA9876"
        
    Dim simplenum(10As 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:
     
  5. Cableguy

    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
     
  6. RandomCoder

    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?
    Code:
    Sub invertHex(hexnum As StringAs 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(6As String
        
    For i = 0 To 5
            splitnum(i) = hexnum.CharAt(i)
        
    Next
        
    Dim resultnum As String = ""
        
    Dim simplestring As String = "FEDCBA9876"
        
    Dim simplenum(10As String
        
    For i = 0 To 9
            splitnum(i) = simplestring.CharAt(i)
        
    Next
        
    Dim complexnum As Map
        complexnum.Put(
    05)
        complexnum.Put(
    14)
        complexnum.Put(
    23)
        complexnum.Put(
    32)
        complexnum.Put(
    41)
        complexnum.Put(
    50)

        
    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
     
    DonManfred likes this.
  7. RandomCoder

    RandomCoder Well-Known Member Licensed User

    You'll have to let me know if I've passed or failed miserably :rolleyes:
     
    DonManfred likes this.
  8. Cableguy

    Cableguy Expert Licensed User

    Not today I'm afraid, PC is already off!
     
  9. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Hi pal, did it work then??? :rolleyes:
     
  10. Cableguy

    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
     
  11. RandomCoder

    RandomCoder Well-Known Member Licensed User

    Did the conversion I wrote error or did it just not do what you expected it to do?
     
  12. LucaMs

    LucaMs Expert Licensed User

  13. Cableguy

    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.
     
  14. LucaMs

    LucaMs Expert Licensed User

    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/)
     
    RandomCoder likes this.
  15. Cableguy

    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.
     
Loading...