package com.algobase.share.dialog; import java.util.ArrayList; import android.os.Handler; import android.os.Bundle; import android.app.Dialog; import android.content.Context; import android.text.Html; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.LinearGradient; import android.graphics.Paint; import android.graphics.Typeface; import android.graphics.Shader; import android.graphics.Shader.TileMode; import android.graphics.ComposeShader; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.graphics.drawable.GradientDrawable; import android.text.Spannable; import android.text.SpannableString; import android.text.style.RelativeSizeSpan; import android.view.View; import android.view.View.OnTouchListener; import android.view.View.OnClickListener; import android.view.Window; import android.view.Gravity; import android.view.MotionEvent; import android.view.ContextThemeWrapper; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; import android.widget.TextView; import android.widget.Button; import android.widget.HorizontalScrollView; import android.widget.Toast; import com.algobase.share.system.*; import com.algobase.share.compat.*; public class ColorPickerDialog extends Dialog { private static int[] styles = { StyleCompat.Theme_Holo, StyleCompat.Theme_Holo_Light, StyleCompat.Theme_Material, StyleCompat.Theme_Material_Light }; private static int def_style = 1; public interface OnColorResultListener { void onColorResult(int clr1, int clr2); } private OnColorResultListener cc_listener; private Context context; private int initialColor; private ArrayList predefinedColors; private String title; private int width; private int height; private int button_h; private int view_h; // private TextView titleView; private Button but1; private Button but2; private Button but3; private Button[] preButtons; ColorPickerView colorPickerView; private static class ColorPickerView extends View { private Paint paint; private float current_hue = 0; private int current_rgb = 0; private int width = 600; private int height = 800; private int huebar_height = 100; private int matrix_width = width; private int matrix_height = height - huebar_height; private float hue_xoff = 0; private float hue_xcur = 0; private float xoff = 0; private float yoff = 0; private float xcur = 0; private float ycur = 0; private boolean button_down = false; private boolean hue_down = false; public static int text_color(int clr) { //if (Color.red(clr) + Color.green(clr) + Color.blue(clr) > 600) if (Color.red(clr) + Color.green(clr) + Color.blue(clr) > 400) return Color.BLACK; else return Color.WHITE; } public void onColorChanged(int clr) {} public int getCurrentColor() { return current_rgb; } ColorPickerView(Context c, int clr, int w, int h, int bar_h) { super(c); setLayerType(View.LAYER_TYPE_SOFTWARE,null); width = w; height = h; huebar_height = bar_h; matrix_width = width; matrix_height = height - huebar_height; paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextAlign(Paint.Align.CENTER); paint.setTextSize(12); setCurrentColor(clr); } public void setCurrentColor(int clr) { current_rgb = clr; // get hue from current color float[] hsv = new float[3]; Color.colorToHSV(clr, hsv); current_hue = hsv[0]; current_hue = (int)(0.5f + current_hue); hue_xoff = matrix_width/2 - (current_hue*matrix_width)/360; xoff = matrix_width*(0.5f-hsv[1]); yoff = matrix_height*(hsv[2]-0.5f); xcur = 0; ycur = 0; updateCurrentColor(); } private int hsv_to_color(float h, float s, float v) { float[] hsv = {h,s,v}; return Color.HSVToColor(hsv) | 0xff000000; } private void updateCurrentColor() { float h = current_hue; float s = 0.5f -(xoff+xcur)/matrix_width; float v = 0.5f +(yoff+ycur)/matrix_height; current_rgb = hsv_to_color(h,s,v); onColorChanged(current_rgb); invalidate(); } /* @Override protected void onSizeChanged(int w, int h, int old_w, int old_h) { view_width = w; view_height = h; matrix_width = view_width; matrix_height = view_height; super.onSizeChanged(w,h,old_w,old_h); } */ @Override protected void onDraw(Canvas canvas) { int canvas_width = canvas.getWidth(); int canvas_height = canvas.getHeight(); //background paint.setStyle(Paint.Style.FILL); paint.setColor(0xffc0c0c0); canvas.drawRect(0,0,canvas_width,canvas_height,paint); // shaded box int rgb = hsv_to_color(current_hue,1,1); Shader grad1 = new LinearGradient(0,0,0,matrix_height, 0xffffffff, 0xff000000, TileMode.CLAMP); Shader grad2 = new LinearGradient(0,0,matrix_width,0, 0xffffffff, rgb, TileMode.CLAMP); ComposeShader shader = new ComposeShader(grad1,grad2, PorterDuff.Mode.MULTIPLY); canvas.save(); float dx = xoff + xcur; float dy = huebar_height + yoff + ycur; canvas.translate(dx,dy); paint.setStyle(Paint.Style.FILL); paint.setShader(shader); canvas.drawRect(0, 0, matrix_width, matrix_height, paint); paint.setShader(null); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1); paint.setColor(Color.BLACK); canvas.drawRect(0, 0, matrix_width, matrix_height, paint); canvas.restore(); float dpi = getResources().getDisplayMetrics().densityDpi; // draw circle and crosshairs int textColor = text_color(current_rgb); float r = (6 * dpi)/160.0f; //float w = (1 * dpi)/160.0f; float x = matrix_width/2 + 1; float y = huebar_height + matrix_height/2; paint.setStyle(Paint.Style.STROKE); paint.setColor(textColor); //paint.setStrokeWidth(w); paint.setStrokeWidth(1); canvas.drawCircle(x, y, r, paint); canvas.drawLine(x,y-matrix_height/2,x,y-r,paint); canvas.drawLine(x,y+r,x,y+matrix_height/2,paint); canvas.drawLine(x-matrix_width/2,y,x-r,y,paint); canvas.drawLine(x+r,y,x+matrix_width/2,y,paint); // hue bar paint.setStyle(Paint.Style.FILL); paint.setColor(Color.WHITE); canvas.drawRect(0, 0, matrix_width, huebar_height, paint); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(1); for (x=0; x matrix_width) d -= matrix_width; float hue = (360.0f*d)/matrix_width; paint.setColor(hsv_to_color(hue,1,1)); canvas.drawLine(x, 0, x, huebar_height, paint); } paint.setColor(Color.BLACK); paint.setStrokeWidth((1.5f * dpi)/160.0f); x = matrix_width/2 + 1; canvas.drawLine(x,0,x,huebar_height,paint); paint.setStrokeWidth(1); canvas.drawRect(0, 0, matrix_width, huebar_height, paint); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //setMeasuredDimension(276, 366); setMeasuredDimension(width,height); } @Override public boolean onTouchEvent(MotionEvent event) { int action = event.getAction(); float x = event.getX(); float y = event.getY(); if (hue_down || y < huebar_height) { if (action == MotionEvent.ACTION_DOWN) { hue_down = true; hue_xoff -= x; hue_xcur = x; } if (action == MotionEvent.ACTION_UP) { hue_down = false; hue_xoff += x; hue_xcur = 0; } if (action == MotionEvent.ACTION_MOVE) { hue_xcur = x; int d = (int)(matrix_width/2 - (hue_xoff + x)); while (d < 0) d += matrix_width; while (d > matrix_width) d -= matrix_width; current_hue = (360.0f*d)/matrix_width; //xoff = xcur = 0; //yoff = ycur = 0; updateCurrentColor(); } return true; } if (action == MotionEvent.ACTION_DOWN) { xcur = x; ycur = y; xoff -= x; yoff -= y; invalidate(); return true; } if (action == MotionEvent.ACTION_UP) { xcur = 0; ycur = 0; xoff += x; yoff += y; updateCurrentColor(); return true; } if (action == MotionEvent.ACTION_MOVE) { xcur = x; ycur = y; if (xoff + xcur > matrix_width/2) xoff = matrix_width/2 - xcur; if (xoff + xcur < -matrix_width/2) xoff = - matrix_width/2 - xcur; if (yoff + ycur > matrix_height/2) yoff = matrix_height/2 - ycur; if (yoff + ycur < -matrix_height/2) yoff = -matrix_height/2 - ycur; updateCurrentColor(); } return true; } } private int DipToPixels(float dp) { float dpi = context.getResources().getDisplayMetrics().densityDpi; return (int)(0.5 + dp * (dpi/160f)); } public ColorPickerDialog(Context ct, OnColorResultListener listener, String t, int clr, int w, int h) { //super(ct); super(new ContextThemeWrapper(ct,styles[def_style])); //context = ct; context = new ContextThemeWrapper(ct,styles[def_style]); title = t; initialColor = clr; predefinedColors = new ArrayList(); cc_listener = listener; width = w; height = h; button_h = DipToPixels(60); view_h = height - button_h; } public void addPredefinedColor(int clr) { predefinedColors.add(clr); } private Button highlightButton(int clr) { Button b = null; for(int i=0; i 0) view_h -= button_h; colorPickerView = new ColorPickerView(context,initialColor,width+10,view_h, button_h) { @Override public void onColorChanged(int clr) { int r = Color.red(clr); int g = Color.green(clr); int b = Color.blue(clr); /* String txt = String.format("%s %.0f / %02X-%02X-%02X", title,hue,r,g,b); */ String txt = String.format("%s 0x%02x%02x%02x",title,r,g,b); int[] clrs = new int[2]; clrs[0] = 0xff303030; clrs[1] = clr; GradientDrawable gd1 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, clrs.clone()); gd1.setStroke(1,0xffc0c0c0); clrs[0] = clr; clrs[1] = clr; GradientDrawable gd2 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, clrs.clone()); gd2.setStroke(1,0xffc0c0c0); clrs[0] = clr; clrs[1] = 0xffa0a0a0; GradientDrawable gd3 = new GradientDrawable(GradientDrawable.Orientation.TOP_BOTTOM, clrs.clone()); gd3.setStroke(1,0xffc0c0c0); int txt_clr = ColorPickerView.text_color(clr); /* titleView.setBackground(gd1); titleView.setTextColor(txt_clr); titleView.setText(txt); */ if (but1 != null) { //but1.setBackground(gd1); but1.setBackground(gd2); but1.setTextColor(txt_clr); } if (but2 != null) { but2.setBackground(gd2); but2.setTextColor(txt_clr); but2.setText(String.format("0x%02x%02x%02x",r,g,b)); /* String s = String.format("OK\n0x%02x%02x%02x",r,g,b); Spannable span = new SpannableString(s); float f = 0.7f; span.setSpan(new RelativeSizeSpan(f),2,s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); but2.setText(span); */ } if (but3 != null) { but3.setBackground(gd3); but3.setTextColor(txt_clr); } } }; View.OnTouchListener on_touch = new View.OnTouchListener() { public boolean onTouch(final View v, MotionEvent event) { if (event.getAction() == MotionEvent.ACTION_DOWN) { v.setBackgroundColor(0xff707070); ((Button)v).setTextColor(0xffffffff); new Handler().postDelayed(new Runnable() { public void run() { Button but = (Button)v; int tag = (Integer)v.getTag(); if (tag == -1) { dismiss(); return; } int clr = colorPickerView.getCurrentColor(); if (tag < 0) { int clr1 = 0; int clr2 = 0; but.setBackgroundColor(0xffa0a0a0); if (tag == -1) { clr1 = 0; clr2 = clr; } if (tag == -2) { clr1 = clr; clr2 = clr; } if (tag == -3) { clr1 = clr; clr2 = 0; } if (cc_listener != null) cc_listener.onColorResult(clr1,clr2); dismiss(); } } },500); } return false; } }; but1.setOnTouchListener(on_touch); but2.setOnTouchListener(on_touch); but3.setOnTouchListener(on_touch); final HorizontalScrollView sc = new HorizontalScrollView(context); LinearLayout buttons_line = new LinearLayout(context); buttons_line.setBackgroundColor(0xff777777); int sz = predefinedColors.size(); preButtons = new Button[sz]; for(int i=0; i 0) layout.addView(sc,params3); layout.addView(buttons); setContentView(layout); final Button b = highlightButton(initialColor); if (b == null) return; sc.post( new Runnable() { public void run() { int x = (int)(b.getLeft() - 0.4f*(width+10)); if (x < 0) x = 0; sc.smoothScrollTo(x,0); } }); } }