@Override
  protected void onDraw(Canvas canvas) {
    // draw the background and color wheel
    canvas.drawBitmap(mBackground, mBackgroundPosition.x, mBackgroundPosition.y, null);
    canvas.drawBitmap(mWheel, mWheelPosition.x, mWheelPosition.y, null);

    // setup paint for the gradient filling
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setStyle(Paint.Style.FILL);
    paint.setStrokeWidth(2.0f);
    // create a shader that will show the range of brightness settings
    float[] gradientStartColor = new float[3];
    float[] gradientEndColor = new float[3];
    System.arraycopy(mNewColor, 0, gradientStartColor, 0, mNewColor.length);
    System.arraycopy(mNewColor, 0, gradientEndColor, 0, mNewColor.length);
    gradientStartColor[2] = 1.0f;
    gradientEndColor[2] = 0.0f;
    Shader gradientShader =
        new LinearGradient(
            (float) (mWheelPosition.x + mRadius),
            mWheelPosition.y,
            mOuterArcRect.right,
            mWheelPosition.y + mWheel.getHeight(),
            Color.HSVToColor(gradientStartColor),
            Color.HSVToColor(gradientEndColor),
            Shader.TileMode.MIRROR);
    paint.setShader(gradientShader);
    canvas.drawPath(mArcPath, paint);

    drawHSCrosshairs(canvas);
    drawBrightnessIndicator(canvas);
  }
Exemple #2
0
  public void refresh() {
    int alpha = Color.alpha(mButtonColor);
    float[] hsv = new float[3];
    Color.colorToHSV(mButtonColor, hsv);
    hsv[2] *= 0.8f; // value component
    // if shadow color was not defined, generate shadow color = 80% brightness
    if (!isShadowColorDefined) {
      mShadowColor = Color.HSVToColor(alpha, hsv);
    }
    // Create pressed background and unpressed background drawables

    if (this.isEnabled()) {
      if (isShadowEnabled) {
        pressedDrawable = createDrawable(mCornerRadius, Color.TRANSPARENT, mButtonColor);
        unpressedDrawable = createDrawable(mCornerRadius, mButtonColor, mShadowColor);
      } else {
        mShadowHeight = 0;
        pressedDrawable = createDrawable(mCornerRadius, mShadowColor, Color.TRANSPARENT);
        unpressedDrawable = createDrawable(mCornerRadius, mButtonColor, Color.TRANSPARENT);
      }
    } else {
      Color.colorToHSV(mButtonColor, hsv);
      hsv[1] *= 0.25f; // saturation component
      int disabledColor = mShadowColor = Color.HSVToColor(alpha, hsv);
      // Disabled button does not have shadow
      pressedDrawable = createDrawable(mCornerRadius, disabledColor, Color.TRANSPARENT);
      unpressedDrawable = createDrawable(mCornerRadius, disabledColor, Color.TRANSPARENT);
    }
    updateBackground(unpressedDrawable);
    // Set padding
    this.setPadding(
        mPaddingLeft, mPaddingTop + mShadowHeight, mPaddingRight, mPaddingBottom + mShadowHeight);
  }
