コード例 #1
0
ファイル: ColorPicker.java プロジェクト: hsujm/android
  /**
   * Set the color to be highlighted by the pointer. </br> </br> If the instances {@code SVBar} and
   * the {@code OpacityBar} aren't null the color will also be set to them
   *
   * @param color The RGB value of the color to highlight. If this is not a color displayed on the
   *     color wheel a very simple algorithm is used to map it to the color wheel. The resulting
   *     color often won't look close to the original color. This is especially true for shades of
   *     grey. You have been warned!
   */
  public void setColor(int color) {
    mAngle = colorToAngle(color);
    mPointerColor.setColor(calculateColor(mAngle));

    // check of the instance isn't null
    if (mOpacityBar != null) {
      // set the value of the opacity
      mOpacityBar.setColor(mColor);
      mOpacityBar.setOpacity(Color.alpha(color));
    }

    // check if the instance isn't null
    if (mSVbar != null) {
      // the array mHSV will be filled with the HSV values of the color.
      Color.colorToHSV(color, mHSV);
      mSVbar.setColor(mColor);

      // because of the design of the Saturation/Value bar,
      // we can only use Saturation or Value every time.
      // Here will be checked which we shall use.
      if (mHSV[1] < mHSV[2]) {
        mSVbar.setSaturation(mHSV[1]);
      } else { // if (mHSV[1] > mHSV[2]) {
        mSVbar.setValue(mHSV[2]);
      }
    }

    invalidate();
  }
コード例 #2
0
ファイル: ColorPicker.java プロジェクト: hsujm/android
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // Convert coordinates to our internal coordinate system
    float x = event.getX() - mTranslationOffset;
    float y = event.getY() - mTranslationOffset;

    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
        // Check whether the user pressed on the pointer.
        float[] pointerPosition = calculatePointerPosition(mAngle);
        if (x >= (pointerPosition[0] - mColorPointerHaloRadius)
            && x <= (pointerPosition[0] + mColorPointerHaloRadius)
            && y >= (pointerPosition[1] - mColorPointerHaloRadius)
            && y <= (pointerPosition[1] + mColorPointerHaloRadius)) {
          mUserIsMovingPointer = true;
          invalidate();
        }
        // Check wheter the user pressed on the center.
        if (x >= -mColorCenterRadius
            && x <= mColorCenterRadius
            && y >= -mColorCenterRadius
            && y <= mColorCenterRadius) {
          mCenterHaloPaint.setAlpha(0x50);
          setColor(getOldCenterColor());
          mCenterNewPaint.setColor(getOldCenterColor());
          invalidate();
        }
        break;
      case MotionEvent.ACTION_MOVE:
        if (mUserIsMovingPointer) {
          mAngle = (float) java.lang.Math.atan2(y, x);
          mPointerColor.setColor(calculateColor(mAngle));

          // Check wheter there is a Saturation/Value bar or/and
          // OpacityBar connected.
          if (mSVbar != null && mOpacityBar == null) {
            mSVbar.setColor(mColor);
          } else if (mSVbar == null && mOpacityBar != null) {
            mOpacityBar.setColor(mColor);
          } else if (mSVbar != null && mOpacityBar != null) {
            mOpacityBar.setColor(mColor);
            mSVbar.setColor(mColor);
          } else {
            setNewCenterColor(mCenterNewColor = calculateColor(mAngle));
          }
          invalidate();
        }
        break;
      case MotionEvent.ACTION_UP:
        mUserIsMovingPointer = false;
        mCenterHaloPaint.setAlpha(0x00);
        invalidate();
        break;
    }
    getParent().requestDisallowInterceptTouchEvent(true);
    return true;
  }
コード例 #3
0
ファイル: ColorPicker.java プロジェクト: hsujm/android
 /**
  * Add a Saturation/Value bar to the color wheel.
  *
  * @param bar The instance of the Saturation/Value bar.
  */
 public void addSVBar(SVBar bar) {
   mSVbar = bar;
   // Give an instance of the color picker to the Saturation/Value bar.
   mSVbar.setColorPicker(this);
   mSVbar.setColor(mColor);
 }