16125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford/* 26125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * Copyright (C) 2013 The Android Open Source Project 36125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * 46125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * Licensed under the Apache License, Version 2.0 (the "License"); 56125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * you may not use this file except in compliance with the License. 66125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * You may obtain a copy of the License at 76125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * 86125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * http://www.apache.org/licenses/LICENSE-2.0 96125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * 106125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * Unless required by applicable law or agreed to in writing, software 116125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * distributed under the License is distributed on an "AS IS" BASIS, 126125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * See the License for the specific language governing permissions and 146125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford * limitations under the License. 156125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford */ 166125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 176125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordpackage com.android.gallery3d.filtershow.colorpicker; 186125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 196125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.content.Context; 206125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.content.res.Resources; 216125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.graphics.Bitmap; 226125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.graphics.BitmapShader; 236125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.graphics.Canvas; 246125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.graphics.Color; 256125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.graphics.Paint; 266125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.graphics.Path; 276125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.graphics.Shader; 286125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.util.AttributeSet; 296125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.util.DisplayMetrics; 306125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.view.MotionEvent; 316125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport android.view.View; 326125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 336125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport com.android.gallery3d.R; 346125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 356125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordimport java.util.ArrayList; 366125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 376125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hofordpublic class ColorCompareView extends View implements ColorListener { 386125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 396125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private float mRadius; 406125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private float mWidth; 416125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private Paint mBarPaint1; 426125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private Paint mOrigBarPaint1; 436125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private Paint mCheckPaint; 446125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 456125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private float mHeight; 466125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 476125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private int mBgcolor = 0; 486125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 496125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private float mBorder; 506125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 516125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private float[] mHSVO = new float[4]; 526125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private float[] mOrigHSVO = new float[4]; 536125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private Path mRegion; 546125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private Path mOrigRegion; 556125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 566125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public final static float BORDER_SIZE = 0; 576125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private int mCheckDim = 8; 586125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 596125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public ColorCompareView(Context ctx, AttributeSet attrs) { 606125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford super(ctx, attrs); 616125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford DisplayMetrics metrics = ctx.getResources().getDisplayMetrics(); 626125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford float mDpToPix = metrics.density; 636125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mBorder = BORDER_SIZE * mDpToPix; 646125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mBarPaint1 = new Paint(); 656125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mOrigBarPaint1 = new Paint(); 666125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford Resources res = ctx.getResources(); 676125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mCheckDim = res.getDimensionPixelSize(R.dimen.draw_color_check_dim); 686125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mBarPaint1.setStyle(Paint.Style.FILL); 696125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mOrigBarPaint1.setStyle(Paint.Style.FILL); 706125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 716125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford makeCheckPaint(); 726125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 736125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 746125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private void makeCheckPaint() { 756125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford int imgdim = mCheckDim * 2; 766125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford int[] colors = new int[imgdim * imgdim]; 776125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford for (int i = 0; i < colors.length; i++) { 786125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford int y = i / (imgdim * mCheckDim); 796125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford int x = (i / mCheckDim) % 2; 806125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford colors[i] = (x == y) ? 0xFFAAAAAA : 0xFF444444; 816125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 826125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford Bitmap bitmap = Bitmap.createBitmap(colors, imgdim, imgdim, Bitmap.Config.ARGB_8888); 836125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford BitmapShader bs = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT); 846125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mCheckPaint = new Paint(); 856125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mCheckPaint.setShader(bs); 866125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 876125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 886125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public boolean onDown(MotionEvent e) { 896125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford return true; 906125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 916125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 926125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford @Override 936125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public boolean onTouchEvent(MotionEvent event) { 946125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford if (event.getAction() != MotionEvent.ACTION_UP) { 956125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford return true; 966125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 976125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford float x = event.getX(); 986125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford float y = event.getY(); 996125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford if (x> mWidth-2*mHeight) { 1006125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford resetToOriginal(); 1016125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1026125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford return true; 1036125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1046125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1056125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public void resetToOriginal(){ 1066125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford System.arraycopy(mOrigHSVO, 0, mHSVO, 0, mOrigHSVO.length); 1076125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford updatePaint(); 1086125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford notifyColorListeners(mHSVO); 1096125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford invalidate(); 1106125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1116125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1126125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford @Override 1136125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford protected void onSizeChanged(int w, int h, int oldw, int oldh) { 1146125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mWidth = w; 1156125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mHeight = h; 1166125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford updatePaint(); 1176125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1186125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1196125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford private void updatePaint() { 1206125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford int color = Color.HSVToColor((int) (mHSVO[3] * 255), mHSVO); 1216125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mBarPaint1.setColor(color); 1226125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford int origColor = Color.HSVToColor((int) (mOrigHSVO[3] * 255), mOrigHSVO); 1236125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mOrigBarPaint1.setColor(origColor); 1246125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mOrigRegion = new Path(); 1256125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mOrigRegion.moveTo(mWidth, 0); 1266125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mOrigRegion.lineTo(mWidth, mHeight); 1276125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mOrigRegion.lineTo(mWidth - mHeight * 2, mHeight); 1286125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mOrigRegion.lineTo(mWidth - mHeight, 0); 1296125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1306125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mRegion = new Path(); 1316125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mRegion.moveTo(0, 0); 1326125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mRegion.lineTo(mWidth - mHeight, 0); 1336125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mRegion.lineTo(mWidth - mHeight * 2, mHeight); 1346125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mRegion.lineTo(0, mHeight); 1356125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1366125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1376125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1386125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford @Override 1396125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford protected void onDraw(Canvas canvas) { 1406125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford super.onDraw(canvas); 1416125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford canvas.drawColor(mBgcolor); 1426125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford canvas.drawRect(mBorder, 0, mWidth, mHeight, mCheckPaint); 1436125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford canvas.drawPath(mRegion, mBarPaint1); 1446125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford canvas.drawPath(mOrigRegion, mOrigBarPaint1); 1456125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1466125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1476125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public void setOrigColor(float[] hsv) { 1486125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford System.arraycopy(hsv, 0, mOrigHSVO, 0, mOrigHSVO.length); 1496125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford int color2 = Color.HSVToColor((int) (mOrigHSVO[3] * 255), mOrigHSVO); 1506125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mOrigBarPaint1.setColor(color2); 1516125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford updatePaint(); 1526125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1536125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1546125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford @Override 1556125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public void setColor(float[] hsv) { 1566125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford System.arraycopy(hsv, 0, mHSVO, 0, mHSVO.length); 1576125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford updatePaint(); 1586125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford invalidate(); 1596125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1606125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1616125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford ArrayList<ColorListener> mColorListeners = new ArrayList<ColorListener>(); 1626125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1636125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public void notifyColorListeners(float[] hsvo) { 1646125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford for (ColorListener l : mColorListeners) { 1656125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford l.setColor(hsvo); 1666125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1676125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1686125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1696125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public void addColorListener(ColorListener l) { 1706125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mColorListeners.add(l); 1716125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1726125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 1736125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford public void removeColorListener(ColorListener l) { 1746125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford mColorListeners.remove(l); 1756125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford } 1766125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford} 1776125a082a2b5ddc0d34a5198c5a7e826bd77e202John Hoford 178