Exemple #3
0
  /**
   * Calculate the color selected by the pointer on the bar.
   *
   * @param coord Coordinate of the pointer.
   */
  private void calculateColor(int coord) {
    coord = coord - mBarPointerHaloRadius;
    if (coord < 0) {
      coord = 0;
    } else if (coord > mBarLength) {
      coord = mBarLength;
    }

    if (coord > (mBarPointerHaloRadius + (mBarLength / 2))
        && coord < (mBarPointerHaloRadius + mBarLength)) {
      mColor =
          Color.HSVToColor(
              new float[] {
                mHSVColor[0],
                1f,
                (float)
                    (1 - (mPosToSVFactor * (coord - (mBarPointerHaloRadius + (mBarLength / 2)))))
              });
    } else if (coord > mBarPointerHaloRadius && coord < (mBarPointerHaloRadius + mBarLength)) {
      mColor =
          Color.HSVToColor(
              new float[] {
                mHSVColor[0], (float) ((mPosToSVFactor * (coord - mBarPointerHaloRadius))), 1f
              });
    } else if (coord == mBarPointerHaloRadius) {
      mColor = Color.WHITE;
    } else if (coord == mBarPointerHaloRadius + mBarLength) {
      mColor = Color.BLACK;
    }
  }
  @Override
  public View getView(int position, View convertView, ViewGroup parent) {
    int itemType = this.getItemViewType(position);
    switch (itemType) {
      case ITEM_TYPE:
        // Get the data item for this position
        WakelockStats wakelock = (WakelockStats) getItem(position);

        // Check if an existing view is being reused, otherwise inflate the view
        WakelockViewHolder viewHolder; // view lookup cache stored in tag
        if (convertView == null) {
          viewHolder = new WakelockViewHolder();

          LayoutInflater inflater = LayoutInflater.from(getContext());
          convertView = inflater.inflate(R.layout.fragment_wakelocks_listitem, parent, false);
          viewHolder.name = (TextView) convertView.findViewById(R.id.textviewWakelockName);
          if (mTruncateEnd) {
            viewHolder.name.setEllipsize(TextUtils.TruncateAt.END);
          }
          viewHolder.wakeTime = (TextView) convertView.findViewById(R.id.textviewWakelockTime);
          viewHolder.wakeCount = (TextView) convertView.findViewById(R.id.textViewWakelockCount);

          convertView.setTag(viewHolder);
        } else {
          viewHolder = (WakelockViewHolder) convertView.getTag();
        }

        // Populate the data into the template view using the data object
        viewHolder.name.setText(wakelock.getName());
        viewHolder.name.setSelected(true);

        viewHolder.wakeTime.setText(String.valueOf(wakelock.getDurationAllowedFormatted()));
        viewHolder.wakeCount.setText(String.valueOf(wakelock.getAllowedCount()));

        // Size the count box width to at least the height.
        viewHolder.wakeCount.measure(
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
            View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        int height = viewHolder.wakeCount.getMeasuredHeight();
        int width = viewHolder.wakeCount.getMeasuredWidth();
        if (height > width) {
          viewHolder.wakeCount.setLayoutParams(new LinearLayout.LayoutParams(height, height));
        } else {
          viewHolder.wakeCount.setLayoutParams(new LinearLayout.LayoutParams(width, width));
        }
        float[] hsv = getBackColorFromSpectrum(wakelock);
        viewHolder.wakeCount.setBackgroundColor(Color.HSVToColor(hsv));

        hsv = getForeColorFromBack(hsv);
        viewHolder.wakeCount.setTextColor(Color.HSVToColor(hsv));
        break;

      case CATEGORY_TYPE:
        convertView = getCategoryView(position, convertView, parent);
        break;
    }

    return convertView;
  }
  public AvatarDrawable(char letter) {
    mLetter = letter;
    int hue = generateColorFromLetter(letter);

    mBgColor = Color.HSVToColor(new float[] {hue, 0.5f, 0.8f});
    mBgActivatedColor = Color.HSVToColor(new float[] {hue, 0.0f, 0.5f});

    mPaint.setAntiAlias(true);
    mPaint.setTypeface(Typeface.DEFAULT_BOLD);
  }
Exemple #6
0
  /**
   * Calculate the color selected by the pointer on the bar.
   *
   * @param coord Coordinate of the pointer.
   */
  private void calculateColor(int coord) {
    coord = coord - mBarPointerHaloRadius;
    if (coord < 0) {
      coord = 0;
    } else if (coord > mBarLength) {
      coord = mBarLength;
    }

    mColor = Color.HSVToColor(Math.round(mPosToOpacFactor * coord), mHSVColor);
    if (Color.alpha(mColor) > 250) {
      mColor = Color.HSVToColor(mHSVColor);
    } else if (Color.alpha(mColor) < 5) {
      mColor = Color.TRANSPARENT;
    }
  }
  @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);
  }
  /** Given a particular color, adjusts its value by a multiplier */
  private static int getPressedColor(int color) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    hsv[2] = hsv[2] * PRESSED_STATE_MULTIPLIER;

    return Color.HSVToColor(hsv);
  }
  @Override
  protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawColor(backgroundColor);
    if (colorWheel != null) canvas.drawBitmap(colorWheel, 0, 0, null);
    if (currentColorCircle != null) {
      float maxRadius =
          canvas.getWidth() / 2f - STROKE_RATIO * (1f + ColorWheelRenderer.GAP_PERCENTAGE);
      float size = maxRadius / density / 2;
      colorWheelFill.setColor(
          Color.HSVToColor(currentColorCircle.getHsvWithLightness(this.lightness)));
      colorWheelFill.setAlpha((int) (alpha * 0xff));
      canvas.drawCircle(
          currentColorCircle.getX(),
          currentColorCircle.getY(),
          size * STROKE_RATIO,
          selectorStroke1);
      canvas.drawCircle(
          currentColorCircle.getX(),
          currentColorCircle.getY(),
          size * (1 + (STROKE_RATIO - 1) / 2),
          selectorStroke2);

      canvas.drawCircle(
          currentColorCircle.getX(), currentColorCircle.getY(), size, alphaPatternPaint);
      canvas.drawCircle(currentColorCircle.getX(), currentColorCircle.getY(), size, colorWheelFill);
    }
  }
    private void createBitmap() {
      if (null == bitmap) return;

      int selectorHalfWidth = 1;
      int w = getWidth();
      int selectedValue = (int) (colorHsv[2] * (w - selectorHalfWidth * 2));

      float value = 0;
      float valueStep = 1f / w;
      float[] hsvTmp = new float[] {colorHsv[0], colorHsv[1], 1f};
      for (int x = 0; x < w; x++) {
        value += valueStep;
        if (x >= selectedValue - selectorHalfWidth && x <= selectedValue + selectorHalfWidth) {
          int intVal = 0xFF - (int) (value * 0xFF);
          int selectorColor = intVal * 0x010101 + 0xFF000000;
          pixels[x] = selectorColor;
        } else {
          hsvTmp[2] = value;
          pixels[x] = Color.HSVToColor(hsvTmp);
        }
      }

      bitmap.setPixels(pixels, 0, w, 0, 0, w, 1);
      invalidate();
    }
 private int getColor(int clusterSize) {
   final float hueRange = 220;
   final float sizeRange = 300;
   final float size = Math.min(clusterSize, sizeRange);
   final float hue = (sizeRange - size) * (sizeRange - size) / (sizeRange * sizeRange) * hueRange;
   return Color.HSVToColor(new float[] {hue, 1f, .6f});
 }
