Android Code Snippet Color Picker

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

SS-2017-06-05_11.53.27.png


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.
B4X:
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, 20dip, Colors.Black, False, 3dip)
   pnlOverlay.Invalidate
End Sub

File is available here: https://www.b4x.com/android/forum/attachments/colorpicker-zip.56334/
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
And now as a custom dialog:
SS-2017-06-05_13.06.47.png


Usage:
B4X:
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+.
 

Attachments

  • ColorPickerAsDialog.zip
    399.6 KB · Views: 1,621

DavideV

Active Member
Licensed User
Longtime 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
 

DavideV

Active Member
Licensed User
Longtime User
Thanks Star Dust, looks interesting, perharps what i was looking for :)
(Grazie Star Dust potrebbe essere quello che cercavo!)
 

Star-Dust

Expert
Licensed User
Longtime 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
 

MrKim

Well-Known Member
Licensed User
Longtime 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:
Top