changeset 2735:970cbcdb68f9

Android: This is kind of a hacky solution, but use a while loop to figure out... the offset from the initial color value when creating the color picker.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Thu, 16 Dec 2021 01:12:04 +0000
parents cd3c7740e352
children 09775418c776
files android/DWindows.kt
diffstat 1 files changed, 30 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/android/DWindows.kt	Wed Dec 15 15:49:39 2021 +0000
+++ b/android/DWindows.kt	Thu Dec 16 01:12:04 2021 +0000
@@ -3,6 +3,7 @@
 package org.dbsoft.dwindows
 
 import android.R
+import android.animation.ArgbEvaluator
 import android.annotation.SuppressLint
 import android.app.Activity
 import android.app.Dialog
@@ -5377,6 +5378,25 @@
         return retval
     }
 
+    // No reverse evaluate function to get the offset from a color in a range...
+    // So we do a hacky while loop to test offsets in the range to see if we can
+    // find the color and return the offset... return -1F on failure
+    fun colorSliderOffset(chosenColor: Int, startColor: Int, endColor: Int): Float
+    {
+        val argbEvaluator = android.animation.ArgbEvaluator()
+        var testOffset = 0F
+
+        while(testOffset <= 1F) {
+            val testColor = argbEvaluator.evaluate(testOffset, startColor, endColor) as Int
+
+            if(testColor == chosenColor) {
+                return testOffset
+            }
+            testOffset += 0.001F
+        }
+        return -1F
+    }
+
     fun colorChoose(color: Int, alpha: Int, red: Int, green: Int, blue: Int): Int
     {
         var retval: Int = color
@@ -5417,9 +5437,16 @@
                 colorChosen = Color.rgb(red, green, blue)
                 colorWheel.rgb = colorChosen
                 gradientBar.setBlackToColor(colorWheel.rgb)
-                var hsv = FloatArray(3)
-                Color.colorToHSV(colorChosen, hsv)
-                gradientBar.offset = hsv[2]
+                val testOffset = colorSliderOffset(colorChosen, gradientBar.startColor, gradientBar.endColor)
+                if(testOffset < 0F) {
+                    // If our test method didn't work... convert to HSV
+                    // and use the brightness value as the slider offset
+                    var hsv = FloatArray(3)
+                    Color.colorToHSV(colorChosen, hsv)
+                    gradientBar.offset = hsv[2]
+                } else {
+                    gradientBar.offset = testOffset
+                }
                 display.setBackgroundColor(colorChosen)
                 colorWheel.colorChangeListener = { rgb: Int ->
                     gradientBar.setBlackToColor(rgb)