aboutsummaryrefslogtreecommitdiff
path: root/demokit/app/src/com/google/android/DemoKit/ColorWheel.java
blob: a1292cc5b553d5e64ff0c53eb64ab8f88cab7906 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.DemoKit;

import android.content.Context;
import android.graphics.*;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;


/* This class was masterfully pilfered, Carmen-Sandiego style, from our awesome sample app
 * ApiDemos.  To see how it looks as a dialog, check out:
 * http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/graphics/ColorPickerDialog.html
 *
 */

  public class ColorWheel extends View {
      public interface OnColorChangedListener {
          void colorChanged(int color);
      }

      /*
      */

      private OnColorChangedListener mListener;

      private int mInitialColor;

      private Paint mPaint;
      private Paint mCenterPaint;
      private final int[] mColors;

      public void setOnColorChangedListener(OnColorChangedListener l) {
          mListener = l;
      }


      public ColorWheel(Context c, AttributeSet attributes) {

          super(c, attributes);

          mColors = new int[] {
              0xFFFF0000, 0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
              0xFFFFFF00, 0xFFFF0000
          };
          Shader s = new SweepGradient(0, 0, mColors, null);

          mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
          mPaint.setShader(s);
          mPaint.setStyle(Paint.Style.STROKE);
          mPaint.setStrokeWidth(32);

          mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
          mCenterPaint.setColor(0);
          mCenterPaint.setStrokeWidth(5);
      }



      private boolean mTrackingCenter;
      private boolean mHighlightCenter;

      @Override
      protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);
          float r = CENTER_X - mPaint.getStrokeWidth()*0.5f;

          canvas.translate(CENTER_X, CENTER_X);

          canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
          canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);

          if (mTrackingCenter) {
              int c = mCenterPaint.getColor();
              mCenterPaint.setStyle(Paint.Style.STROKE);

              if (mHighlightCenter) {
                  mCenterPaint.setAlpha(0xFF);
              } else {
                  mCenterPaint.setAlpha(0x80);
              }
              canvas.drawCircle(0, 0,
                                CENTER_RADIUS + mCenterPaint.getStrokeWidth(),
                                mCenterPaint);

              mCenterPaint.setStyle(Paint.Style.FILL);
              mCenterPaint.setColor(c);
          }
      }

      @Override
      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

          setMeasuredDimension(CENTER_X*2, CENTER_Y*2);
      }

      private static final int CENTER_X = 200;
      private static final int CENTER_Y = 200;
      private static final int CENTER_RADIUS = 64;

      private int floatToByte(float x) {
          int n = java.lang.Math.round(x);
          return n;
      }
      private int pinToByte(int n) {
          if (n < 0) {
              n = 0;
          } else if (n > 255) {
              n = 255;
          }
          return n;
      }

      private int ave(int s, int d, float p) {
          return s + java.lang.Math.round(p * (d - s));
      }

      private int interpColor(int colors[], float unit) {
          if (unit <= 0) {
              return colors[0];
          }
          if (unit >= 1) {
              return colors[colors.length - 1];
          }

          float p = unit * (colors.length - 1);
          int i = (int)p;
          p -= i;

          // now p is just the fractional part [0...1) and i is the index
          int c0 = colors[i];
          int c1 = colors[i+1];
          int a = ave(Color.alpha(c0), Color.alpha(c1), p);
          int r = ave(Color.red(c0), Color.red(c1), p);
          int g = ave(Color.green(c0), Color.green(c1), p);
          int b = ave(Color.blue(c0), Color.blue(c1), p);

          return Color.argb(a, r, g, b);
      }

      private int rotateColor(int color, float rad) {
          float deg = rad * 180 / 3.1415927f;
          int r = Color.red(color);
          int g = Color.green(color);
          int b = Color.blue(color);

          ColorMatrix cm = new ColorMatrix();
          ColorMatrix tmp = new ColorMatrix();

          cm.setRGB2YUV();
          tmp.setRotate(0, deg);
          cm.postConcat(tmp);
          tmp.setYUV2RGB();
          cm.postConcat(tmp);

          final float[] a = cm.getArray();

          int ir = floatToByte(a[0] * r +  a[1] * g +  a[2] * b);
          int ig = floatToByte(a[5] * r +  a[6] * g +  a[7] * b);
          int ib = floatToByte(a[10] * r + a[11] * g + a[12] * b);

          return Color.argb(Color.alpha(color), pinToByte(ir),
                            pinToByte(ig), pinToByte(ib));
      }

      private static final float PI = 3.1415926f;

      @Override
      public boolean onTouchEvent(MotionEvent event) {
          float x = event.getX() - CENTER_X;
          float y = event.getY() - CENTER_Y;
          boolean inCenter = java.lang.Math.sqrt(x*x + y*y) <= CENTER_RADIUS;

          switch (event.getAction()) {
              case MotionEvent.ACTION_DOWN:
                  mTrackingCenter = inCenter;
                  if (inCenter) {
                      mHighlightCenter = true;
                      invalidate();
                      break;
                  }
              case MotionEvent.ACTION_MOVE:
                  if (mTrackingCenter) {
                      if (mHighlightCenter != inCenter) {
                          mHighlightCenter = inCenter;
                          invalidate();
                      }
                  } else {
                      float angle = (float)java.lang.Math.atan2(y, x);
                      // need to turn angle [-PI ... PI] into unit [0....1]
                      float unit = angle/(2*PI);
                      if (unit < 0) {
                          unit += 1;
                      }
                      mCenterPaint.setColor(interpColor(mColors, unit));
                      invalidate();
                      mListener.colorChanged(mCenterPaint.getColor());
                  }
                  break;
              case MotionEvent.ACTION_UP:
                  if (mTrackingCenter) {
                      if (inCenter) {
                          mListener.colorChanged(mCenterPaint.getColor());
                      }
                      mListener.colorChanged(0);
                      // mListener.colorChanged(mCenterPaint.getColor());
                      mTrackingCenter = false;    // so we draw w/o halo
                      invalidate();
                  }
                  break;
          }
          return true;
      }
  }