Example #1
0
  /**
   * 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]);
      }
    }

    if (mSaturationBar != null) {
      Color.colorToHSV(color, mHSV);
      mSaturationBar.setColor(mColor);
      mSaturationBar.setSaturation(mHSV[1]);
    }

    if (mValueBar != null && mSaturationBar == null) {
      Color.colorToHSV(color, mHSV);
      mValueBar.setColor(mColor);
      mValueBar.setValue(mHSV[2]);
    } else if (mValueBar != null) {
      Color.colorToHSV(color, mHSV);
      mValueBar.setValue(mHSV[2]);
    }

    invalidate();
  }
Example #2
0
 /**
  * Used to change the color of the {@code SaturationBar}.
  *
  * @param color int of the color used to change the opacity bar color.
  */
 public void changeSaturationBarColor(int color) {
   if (mSaturationBar != null) {
     mSaturationBar.setColor(color);
   }
 }
Example #3
0
 public void addSaturationBar(SaturationBar bar) {
   mSaturationBar = bar;
   mSaturationBar.setColorPicker(this);
   mSaturationBar.setColor(mColor);
 }
Example #4
0
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    getParent().requestDisallowInterceptTouchEvent(true);

    // 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)) {
          mSlopX = x - pointerPosition[0];
          mSlopY = y - pointerPosition[1];
          mUserIsMovingPointer = true;
          invalidate();
        }
        // Check whether the user pressed on the center.
        else if (x >= -mColorCenterRadius
            && x <= mColorCenterRadius
            && y >= -mColorCenterRadius
            && y <= mColorCenterRadius
            && mShowCenterOldColor) {
          mCenterHaloPaint.setAlpha(0x50);
          setColor(getOldCenterColor());
          invalidate();
        }
        // Check whether the user pressed anywhere on the wheel.
        else if (Math.sqrt(x * x + y * y) <= mColorWheelRadius + mColorPointerHaloRadius
            && Math.sqrt(x * x + y * y) >= mColorWheelRadius - mColorPointerHaloRadius
            && mTouchAnywhereOnColorWheelEnabled) {
          mUserIsMovingPointer = true;
          invalidate();
        }
        // If user did not press pointer or center, report event not handled
        else {
          getParent().requestDisallowInterceptTouchEvent(false);
          return false;
        }
        break;
      case MotionEvent.ACTION_MOVE:
        if (mUserIsMovingPointer) {
          mAngle = (float) Math.atan2(y - mSlopY, x - mSlopX);
          mPointerColor.setColor(calculateColor(mAngle));

          setNewCenterColor(mCenterNewColor = calculateColor(mAngle));

          if (mOpacityBar != null) {
            mOpacityBar.setColor(mColor);
          }

          if (mValueBar != null) {
            mValueBar.setColor(mColor);
          }

          if (mSaturationBar != null) {
            mSaturationBar.setColor(mColor);
          }

          if (mSVbar != null) {
            mSVbar.setColor(mColor);
          }

          invalidate();
        }
        // If user did not press pointer or center, report event not handled
        else {
          getParent().requestDisallowInterceptTouchEvent(false);
          return false;
        }
        break;
      case MotionEvent.ACTION_UP:
        mUserIsMovingPointer = false;
        mCenterHaloPaint.setAlpha(0x00);

        if (onColorSelectedListener != null && mCenterNewColor != oldSelectedListenerColor) {
          onColorSelectedListener.onColorSelected(mCenterNewColor);
          oldSelectedListenerColor = mCenterNewColor;
        }

        invalidate();
        break;
      case MotionEvent.ACTION_CANCEL:
        if (onColorSelectedListener != null && mCenterNewColor != oldSelectedListenerColor) {
          onColorSelectedListener.onColorSelected(mCenterNewColor);
          oldSelectedListenerColor = mCenterNewColor;
        }
        break;
    }
    return true;
  }
Example #5
0
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    getParent().requestDisallowInterceptTouchEvent(true);

    // 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 whether the user pressed on the center.
        else if (x >= -mColorCenterRadius
            && x <= mColorCenterRadius
            && y >= -mColorCenterRadius
            && y <= mColorCenterRadius) {
          mCenterHaloPaint.setAlpha(0x50);
          setColor(getOldCenterColor());
          mCenterNewPaint.setColor(getOldCenterColor());
          invalidate();
        }
        // If user did not press pointer or center, report event not handled
        else {
          getParent().requestDisallowInterceptTouchEvent(false);
          return false;
        }
        break;
      case MotionEvent.ACTION_MOVE:
        if (mUserIsMovingPointer) {
          mAngle = (float) java.lang.Math.atan2(y, x);
          mPointerColor.setColor(calculateColor(mAngle));

          setNewCenterColor(mCenterNewColor = calculateColor(mAngle));

          if (mOpacityBar != null) {
            mOpacityBar.setColor(mColor);
          }

          if (mValueBar != null) {
            mValueBar.setColor(mColor);
          }

          if (mSaturationBar != null) {
            mSaturationBar.setColor(mColor);
          }

          if (mSVbar != null) {
            mSVbar.setColor(mColor);
          }

          invalidate();
        }
        // If user did not press pointer or center, report event not handled
        else {
          getParent().requestDisallowInterceptTouchEvent(false);
          return false;
        }
        break;
      case MotionEvent.ACTION_UP:
        mUserIsMovingPointer = false;
        mCenterHaloPaint.setAlpha(0x00);
        invalidate();
        break;
    }
    return true;
  }