[wish] Get Color(of a label)

AscySoft

Active Member
Licensed User
Longtime User
I wanna create some fancy visual effects when one press some buttons/labels etc, and I need to get the current value of the background color of each view (label, button, panel etc - I know is Int). Practically any view that has a "Color" property.
But for now a "Get" label color will be enough.

I was surprised to discover that this function didn't exist.
Could one create such a function?
 

AscySoft

Active Member
Licensed User
Longtime User
so, Erel was saying on that post
<<Setting the Color property actually assigns a ColorDrawable as the view's drawable property>>
But how to read that? How to read a view's drawable property? Reflection?
There's no such function, you will have to use TAG
But what if someone use a layout with different labels colors on design. How to store that in tags? It's practically impossible.
 

AscySoft

Active Member
Licensed User
Longtime User
In most cases the background is not a ColorDrawable and therefore there isn't any single color value that you can access.

Even if the view has a solid color then its drawable might be a GradientDrawable as ColorDrawable doesn't support round corners.

Let's say that one create a label or a panel with round corners, and setting color (from designer) to "Blue"(DRAWABLE:ColorDrawable, CORNER RADIUS:5, COLOR: Blue)
Are you saying that in this scenario it's impossible to "read" from code the label/panel color?

Given the above situation but setting CORNER RADIUS to zero, is it possible to read somehow color value now?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Seems like that even in the case of the simple ColorDrawable it is not so simple unless you target Android 3+.
eclipse - No method "getColor" in ColorDrawable in android - Stack Overflow

There is no getColor method.

The suggestion in this link is to test the actual color. This is a good idea. You can use Canvas.DrawDrawable to draw the background on a bitmap and then use Bitmap.GetPixel to check the color.
 

AscySoft

Active Member
Licensed User
Longtime User
OK, thanks for your reply. I was thinking of a function who will get pixel color of a given position(x,y)- evidently works only for views that are "on top" and visible, with uniform/solid colors.

But could you be so kind enough to elaborate a bit your solution?
Given the simple situation explain above (which is not so simple), could you create some sort of "simple" example. It will mean a lot. But no pressure, it's no hurry. I just want to learn new things. (Android 3.0 is OK too if it's impossible - I know a strong word - in earlier versions)

A big thanks(I forgot)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Here:
B4X:
Sub Globals
   Dim Button1 As Button
   Dim cvs As Canvas
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("1")
   Dim bmp As Bitmap
   bmp.InitializeMutable(50dip, 50dip)
   cvs.Initialize2(bmp)
   
   
   Log(GetViewsColor(Button1))
   Log(Colors.Red)
End Sub

Sub GetViewsColor(v As View) As Int
   Dim destRect As Rect
   destRect.Initialize(0, 0, cvs.Bitmap.Width, cvs.Bitmap.Height)
   cvs.DrawDrawable(v.Background, destRect)
   Return cvs.Bitmap.GetPixel(5dip, 5dip)
End Sub

I created a layout with a button with red background.
 
Top