Changing color according to seek bar value

Sarah Sami picture Sarah Sami · Apr 13, 2014 · Viewed 9.8k times · Source

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?

Answer

Alex Salauyou picture Alex Salauyou · Apr 13, 2014

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