Android Question [Solved]How to get the panel 's color when using the library ?(I don't know that the panel's color will be)

Theera

Expert
Licensed User
Longtime User
I've created a CustomView (XUI) library which display the value of percent as code's belows

B4X:
cvs.DrawText(Progress & "%", mBase.Width / 2, mBase.Height - 60dip, xui.CreateDefaultFont(18), xui.Color_RGB(165,42,42), "CENTER")


I need to contrast color 's the value of percent with the panel 's color ,xui.Color_RGB(165,42,42) is fixed color, which I don't know its color because the value of percent
created as customview(xui). How to get the panel 's color ? In order to code in the library. (I don't need edit xui.Color_RGB(165,42,42) every time when is used.)
 
Solution
Addition EnhanceColor() function for making it be clearer.

Usage:
B4X:
    cvs.DrawText(Progress & "%", mBase.Width / 2, mBase.Height - 60dip, xui.CreateDefaultFont(18),  EnhanceColor(GetComplementaryColor(mBase.Parent.Color)), "CENTER")

B4X:
'ฟังก์ชันหลักสำหรับปรับสีให้ชัดเจน
Sub EnhanceColor(OriginalColor As Int) As Int
    ' แยกค่า RGB
    Dim r As Int = Bit.And(Bit.UnsignedShiftRight(OriginalColor, 16), 0xFF)
    Dim g As Int = Bit.And(Bit.UnsignedShiftRight(OriginalColor, 8), 0xFF)
    Dim b As Int = Bit.And(OriginalColor, 0xFF)
    ' แปลง RGB เป็น HSV
    Dim hsv() As Float = RGBtoHSV(r, g, b)
 
 
    Dim hue As Float = hsv(0)
    Dim saturation As Float = hsv(1)
    Dim value As Float = hsv(2)
 
    ' กำหนดเกณฑ์การปรับสี...

Theera

Expert
Licensed User
Longtime User
I 've just asked AI, he advise this.

B4X:
'In CustomView Class  
Public Sub GetParentColorUsingReflection As Int
    Try
        Dim r As Reflector
        r.Target = mBase.Parent
        
        'for Panel
        If r.GetType.Contains("Panel") Then
            Return r.GetField("Color")
        End If
        
    Catch
        Log("Reflection failed")
    End Try
    Return Colors.White
End Sub
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Another strategy for solved , ContrastColor() is found this

B4X:
'In CustomView Class 
Public Sub GetParentColor As Int
    Try
        If mBase.Parent=Null Then Return xui.Color_White
        Dim jo As JavaObject=mBase.Parent
       
        'Check field color
        If HasField(jo,"color") Then
            Return jo.GetField("color")
        End If
       
    Catch
        Log("Get parent color failed" & LastException.Message)
    End Try
    Return xui.Color_White
End Sub

'Check Field

Private Sub HasField(jo As JavaObject,fieldName As String) As Boolean
     Try
         Dim cls As JavaObject=jo.GetField("java.lang.Object.class")
        Dim fields() As Object= cls.RunMethod("getDeclaredFields",Null)
       
        For Each field As JavaObject In fields
            Dim name As String=field.RunMethod("getName",Null)
            If name=fieldName Then
                 Return True
            End If
        Next
    Catch
        Log(LastException)
       
    End Try
    Return False
End Sub

Usage:

B4X:
cvs.DrawText(Progress & "%", mBase.Width / 2, mBase.Height - 60dip, xui.CreateDefaultFont(18), ContrastColor(GetParentColor), "CENTER")
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Here is the best of my answer of solution

Usage:
B4X:
    cvs.DrawText(Progress & "%", mBase.Width / 2, mBase.Height - 60dip, xui.CreateDefaultFont(18), GetComplementaryColor(mBase.Parent.Color), "CENTER")