Exemple #12
0
 public static int darker(int color) {
   float[] hsv = new float[3];
   Color.colorToHSV(color, hsv);
   hsv[2] = 1.0f - 0.8f * (1.0f - hsv[2]);
   color = Color.HSVToColor(hsv);
   return color;
 }
Exemple #13
0
  /**
   * Set the bar color. <br>
   * <br>
   * Its discouraged to use this method.
   *
   * @param color
   */
  public void setColor(int color) {
    int x1, y1;
    if (mOrientation == ORIENTATION_HORIZONTAL) {
      x1 = (mBarLength + mBarPointerHaloRadius);
      y1 = mBarThickness;
    } else {
      x1 = mBarThickness;
      y1 = (mBarLength + mBarPointerHaloRadius);
    }

    Color.colorToHSV(color, mHSVColor);
    shader =
        new LinearGradient(
            mBarPointerHaloRadius,
            0,
            x1,
            y1,
            new int[] {Color.HSVToColor(0x00, mHSVColor), color},
            null,
            Shader.TileMode.CLAMP);
    mBarPaint.setShader(shader);
    calculateColor(mBarPointerPosition);
    mBarPointerPaint.setColor(mColor);
    if (mPicker != null) {
      mPicker.setNewCenterColor(mColor);
    }
    invalidate();
  }
