iOS Question how to convert panel color to rgb ?

Discussion in 'iOS Questions' started by bagazhou, Jul 11, 2019 at 4:26 AM.

  1. bagazhou

    bagazhou Member Licensed User

    HI

    i want to use ColorTemplate.SelectedColor to set color to panel, and how to use panel color to drawline ? thanks

    Code:
    Dialog.Title = "select color"
       
       
    Wait For (Dialog.ShowTemplate(ColorTemplate, "OK""""Exit")) Complete (Result As Int)
       
    If Result = XUI.DialogResponse_Positive Then
           
    Log("ColorTemplate.SelectedColor:" & ColorTemplate.SelectedColor)
           
          PanelColorCurrent.Color = ColorTemplate.SelectedColor
       
          
    'in this line , how to change panel color to rgb?
          'Canv.DrawLine(oldX, oldY, newX, newY, Colors.ARGB(color_A,color_R,color_G,color_B), 5)
          'Canv.Refresh

           
       
    End If
     
  2. Brandsum

    Brandsum Active Member Licensed User

    https://www.b4x.com/android/forum/threads/get-the-rgb-color-from-getpixel.56760/#post-357302
    Code:
    Sub GetARGB(Color As Int) As Int()
        
    Private res(4As 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
     
  3. bagazhou

    bagazhou Member Licensed User

    Thank you for your help ,I'll give it a try.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Why do you need the ARGB values?

    Code:
    Dim pnl As B4XView = YourPanel
    Canv.DrawLine(oldX, oldY, newX, newY, pnl.Color, 
    5dip)
     
    emexes likes this.
  5. emexes

    emexes Well-Known Member Licensed User

    The bit-shifting approach works fine, but does have an edgy feel about it due to Int being signed.

    The ByteConverter library has methods specifically for packing and unpacking different-sized variables, and is often better suited to the task (admittedly, for this particular example, it's a 51:49 toss-up... but I'd argue that generally: consistent approach = simpler programming life):
    Code:
    Sub GetARGB(Color As Int) As Byte()
        
    Dim bc As ByteConverter
        
    Dim ColorComponents() As Byte = bc.IntsToBytes(Array As Int(Color))    'split 32-bit Color Int into four 8-bit bytes
        Return ColorComponents
    End Sub
     
    Last edited: Jul 12, 2019 at 3:19 AM
  6. emexes

    emexes Well-Known Member Licensed User

    or even, no need for temporary array, so could just be:
    Code:
    Sub GetARGB(Color As Int) As Byte()
        
    Dim bc As ByteConverter
        
    return bc.IntsToBytes(Array As Int(Color))    'split 32-bit Color Int into four 8-bit bytes
    End Sub
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice