Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to change ColorBar color on new colorSeeds #35

Closed
EddieXu123 opened this issue Apr 7, 2022 · 7 comments
Closed

Add option to change ColorBar color on new colorSeeds #35

EddieXu123 opened this issue Apr 7, 2022 · 7 comments

Comments

@EddieXu123
Copy link

Hi there! I was trying to build an app with two ColorSeekBars - one to control the hue of my view and one to control the saturation. When I change my hue colorSeekBar, the colorSeeds of my saturation colorSeekBar change to (Color.WHITE, hueSeekBar.getColor()). This works well, but when I try dragging the saturationSeekBar, the color of the thumb remains unchanged (the original value). How would I go about doing this? Thank you!

App

@EddieXu123
Copy link
Author

EddieXu123 commented Apr 7, 2022

Ahh nevermind, I found a tutorial a minute after sending this Issue: https://www.geeksforgeeks.org/how-to-add-colorseekbar-in-android/

Great stuff btw!

PS. I tried adding the stuff from the tutorial and it works, but I was wondering if you could include the showAlphaBar option back in the latest version?

@EddieXu123 EddieXu123 reopened this Apr 7, 2022
@rtugeek
Copy link
Owner

rtugeek commented Apr 8, 2022

@EddieXu123 showAlphaBar was removed in version 2.0.*, please use AlphaSeekbar to control the alpha channel value, use alphaSeekBar.setOnAlphaChangeListener to watch value changed.

<com.rtugeek.android.colorseekbar.AlphaSeekBar 
    android:id="@+id/alphaSeekBar"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" />

If you want to merge AlphaSeekBar's alpha into ColorSeekBar's color, use this:

int color = colorSeekBar.getColor()
int alpha = alphaSeekBar.getAlphaValue()
Color.argb(alpha, Color.red(color), Color.green(color), Color.blue(color))
/**
 * if you use Kotlin, this extension function may help
 * mix alpha value  (0-255) into current color
 */
fun Int.mixAlpha(@IntRange(from = 0, to = 255) alpha: Int): Int {
    return Color.argb(alpha, Color.red(this), Color.green(this), Color.blue(this))
}

@EddieXu123
Copy link
Author

Thank you! I was also wondering, for now the .getColor() of the bar seems to return an integer in the negatives. I was just wondering how you would transfer this number to hsv? Or if not hsv RGB? Thanks!

@rtugeek
Copy link
Owner

rtugeek commented Apr 11, 2022

val hsv = FloatArray(3)
Color.colorToHSV(colorInt,hsv)
//hsv[0] is Hue \([0..360[\)
//hsv[1] is Saturation \([0...1]\)
//hsv[2] is Value \([0...1]\)

// hsv to color int
Color.HSVToColor(hsv)

@EddieXu123
Copy link
Author

EddieXu123 commented Apr 13, 2022

Thank you! Sorry, I'm really new to Android development so I just had one last question: If I want to add another seekbar to change the brightness of my view (using a ColorMatrixColorFilter), I was wondering how to go about that? This is what I'm doing so far:

public ColorMatrixColorFilter brightIt(int fb) {
        ColorMatrix cmB = new ColorMatrix();
        cmB.set(new float[] {
                1, 0, 0, 0, fb,
                0, 1, 0, 0, fb,
                0, 0, 1, 0, fb,
                0, 0, 0, 1, 0   });

        ColorMatrix colorMatrix = new ColorMatrix();
        colorMatrix.set(cmB);
        
        ColorMatrixColorFilter f = new ColorMatrixColorFilter(colorMatrix);
        return f;
    }

And then when changing the brightness slider:

brightness_seekbar.setOnColorChangeListener((colorBarPosition, alphaBarPosition, color) -> {
         float[] hsv = new float[3];
         Color.colorToHSV(color,hsv);
         imView.setColorFilter(brightIt(hsv[2]));
        
});

(Assuming the code is correct -> I did some reformatting, just trying to figure out how to get my ImageView to remain the same saturation_seekbar color while changing the brightness using that ColorMatrix). Thank you so much for all your help!

@rtugeek
Copy link
Owner

rtugeek commented Apr 14, 2022

https://stackoverflow.com/a/9226771/4639921
This answer may help you. Use PorterDuffColorFilter instead of ColorMatrixColorFilter

@EddieXu123
Copy link
Author

EddieXu123 commented Apr 15, 2022

Got it, thank you so much! It works now after using a GradientDrawable and that helper function :)

@rtugeek rtugeek closed this as completed Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants