Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Draw HSV circle at runtime

I would like to have a color picker in my app looking like this

Something like this

I've tried fill bitmap pixel by pixel, tried use canvas with drawArc() method. Both ways are not good for performance.

Any ideas?

like image 904
Sergey Dmitriev Avatar asked Nov 07 '12 09:11

Sergey Dmitriev


1 Answers

It might not be precisely what it should be (related to colors, saturation and so on), but here is something that start to look like what you want....

import android.graphics.*;
import android.graphics.drawable.Drawable;

public class HSV_Circle extends Drawable {
    Paint p = new Paint();

    @Override
    public void draw(Canvas canvas) {
        int width = canvas.getWidth();
        int height = canvas.getHeight();
        int min = Math.min(width,height);

        RadialGradient radial_gradient = new RadialGradient(width/2, height/2, min/2, 0xFFFFFFFF,
            0x00FFFFFF, android.graphics.Shader.TileMode.CLAMP);

        int colors[] = new int[13];
        float hsv[] = new float[3];
        hsv[1]=1;
        hsv[2]=1;
        for (int i=0; i<12; i++) {
            hsv[0] = (360 / 12) * i;
            colors[i] = Color.HSVToColor(hsv);
        }
        colors[12] = colors[0];

        SweepGradient sweep_gradient = new SweepGradient(width/2, height/2, colors, null);

        ComposeShader shader = new ComposeShader(sweep_gradient, radial_gradient, PorterDuff.Mode.SRC_OVER);

        p.setDither(true);
        p.setShader(shader);

        canvas.drawCircle(width/2, height/2, min/2, p);
    }

    @Override
    public void setAlpha(int i) {
    }

    @Override
    public void setColorFilter(ColorFilter colorFilter) {
    }

    @Override
    public int getOpacity() {
        return PixelFormat.OPAQUE;
    }
}
like image 191
Matthieu Avatar answered Oct 23 '22 19:10

Matthieu