Hello everyone,
I found a nice Color Picker dialog inside examples, and simplified a bit
to work much better on my own device which use API 1.6.
The original one was to slow and it didn't fit the screen right.
Anyway, do whatever you want to do with this.
Ciao,
Goran
package osg.AndroidExample;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
public class ColorPickerDialog
extends Dialog {
public interface OnColorChangeListener {
void colorChange(int color);
}
private OnColorChangeListener tListener;
private int tInitialColor;
private class ColorPickerView extends View {
private Paint tPaint;
private float tCurrentHue = 0;
private int tCurrentColor;
private final int[] tHueGradientColors = new int [258];
public ColorPickerView(Context context, OnColorChangeListener listener, int color) {
super(context);
float[] newHSV = new float[3];
Color.colorToHSV(color, newHSV);
tCurrentHue = newHSV[0];
tCurrentColor = color;
int index = 0;
for(float i=0; i<256; i += 256/42 , index++){
tHueGradientColors[index] = Color.rgb(255, 0, (int)i);
}
for(float i=0; i<256; i += 256/42 , index++){
tHueGradientColors[index] = Color.rgb(255-(int)i, 0, 255);
}
for(float i=0; i<256; i += 256/42 , index++){
tHueGradientColors[index] = Color.rgb(0, (int) i, 255);
}
for(float i=0; i<256; i += 256/42 , index++){
tHueGradientColors[index] = Color.rgb(0, 255, 255-(int)i);
}
for(float i=0; i<256; i += 256/42 , index++){
tHueGradientColors[index] = Color.rgb((int)i, 255, 0);
}
for(float i=0; i<256; i += 256/42 , index++){
tHueGradientColors[index] = Color.rgb(255, 255-(int)i, 0);
}
tPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
private int getCurrentGradientColor(){
int currentHue = 255 - (int)(tCurrentHue*255/360);
int index = 0;
for (float i=0; i<256; i += 256/42, index++){
if (index == currentHue) return Color.rgb(255, 0, (int) i );
}
for (float i=0; i<256; i += 256/42, index++){
if (index == currentHue) return Color.rgb(255-(int)i, 0, 255 );
}
for (float i=0; i<256; i += 256/42, index++){
if (index == currentHue) return Color.rgb(0, (int) i, 255 );
}
for (float i=0; i<256; i += 256/42, index++){
if (index == currentHue) return Color.rgb(0, 255, 255-(int) i );
}
for (float i=0; i<256; i += 256/42, index++){
if (index == currentHue) return Color.rgb((int) i, 255, 0 );
}
for (float i=0; i<256; i += 256/42, index++){
if (index == currentHue) return Color.rgb(255, 255-(int) i, 0);
}
return Color.RED;
}
private void updateGradientColors(){
final int actualColor = getCurrentGradientColor();
tCurrentColor = actualColor;
}
@Override
protected void onDraw(Canvas canvas){
int translatedHue = 255 - (int)(tCurrentHue*255/360);
for(int x=0; x<240; x++){
if(translatedHue != x){
tPaint.setColor(tHueGradientColors[x]);
tPaint.setStrokeWidth(1);
}
else{
tPaint.setColor(Color.WHITE);
tPaint.setStrokeWidth(3);
}
canvas.drawLine(x, 0, x, 50, tPaint);
}
updateGradientColors();
setBackgroundColor(tCurrentColor);
}
@Override
protected void onMeasure(int width,int height){
setMeasuredDimension(240, 100);
setBackgroundColor(tCurrentColor);
}
@Override
public boolean onTouchEvent(MotionEvent event){
if(event.getAction() != MotionEvent.ACTION_DOWN && event.getAction() != MotionEvent.ACTION_MOVE && event.getAction() != MotionEvent.ACTION_UP) return true;
float x = event.getX();
float y = event.getY();
if(x>0 && x<240 && y>0 && y<50){
tCurrentHue = (255-x)*360/255;
updateGradientColors();
invalidate();
}
if( x>0 && x <240 && y>0 && y<50){
if(event.getAction() == MotionEvent.ACTION_DOWN ||
event.getAction() == MotionEvent.ACTION_MOVE)
{
}
if(event.getAction() == MotionEvent.ACTION_UP){
tListener.colorChange(tCurrentColor);
}
invalidate();
}
return true;
}
}
public ColorPickerDialog(Context context, OnColorChangeListener listener, int initialColor){
super(context);
tListener = listener;
tInitialColor = initialColor;
}
@Override
protected void onCreate( Bundle savedInstanceState){
super.onCreate(savedInstanceState);
OnColorChangeListener l = new OnColorChangeListener(){
public void colorChange(int color){
tListener.colorChange(color);
dismiss();
}
};
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(new ColorPickerView(getContext(),l,tInitialColor));
}
}
_______________________________________________
osg-submissions mailing list
[email protected]
http://lists.openscenegraph.org/listinfo.cgi/osg-submissions-openscenegraph.org