Android Code Snippet Color Picker

Discussion in 'Code Snippets' started by Erel, Jun 5, 2017.

  1. Erel

    Erel Administrator Staff Member Licensed User

    Code to create a simple color picker based on an image:

    [​IMG]

    Two canvases are used. The first one reads the color from the image and the second one draws the selection circle.
    You can replace the image with any other image to change the color palete.
    Code:
    Sub Activity_Create(FirstTime As Boolean)
       
    Activity.LoadLayout("1")
       cvsColors.Initialize(pnlColors)
       cvsOverlay.Initialize(pnlOverlay)
    End Sub

    Sub pnlColors_Touch (Action As Int, X As Float, Y As Float)
       
    Dim cx = pnlColors.Width / 2, cy = pnlColors.Height / 2 As Float
       
    If Sqrt(Power(x - cx, 2) + Power(y - cy, 2)) > pnlColors.Width / 2 Then Return
       
    Dim clr As Int = cvsColors.Bitmap.GetPixel(X, Y) '<---- clr is the result
       Activity.Color = clr
       cvsOverlay.DrawColor(
    Colors.Transparent)
       cvsOverlay.DrawCircle(X, Y, 
    20dipColors.Black, False3dip)
       pnlOverlay.Invalidate
    End Sub
    File is available here: https://www.b4x.com/android/forum/attachments/colorpicker-zip.56334/
     
    Danamo, kaktus, Star-Dust and 2 others like this.
  2. Erel

    Erel Administrator Staff Member Licensed User

    And now as a custom dialog:
    [​IMG]

    Usage:
    Code:
    Sub btnSelectColor_Click
       
    Dim cw As ColorWheelDialog
       cw.Initialize
       cw.ShowAsync(Me, 
    "Select Color")
       
    Wait For (cw) Color_Result(Success As Boolean)
       
    If Success Then
         
    Activity.Color = cw.ColorResult
       
    End If
    End Sub
    Depends on Dialogs library v4+ and B4A v7+.
     

    Attached Files:

    Peter Simpson, Beja, DavideV and 4 others like this.
  3. DavideV

    DavideV Active Member Licensed User

    Hi Erel,
    could the colored image be created with code without any dependency from the png file?
    It will be great for a new dialog library i'm writing all based on b4a code only.
    Thanks
     
  4. Star-Dust

    Star-Dust Expert Licensed User

    DavideV likes this.
  5. DavideV

    DavideV Active Member Licensed User

    Thanks Star Dust, looks interesting, perharps what i was looking for :)
    (Grazie Star Dust potrebbe essere quello che cercavo!)
     
  6. Star-Dust

    Star-Dust Expert Licensed User

    I'm still looking for algorithms, I found them different in java, c and phyton ...
    I've read many guides and color studies ..
    Only now I'm more confused ... just have time to write other code to try the things you learned
     
  7. MrKim

    MrKim Well-Known Member Licensed User

    What I would really like to have is the drop down list of colors you have in the Designer. Is code for that available?
    I am talking about just the drop down pick list of colors, not the pallet.
     
    Last edited: Jun 11, 2017
  8. derez

    derez Expert Licensed User

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