Exemple #14
0
  private void drawPointerArrow(Canvas canvas) {

    int centerX = getWidth() / 2;
    int centerY = getHeight() / 2;

    double tipAngle = (colorHSV[2] - 0.5f) * Math.PI;
    double leftAngle = tipAngle + Math.PI / 96;
    double rightAngle = tipAngle - Math.PI / 96;

    double tipAngleX = Math.cos(tipAngle) * outerWheelRadius;
    double tipAngleY = Math.sin(tipAngle) * outerWheelRadius;
    double leftAngleX = Math.cos(leftAngle) * (outerWheelRadius + arrowPointerSize);
    double leftAngleY = Math.sin(leftAngle) * (outerWheelRadius + arrowPointerSize);
    double rightAngleX = Math.cos(rightAngle) * (outerWheelRadius + arrowPointerSize);
    double rightAngleY = Math.sin(rightAngle) * (outerWheelRadius + arrowPointerSize);

    arrowPointerPath.reset();
    arrowPointerPath.moveTo((float) tipAngleX + centerX, (float) tipAngleY + centerY);
    arrowPointerPath.lineTo((float) leftAngleX + centerX, (float) leftAngleY + centerY);
    arrowPointerPath.lineTo((float) rightAngleX + centerX, (float) rightAngleY + centerY);
    arrowPointerPath.lineTo((float) tipAngleX + centerX, (float) tipAngleY + centerY);

    valuePointerArrowPaint.setColor(Color.HSVToColor(colorHSV));
    valuePointerArrowPaint.setStyle(Style.FILL);
    canvas.drawPath(arrowPointerPath, valuePointerArrowPaint);

    valuePointerArrowPaint.setStyle(Style.STROKE);
    valuePointerArrowPaint.setStrokeJoin(Join.ROUND);
    valuePointerArrowPaint.setColor(Color.BLACK);
    canvas.drawPath(arrowPointerPath, valuePointerArrowPaint);
  }
 private void updateAlphaView() {
   final GradientDrawable gd =
       new GradientDrawable(
           GradientDrawable.Orientation.TOP_BOTTOM,
           new int[] {Color.HSVToColor(currentColorHsv), 0x0});
   viewAlphaOverlay.setBackgroundDrawable(gd);
 }
  public static Bitmap applySaturationFilter(Bitmap source, int level) {
    // get image size
    int width = source.getWidth();
    int height = source.getHeight();
    int[] pixels = new int[width * height];
    float[] HSV = new float[3];
    // get pixel array from source
    source.getPixels(pixels, 0, width, 0, 0, width, height);

    int index = 0;
    // iteration through pixels
    for (int y = 0; y < height; ++y) {
      for (int x = 0; x < width; ++x) {
        // get current index in 2D-matrix
        index = y * width + x;
        // convert to HSV
        Color.colorToHSV(pixels[index], HSV);
        // increase Saturation level
        HSV[1] *= level;
        HSV[1] = (float) Math.max(0.0, Math.min(HSV[1], 1.0));
        // take color back
        pixels[index] |= Color.HSVToColor(HSV);
      }
    }
    // output bitmap
    Bitmap bmOut = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    bmOut.setPixels(pixels, 0, width, 0, 0, width, height);
    return bmOut;
  }
