comparison android/DWindows.kt @ 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
comparison
equal deleted inserted replaced
2734:cd3c7740e352 2735:970cbcdb68f9
1 // (C) 2021 Brian Smith <brian@dbsoft.org> 1 // (C) 2021 Brian Smith <brian@dbsoft.org>
2 // (C) 2019 Anton Popov 2 // (C) 2019 Anton Popov
3 package org.dbsoft.dwindows 3 package org.dbsoft.dwindows
4 4
5 import android.R 5 import android.R
6 import android.animation.ArgbEvaluator
6 import android.annotation.SuppressLint 7 import android.annotation.SuppressLint
7 import android.app.Activity 8 import android.app.Activity
8 import android.app.Dialog 9 import android.app.Dialog
9 import android.app.NotificationChannel 10 import android.app.NotificationChannel
10 import android.app.NotificationManager 11 import android.app.NotificationManager
5375 } 5376 }
5376 5377
5377 return retval 5378 return retval
5378 } 5379 }
5379 5380
5381 // No reverse evaluate function to get the offset from a color in a range...
5382 // So we do a hacky while loop to test offsets in the range to see if we can
5383 // find the color and return the offset... return -1F on failure
5384 fun colorSliderOffset(chosenColor: Int, startColor: Int, endColor: Int): Float
5385 {
5386 val argbEvaluator = android.animation.ArgbEvaluator()
5387 var testOffset = 0F
5388
5389 while(testOffset <= 1F) {
5390 val testColor = argbEvaluator.evaluate(testOffset, startColor, endColor) as Int
5391
5392 if(testColor == chosenColor) {
5393 return testOffset
5394 }
5395 testOffset += 0.001F
5396 }
5397 return -1F
5398 }
5399
5380 fun colorChoose(color: Int, alpha: Int, red: Int, green: Int, blue: Int): Int 5400 fun colorChoose(color: Int, alpha: Int, red: Int, green: Int, blue: Int): Int
5381 { 5401 {
5382 var retval: Int = color 5402 var retval: Int = color
5383 5403
5384 // This can't be called from the main thread 5404 // This can't be called from the main thread
5415 5435
5416 dialog.setContentView(layout) 5436 dialog.setContentView(layout)
5417 colorChosen = Color.rgb(red, green, blue) 5437 colorChosen = Color.rgb(red, green, blue)
5418 colorWheel.rgb = colorChosen 5438 colorWheel.rgb = colorChosen
5419 gradientBar.setBlackToColor(colorWheel.rgb) 5439 gradientBar.setBlackToColor(colorWheel.rgb)
5420 var hsv = FloatArray(3) 5440 val testOffset = colorSliderOffset(colorChosen, gradientBar.startColor, gradientBar.endColor)
5421 Color.colorToHSV(colorChosen, hsv) 5441 if(testOffset < 0F) {
5422 gradientBar.offset = hsv[2] 5442 // If our test method didn't work... convert to HSV
5443 // and use the brightness value as the slider offset
5444 var hsv = FloatArray(3)
5445 Color.colorToHSV(colorChosen, hsv)
5446 gradientBar.offset = hsv[2]
5447 } else {
5448 gradientBar.offset = testOffset
5449 }
5423 display.setBackgroundColor(colorChosen) 5450 display.setBackgroundColor(colorChosen)
5424 colorWheel.colorChangeListener = { rgb: Int -> 5451 colorWheel.colorChangeListener = { rgb: Int ->
5425 gradientBar.setBlackToColor(rgb) 5452 gradientBar.setBlackToColor(rgb)
5426 display.setBackgroundColor(gradientBar.argb) 5453 display.setBackgroundColor(gradientBar.argb)
5427 colorChosen = gradientBar.argb 5454 colorChosen = gradientBar.argb