Code example for RectF

Methods: contains, height, width

0
	} 
 
	private float[] pointToSatVal(float x, float y){
 
		final RectF rect = mSatValRect;
		float[] result = new float[2];
 
		float width = rect.width();
		float height = rect.height();
 
		if (x < rect.left){
			x = 0f;
		} 
		else if(x > rect.right){
			x = width;
		} 
		else{ 
			x = x - rect.left;
		} 
 
		if (y < rect.top){
			y = 0f;
		} 
		else if(y > rect.bottom){
			y = height;
		} 
		else{ 
			y = y - rect.top;
		} 
 
 
		result[0] = 1.f / width * x;
		result[1] = 1.f - (1.f / height * y);
 
		return result;
	} 
 
	private float pointToHue(float y){
 
		final RectF rect = mHueRect;
 
		float height = rect.height();
 
		if (y < rect.top){
			y = 0f;
		} 
		else if(y > rect.bottom){
			y = height;
		} 
		else{ 
			y = y - rect.top;
		} 
 
		return 360f - (y * 360f / height);
	} 
 
	private int pointToAlpha(int x){
 
		final RectF rect = mAlphaRect;
		final int width = (int) rect.width();
 
		if(x < rect.left){
			x = 0;
		} 
		else if(x > rect.right){
			x = width;
		} 
		else{ 
			x = x - (int)rect.left;
		} 
 
		return 0xff - (x * 0xff / width);
 
	} 
 
 
	@Override 
	public boolean onTrackballEvent(MotionEvent event) {
 
		float x = event.getX();
		float y = event.getY();
 
		boolean update = false;
 
 
		if(event.getAction() == MotionEvent.ACTION_MOVE){
 
			switch(mLastTouchedPanel){
 
			case PANEL_SAT_VAL:
 
				float sat, val;
 
				sat = mSat + x/50f;
				val = mVal - y/50f;
 
				if(sat < 0f){
					sat = 0f;
				} 
				else if(sat > 1f){
					sat = 1f;
				} 
 
				if(val < 0f){
					val = 0f;
				} 
				else if(val > 1f){
					val = 1f;
				} 
 
				mSat = sat;
				mVal = val;
 
				update = true;
 
				break; 
 
			case PANEL_HUE:
 
				float hue = mHue - y * 10f;
 
				if(hue < 0f){
					hue = 0f;
				} 
				else if(hue > 360f){
					hue = 360f;
				} 
 
				mHue = hue;
 
				update = true;
 
				break; 
 
			case PANEL_ALPHA:
 
				if(!mShowAlphaPanel || mAlphaRect == null){
					update = false;
				} 
				else{ 
 
					int alpha = (int) (mAlpha - x*10);
 
					if(alpha < 0){
						alpha = 0;
					} 
					else if(alpha > 0xff){
						alpha = 0xff;
					} 
 
					mAlpha = alpha;
 
 
					update = true;
				} 
 
				break; 
			} 
 
 
		} 
 
 
		if(update){
 
			if(mListener != null){
				mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
			} 
 
			invalidate();
			return true; 
		} 
 
 
		return super.onTrackballEvent(event);
	} 
 
	@Override 
	public boolean onTouchEvent(MotionEvent event) {
 
		boolean update = false;
 
		switch(event.getAction()){
 
		case MotionEvent.ACTION_DOWN:
 
			mStartTouchPoint = new Point((int)event.getX(), (int)event.getY());
 
			update = moveTrackersIfNeeded(event);
 
			break; 
 
		case MotionEvent.ACTION_MOVE:
 
			update = moveTrackersIfNeeded(event);
 
			break; 
 
		case MotionEvent.ACTION_UP:
 
			mStartTouchPoint = null;
 
			update = moveTrackersIfNeeded(event);
 
			break; 
 
		} 
 
		if(update){
 
			if(mListener != null){
				mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[]{mHue, mSat, mVal}));
			} 
 
			invalidate();
			return true; 
		} 
 
 
		return super.onTouchEvent(event);
	} 
 
	private boolean moveTrackersIfNeeded(MotionEvent event){
 
		if(mStartTouchPoint == null) return false;
 
		boolean update = false;
 
		int startX = mStartTouchPoint.x;
		int startY = mStartTouchPoint.y;
 
 
		if(mHueRect.contains(startX, startY)){
			mLastTouchedPanel = PANEL_HUE;
 
			mHue = pointToHue(event.getY());
 
			update = true;
		} 
		else if(mSatValRect.contains(startX, startY)){
 
			mLastTouchedPanel = PANEL_SAT_VAL;