Exemple #17
0
  private Bitmap createColorWheelBitmap(int width, int height) {

    Bitmap bitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);

    int colorCount = 12;
    int colorAngleStep = 360 / 12;
    int colors[] = new int[colorCount + 1];
    float hsv[] = new float[] {0f, 1f, 1f};
    for (int i = 0; i < colors.length; i++) {
      hsv[0] = (i * colorAngleStep + 180) % 360;
      colors[i] = Color.HSVToColor(hsv);
    }
    colors[colorCount] = colors[0];

    SweepGradient sweepGradient = new SweepGradient(width / 2, height / 2, colors, null);
    RadialGradient radialGradient =
        new RadialGradient(
            width / 2, height / 2, colorWheelRadius, 0xFFFFFFFF, 0x00FFFFFF, TileMode.CLAMP);
    ComposeShader composeShader =
        new ComposeShader(sweepGradient, radialGradient, PorterDuff.Mode.SRC_OVER);

    colorWheelPaint.setShader(composeShader);

    Canvas canvas = new Canvas(bitmap);
    canvas.drawCircle(width / 2, height / 2, colorWheelRadius, colorWheelPaint);

    return bitmap;
  }
 private int darkenColor(int color) {
   float[] hsv = new float[3];
   Color.colorToHSV(color, hsv);
   hsv[2] = hsv[2] * 0.75f;
   color = Color.HSVToColor(hsv);
   return color;
 }
  private void drawAlphaPanel(Canvas canvas) {

    if (!mShowAlphaPanel || mAlphaRect == null || mAlphaPattern == null) return;

    final RectF rect = mAlphaRect;

    if (BORDER_WIDTH_PX > 0) {
      mBorderPaint.setColor(mBorderColor);
      canvas.drawRect(
          rect.left - BORDER_WIDTH_PX,
          rect.top - BORDER_WIDTH_PX,
          rect.right + BORDER_WIDTH_PX,
          rect.bottom + BORDER_WIDTH_PX,
          mBorderPaint);
    }

    mAlphaPattern.draw(canvas);

    float[] hsv = new float[] {mHue, mSat, mVal};
    int color = Color.HSVToColor(hsv);
    int acolor = Color.HSVToColor(0, hsv);

    mAlphaShader =
        new LinearGradient(
            rect.left, rect.top, rect.right, rect.top, color, acolor, TileMode.CLAMP);

    mAlphaPaint.setShader(mAlphaShader);

    canvas.drawRect(rect, mAlphaPaint);

    if (mAlphaSliderText != null && mAlphaSliderText != "") {
      canvas.drawText(
          mAlphaSliderText, rect.centerX(), rect.centerY() + 4 * mDensity, mAlphaTextPaint);
    }

    float rectWidth = 4 * mDensity / 2;

    Point p = alphaToPoint(mAlpha);

    RectF r = new RectF();
    r.left = p.x - rectWidth;
    r.right = p.x + rectWidth;
    r.top = rect.top - RECTANGLE_TRACKER_OFFSET;
    r.bottom = rect.bottom + RECTANGLE_TRACKER_OFFSET;

    canvas.drawRoundRect(r, 2, 2, mHueTrackerPaint);
  }
  private int adjustColorBrightness(int argb, float factor) {
    float[] hsv = new float[3];
    Color.colorToHSV(argb, hsv);

    hsv[2] = Math.min(hsv[2] * factor, 1f);

    return Color.HSVToColor(Color.alpha(argb), hsv);
  }
 /**
  * Adjust a Paint which, when painted, dims its underlying object to show the effects of varying
  * value (brightness).
  */
 private void setOvalValDimmer() {
   float[] hsv = new float[3];
   hsv[0] = mHSV[0];
   hsv[1] = 0;
   hsv[2] = mHSV[2];
   int gray = Color.HSVToColor(hsv);
   mValDimmer.setColor(gray);
 }
 @ColorInt
 public static int shiftColor(@ColorInt int color, @FloatRange(from = 0.0f, to = 2.0f) float by) {
   if (by == 1f) return color;
   float[] hsv = new float[3];
   Color.colorToHSV(color, hsv);
   hsv[2] *= by; // value component
   return Color.HSVToColor(hsv);
 }
  @ColorInt
  private static int lightenColor(@ColorInt int color) {
    float[] hsv = new float[3];
    Color.colorToHSV(color, hsv);
    hsv[2] /= 0.7f;
    color = Color.HSVToColor(hsv);

    return color;
  }
  private void calcHSVMap() {
    float[] hsv = {1.0f, 1.0f, 1.0f};

    for (int i = 0; i < hsvMap.length; i++) {
      hsv[0] = (float) i * 360 / hsvMap.length;

      hsvMap[i] = Color.HSVToColor(hsv);
    }
  }
  @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;
      }
    }

    if (update) {
      if (mListener != null) {
        mListener.onColorChanged(Color.HSVToColor(mAlpha, new float[] {mHue, mSat, mVal}));
      }

      invalidate();
      return true;
    }

    return super.onTrackballEvent(event);
  }
 public int getColorForPoint(int x, int y, float[] hsv) {
   x -= middleSize / 2;
   y -= middleSize / 2;
   double centerDist = Math.sqrt(x * x + y * y);
   hsv[0] = (float) (Math.atan2(y, x) / Math.PI * 180f) + 180;
   hsv[1] = Math.max(0f, Math.min(1f, (float) (centerDist / innerRadius)));
   hsv[2] = 1;
   return Color.HSVToColor(hsv);
 }
Exemple #27
0
 /**
  * Calculate the color selected by the pointer on the bar.
  *
  * @param coord Coordinate of the pointer.
  */
 private void calculateColor(int coord) {
   coord = coord - mBarPointerHaloRadius;
   if (coord < 0) {
     coord = 0;
   } else if (coord > mBarLength) {
     coord = mBarLength;
   }
   mColor = Color.HSVToColor(new float[] {mHSVColor[0], (float) ((mPosToSatFactor * coord)), 1f});
 }
Exemple #28
0
  @Override
  protected void onRestoreInstanceState(Parcelable state) {
    Bundle savedState = (Bundle) state;

    Parcelable superState = savedState.getParcelable(STATE_PARENT);
    super.onRestoreInstanceState(superState);

    setColor(Color.HSVToColor(savedState.getFloatArray(STATE_COLOR)));
    setSaturation(savedState.getFloat(STATE_SATURATION));
  }
Exemple #29
0
    private static int[] buildHueColorArray() {
      final int[] hues = new int[361];

      int count = 0;
      for (int i = hues.length - 1; i >= 0; i--, count++) {
        hues[count] = Color.HSVToColor(new float[] {i, 1f, 1f});
      }

      return hues;
    }
Exemple #30
0
  @Override
  protected void onRestoreInstanceState(Parcelable state) {
    Bundle savedState = (Bundle) state;

    Parcelable superState = savedState.getParcelable(STATE_PARENT);
    super.onRestoreInstanceState(superState);

    setColor(Color.HSVToColor(savedState.getFloatArray(STATE_COLOR)));
    setOpacity(savedState.getInt(STATE_OPACITY));
  }