Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing color according to seek bar value

I am trying to change color according to value of seek bar. I'd like colors to appear in wide range.

Here is what I tried, but it doesn't give me colors I want:

seekBarColor.setOnSeekBarChangeListener( new OnSeekBarChangeListener() {
        int progress = 0;
          @Override
          public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
            progress = progresValue;
            int lower = Color.argb(0xFF,progress*0x10000, progress*0x100, progress)+0xff000000;
            color.setBackgroundColor(lower);
          }

          @Override
          public void onStartTrackingTouch(SeekBar seekBar) {

          }

          @Override
          public void onStopTrackingTouch(SeekBar seekBar) {
              db.updateColor("");
          }
    });

Any ideas?

like image 816
Sarah Sami Avatar asked Apr 13 '14 13:04

Sarah Sami


2 Answers

If you need only bright, saturated colors, use Color.HSVToColor() instead of setting R, G and B components directly:

float[] hsvColor = {0, 1, 1};
// generate only hue component in range [0, 360),
// leaving saturation and brightness maximum possible
hsvColor[0] = 360f * progress / maxProgress;
view.setBackgroundColor(Color.HSVToColor(hsvColor));

This code will set color starting from red, then change smoothly to orange, yellow, green, blue and magenta back to red as progress is changing from 0 to maxProgress

like image 61
Alex Salauyou Avatar answered Oct 04 '22 05:10

Alex Salauyou


You have to pass 0-255 values to Color.argb(int,int,int,int) method.

This might work?

Color.argb(0xFF, progress, progress, progress) //will return some gray color based on progress. 

You might have to scale your progress value based on the min and max value. For example if you have progress 0-100 then you should calculate your color values with progress * 255 / 100

like image 35
Sipka Avatar answered Oct 04 '22 05:10

Sipka