B4X:
'  Complementary Color
Sub GetComplementaryColor(color As Int) As Int
    ' แยกค่า RGB
    Dim r As Int = Bit.And(Bit.UnsignedShiftRight(color, 16), 0xFF)
    Dim g As Int = Bit.And(Bit.UnsignedShiftRight(color, 8), 0xFF)
    Dim b As Int = Bit.And(color, 0xFF)
   
    ' แปลง RGB เป็น HSV
    Dim hsv() As Float = RGBtoHSV(r, g, b)
   
    ' หมุนค่า Hue 180 องศา เพื่อหาสีตรงข้าม
    Dim newHue As Float = hsv(0) + 180
    If newHue >= 360 Then newHue = newHue - 360
   
    ' แปลงกลับเป็น RGB
    Dim newRGB() As Int = HSVtoRGB(newHue, hsv(1), hsv(2))
   
    Return xui.color_RGB(newRGB(0), newRGB(1), newRGB(2))
End Sub

' แปลง RGB เป็น HSV
Sub RGBtoHSV(r As Int, g As Int, b As Int) As Float()
    Dim rf As Float = r / 255.0
    Dim gf As Float = g / 255.0
    Dim bf As Float = b / 255.0
   
    Dim maxVal As Float = Max(Max(rf, gf), bf)
    Dim minVal As Float = Min(Min(rf, gf), bf)
    Dim delta As Float = maxVal - minVal
   
    Dim h, s, v As Float
   
    ' คำนวณ Value
    v = maxVal
   
    ' คำนวณ Saturation
    If maxVal = 0 Then
        s = 0
    Else
        s = delta / maxVal
    End If
   
    ' คำนวณ Hue
    If delta = 0 Then
        h = 0
    Else If maxVal = rf Then
        h = 60 * (((gf - bf) / delta) Mod 6)
    Else If maxVal = gf Then
        h = 60 * (((bf - rf) / delta) + 2)
    Else
        h = 60 * (((rf - gf) / delta) + 4)
    End If
   
    If h < 0 Then h = h + 360
   
    Return Array As Float(h, s, v)
End Sub

' แปลง HSV เป็น RGB
Sub HSVtoRGB(h As Float, s As Float, v As Float) As Int()
    Dim c As Float = v * s
    Dim x As Float = c * (1 - Abs(((h / 60) Mod 2) - 1))
    Dim m As Float = v - c
   
    Dim r1, g1, b1 As Float
   
    If h >= 0 And h < 60 Then
        r1 = c: g1 = x: b1 = 0
    Else If h >= 60 And h < 120 Then
        r1 = x: g1 = c: b1 = 0
    Else If h >= 120 And h < 180 Then
        r1 = 0: g1 = c: b1 = x
    Else If h >= 180 And h < 240 Then
        r1 = 0: g1 = x: b1 = c
    Else If h >= 240 And h < 300 Then
        r1 = x: g1 = 0: b1 = c
    Else
        r1 = c: g1 = 0: b1 = x
    End If
   
    Dim r As Int = (r1 + m) * 255
    Dim g As Int = (g1 + m) * 255
    Dim b As Int = (b1 + m) * 255
   
    Return Array As Int(r, g, b)
End Sub
 
Upvote 0

Theera

Expert
Licensed User
Longtime User
Addition EnhanceColor() function for making it be clearer.

Usage:
B4X:
    cvs.DrawText(Progress & "%", mBase.Width / 2, mBase.Height - 60dip, xui.CreateDefaultFont(18),  EnhanceColor(GetComplementaryColor(mBase.Parent.Color)), "CENTER")

B4X:
'ฟังก์ชันหลักสำหรับปรับสีให้ชัดเจน
Sub EnhanceColor(OriginalColor As Int) As Int
    ' แยกค่า RGB
    Dim r As Int = Bit.And(Bit.UnsignedShiftRight(OriginalColor, 16), 0xFF)
    Dim g As Int = Bit.And(Bit.UnsignedShiftRight(OriginalColor, 8), 0xFF)
    Dim b As Int = Bit.And(OriginalColor, 0xFF)
    ' แปลง RGB เป็น HSV
    Dim hsv() As Float = RGBtoHSV(r, g, b)
 
 
    Dim hue As Float = hsv(0)
    Dim saturation As Float = hsv(1)
    Dim value As Float = hsv(2)
 
    ' กำหนดเกณฑ์การปรับสี
    Dim newHue As Float = hue
    Dim newSaturation As Float = saturation
    Dim newValue As Float = value
 
    ' ปรับความเข้มของสี (Saturation)
    If saturation < 0.3 Then
        ' สีจางมาก -> เป็นสีเทาหรือขาว
        If value < 0.3 Then
            Return Colors.Black ' เทาเข้ม -> ดำ
        Else If value > 0.8 Then
            Return Colors.White ' เทาอ่อน -> ขาว
        Else
            Return Colors.Gray ' เทาปานกลาง
        End If
    End If
 
    ' ปรับค่าความสว่าง (Value)
    If value < 0.2 Then
        Return Colors.Black ' สีเข้มมาก -> ดำ
    Else If value > 0.9 And saturation < 0.5 Then
        Return Colors.White ' สีอ่อนมาก -> ขาว
    End If
 
    ' ปรับ Hue ให้เข้าใกล้สีหลัก
    newHue = AdjustHueToMainColor(hue)
 
    ' เพิ่มความเข้มของสี
    newSaturation = Min(1.0, saturation * 1.3)
 
    ' ปรับความสว่างให้เหมาะสม
    If value < 0.4 Then
        newValue = Min(0.8, value * 1.5)
    Else If value > 0.8 Then
        newValue = Max(0.3, value * 0.8)
    End If
 
 
    ' แปลงกลับเป็น RGB
    Dim newRGB() As Int = HSVtoRGB(newHue, newSaturation, newValue)
 
    Return xui.color_RGB(newRGB(0), newRGB(1), newRGB(2))
 
End Sub

' ฟังก์ชันปรับ Hue ให้เข้าใกล้สีหลัก
Sub AdjustHueToMainColor(hue As Float) As Float
    ' แบ่งช่วงสีหลัก (0-360 องศา)
    ' แดง: 0-30, 330-360
    ' ส้ม: 30-60
    ' เหลือง: 60-90
    ' เขียว: 90-150
    ' ฟ้า: 150-210
    ' น้ำเงิน: 210-270
    ' ม่วง: 270-330
 
    Dim hueInDegrees As Float = hue * 360
 
    If (hueInDegrees >= 0 And hueInDegrees <= 30) Or (hueInDegrees >= 330 And hueInDegrees <= 360) Then
        ' แดง
        If hueInDegrees > 15 And hueInDegrees < 330 Then
            Return 0 ' แดงแท้
        End If
    Else If hueInDegrees > 30 And hueInDegrees <= 60 Then
        ' ส้ม
        Return 30 / 360 ' ส้มแท้
    Else If hueInDegrees > 60 And hueInDegrees <= 90 Then
        ' เหลือง
        Return 60 / 360 ' เหลืองแท้
    Else If hueInDegrees > 90 And hueInDegrees <= 150 Then
        ' เขียว
        Return 120 / 360 ' เขียวแท้
    Else If hueInDegrees > 150 And hueInDegrees <= 210 Then
        ' ฟ้า/น้ำเงินอ่อน
        Return 180 / 360 ' ฟ้าแท้
    Else If hueInDegrees > 210 And hueInDegrees <= 270 Then
        ' น้ำเงิน
        Return 240 / 360 ' น้ำเงินแท้
    Else If hueInDegrees > 270 And hueInDegrees <= 330 Then
        ' ม่วง
        Return 300 / 360 ' ม่วงแท้
    End If
 
    Return hue ' คืนค่าเดิมถ้าไม่อยู่ในช่วงใดๆ
End Sub
 
Upvote 0
Solution
Top